From 1e12941e92eea0751baf00b708d112199303054d Mon Sep 17 00:00:00 2001 From: MrBunnyDc Date: Sat, 28 Sep 2024 00:35:52 -0400 Subject: [PATCH] Add Wgc templates --- .../wonejo/wuidebook/api/wgc/WgcTemplate.java | 39 +++++++++ .../api/wgc/WgcTemplateRegistry.java | 53 +++++++++++++ .../wuidebook/impl/wgc/WgcTemplateImpl.java | 79 +++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplate.java create mode 100644 XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplateRegistry.java create mode 100644 XPlat/src/main/java/de/wonejo/wuidebook/impl/wgc/WgcTemplateImpl.java diff --git a/XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplate.java b/XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplate.java new file mode 100644 index 0000000..d17e9e8 --- /dev/null +++ b/XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplate.java @@ -0,0 +1,39 @@ +package de.wonejo.wuidebook.api.wgc; + +import de.wonejo.wuidebook.impl.wgc.WgcTemplateImpl; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; + +import java.util.Set; + +public interface WgcTemplate { + + @NotNull + static Builder builder () { + return WgcTemplateImpl.builderImpl(); + } + + default boolean isTagValid ( WgcTag pTag ) { + for ( ResourceLocation property : this.requiredProperties() ) + if ( !pTag.haveProperty(property) ) return false; + + return true; + } + + Set requiredProperties (); + Set optionalProperties (); + + ResourceLocation templateId (); + + interface Builder { + Builder id ( ResourceLocation pId ); + + Builder required ( ResourceLocation pPropertyKey ); + Builder optional ( ResourceLocation pPropertyKey ); + + Builder requires ( ResourceLocation... pPropertiesKeys ); + Builder optionals ( ResourceLocation... pPropertiesKeys ); + + WgcTemplate build (); + } +} diff --git a/XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplateRegistry.java b/XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplateRegistry.java new file mode 100644 index 0000000..8f6b0e3 --- /dev/null +++ b/XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplateRegistry.java @@ -0,0 +1,53 @@ +package de.wonejo.wuidebook.api.wgc; + +import com.google.common.collect.Maps; +import de.wonejo.wuidebook.api.util.WuidebookRegistryException; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; + +public class WgcTemplateRegistry { + + private static WgcTemplateRegistry INSTANCE; + + public static WgcTemplateRegistry get () { + if ( WgcTemplateRegistry.INSTANCE == null ) WgcTemplateRegistry.INSTANCE = new WgcTemplateRegistry(); + return WgcTemplateRegistry.INSTANCE; + } + + private final Map templates = Maps.newHashMap(); + + private WgcTemplateRegistry () {} + + public void registerTemplate (@NotNull Supplier pTemplate) { + WgcTemplate template = pTemplate.get(); + if ( this.templates.containsKey(template.templateId()) ) throw new WuidebookRegistryException("Can not register Wgc Template because it already exist: " + template.templateId()); + this.templates.put(template.templateId(), template); + } + + public boolean checkProperties ( ResourceLocation pTemplateId, WgcTag pTag ) { + WgcTemplate template = this.templates.get(pTemplateId); + if ( template == null ) throw new WuidebookRegistryException("Wgc template: '" + pTemplateId + "' seems to not be present in registry, can not check properties."); + return template.isTagValid(pTag); + } + + public boolean checkPropertiesWithOptTag ( ResourceLocation pTemplateId, WgcTag pTag ) { + Optional template = this.getOptTemplate(pTemplateId); + return template.map(wgcTemplate -> wgcTemplate.isTagValid(pTag)).orElse(false); + } + + public WgcTemplate getTemplate ( ResourceLocation pTemplateId ) { + WgcTemplate template = this.templates.get(pTemplateId); + if ( template == null ) throw new WuidebookRegistryException("Wgc template: '" + pTemplateId + "' seems to not be present in registry."); + return template; + } + + public Optional getOptTemplate ( ResourceLocation pTemplateId ) { + WgcTemplate template = this.templates.get(pTemplateId); + return Optional.ofNullable(template); + } + +} diff --git a/XPlat/src/main/java/de/wonejo/wuidebook/impl/wgc/WgcTemplateImpl.java b/XPlat/src/main/java/de/wonejo/wuidebook/impl/wgc/WgcTemplateImpl.java new file mode 100644 index 0000000..cd028ab --- /dev/null +++ b/XPlat/src/main/java/de/wonejo/wuidebook/impl/wgc/WgcTemplateImpl.java @@ -0,0 +1,79 @@ +package de.wonejo.wuidebook.impl.wgc; + +import com.google.common.collect.Sets; +import de.wonejo.wuidebook.api.wgc.WgcTemplate; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.Set; + +public class WgcTemplateImpl implements WgcTemplate { + + @NotNull + public static WgcTemplate.Builder builderImpl () { + return new BuilderImpl(); + } + + private final ResourceLocation id; + private final Set requiredProperties; + private final Set optionalProperties; + + private WgcTemplateImpl ( ResourceLocation pId, Set pRequiredProperties, Set pOptionalProperties ) { + this.id = pId; + this.requiredProperties = pRequiredProperties; + this.optionalProperties = pOptionalProperties; + } + + public Set requiredProperties() { + return this.requiredProperties; + } + + public Set optionalProperties() { + return this.optionalProperties; + } + + public ResourceLocation templateId() { + return this.id; + } + + public static class BuilderImpl implements Builder { + + private ResourceLocation id; + private final Set requiredProperties = Sets.newHashSet(); + private final Set optionalProperties = Sets.newHashSet(); + + private BuilderImpl () {} + + public Builder id(ResourceLocation pId) { + this.id = pId; + return this; + } + + public Builder required(ResourceLocation pPropertyKey) { + this.requiredProperties.add(pPropertyKey); + return this; + } + + public Builder optional(ResourceLocation pPropertyKey) { + this.optionalProperties.add(pPropertyKey); + return this; + } + + public Builder requires(ResourceLocation... pPropertiesKeys) { + this.requiredProperties.addAll(Arrays.asList(pPropertiesKeys)); + return this; + } + + public Builder optionals(ResourceLocation... pPropertiesKeys) { + this.optionalProperties.addAll(Arrays.asList(pPropertiesKeys)); + return this; + } + + public WgcTemplate build() { + if ( this.id == null ) throw new IllegalArgumentException("Can not create Wgc template if id is null."); + return new WgcTemplateImpl(this.id, this.requiredProperties, this.optionalProperties); + } + + } +}