Skip to content

Commit

Permalink
Add Wgc templates
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBunnyDc committed Sep 28, 2024
1 parent 4b42164 commit 1e12941
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
39 changes: 39 additions & 0 deletions XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTemplate.java
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation> requiredProperties ();
Set<ResourceLocation> 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 ();
}
}
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation, WgcTemplate> templates = Maps.newHashMap();

private WgcTemplateRegistry () {}

public void registerTemplate (@NotNull Supplier<WgcTemplate> 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<WgcTemplate> 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<WgcTemplate> getOptTemplate ( ResourceLocation pTemplateId ) {
WgcTemplate template = this.templates.get(pTemplateId);
return Optional.ofNullable(template);
}

}
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation> requiredProperties;
private final Set<ResourceLocation> optionalProperties;

private WgcTemplateImpl ( ResourceLocation pId, Set<ResourceLocation> pRequiredProperties, Set<ResourceLocation> pOptionalProperties ) {
this.id = pId;
this.requiredProperties = pRequiredProperties;
this.optionalProperties = pOptionalProperties;
}

public Set<ResourceLocation> requiredProperties() {
return this.requiredProperties;
}

public Set<ResourceLocation> optionalProperties() {
return this.optionalProperties;
}

public ResourceLocation templateId() {
return this.id;
}

public static class BuilderImpl implements Builder {

private ResourceLocation id;
private final Set<ResourceLocation> requiredProperties = Sets.newHashSet();
private final Set<ResourceLocation> 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);
}

}
}

0 comments on commit 1e12941

Please sign in to comment.