Skip to content

Commit

Permalink
Add Wgc tag
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBunnyDc committed Sep 27, 2024
1 parent 7f7b144 commit 30fdd7c
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.wonejo.wuidebook.api.util;

public class InvalidWgcFormatException extends RuntimeException {

public InvalidWgcFormatException ( String pMessage, int pCodeLine, Throwable pThrowable ) {
super("An error occurred on line: " + pCodeLine + " of a Wgc file: " + pMessage, pThrowable);
}

public InvalidWgcFormatException ( String pMessage, int pCodeLine ) {
super("An error occurred on line: " + pCodeLine + " of a Wgc file: " + pMessage);
}

public InvalidWgcFormatException ( String pMessage, Throwable pThrowable ) {
super(pMessage, pThrowable);
}

public InvalidWgcFormatException( String pMessage ) {
super(pMessage);
}

}
42 changes: 42 additions & 0 deletions XPlat/src/main/java/de/wonejo/wuidebook/api/wgc/WgcTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.wonejo.wuidebook.api.wgc;

import de.wonejo.wuidebook.api.wgc.value.WgcTagValue;
import de.wonejo.wuidebook.impl.wgc.WgcTagImpl;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

public interface WgcTag {

@NotNull
static WgcTag createTag (String pKey, Map<ResourceLocation, Property<?>> pProperties) {
return WgcTagImpl.createTag(pKey, pProperties);
}

<T> Property<T> getProperty ( ResourceLocation pKey );
boolean haveProperty ( ResourceLocation pKey );

String key ();
Collection<? extends Property<?>> properties ();
Set<ResourceLocation> propertiesKeySet ();

interface Property<T> {

@NotNull
static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, ResourceLocation pValueType, String pValue ) {
return WgcTagImpl.PropertyImpl.createProperty(pKey, pValueType, pValue);
}

@NotNull
static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, WgcTagValue<B> pValue ) {
return WgcTagImpl.PropertyImpl.createProperty(pKey, pValue);
}

ResourceLocation key ();
WgcTagValue<T> value ();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.wonejo.wuidebook.api.wgc.value;

import de.wonejo.wuidebook.api.util.ResourceLocationUtils;

public class NullWgcValue implements WgcTagValue<Object> {
public static final WgcValueTagType<Object> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_null"), Object.class);

public WgcTagValue<Object> copy() {
return null;
}

public WgcValueTagType<Object> type() {
return null;
}

public Object getValue() {
return null;
}
}
89 changes: 89 additions & 0 deletions XPlat/src/main/java/de/wonejo/wuidebook/impl/wgc/WgcTagImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package de.wonejo.wuidebook.impl.wgc;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import de.wonejo.wuidebook.api.wgc.WgcTag;
import de.wonejo.wuidebook.api.wgc.WgcValueTypeRegistry;
import de.wonejo.wuidebook.api.wgc.value.WgcTagValue;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public class WgcTagImpl implements WgcTag {

@NotNull
public static WgcTag createTag ( String pKey, Map<ResourceLocation, Property<?>> pProperties ) {
return new WgcTagImpl(pKey, pProperties);
}

private final String key;
private final Map<ResourceLocation, Property<?>> properties = Maps.newHashMap();

private WgcTagImpl ( String pKey, Map<ResourceLocation, Property<?>> pProperties ) {
this.key = pKey;
this.properties.putAll(pProperties);
}

@SuppressWarnings("unchecked")
public <T> Property<T> getProperty(ResourceLocation pKey) {
Property<T> prop = (Property<T>) this.properties.get(pKey);
if (prop == null)
throw new IllegalArgumentException("Property with key '" + pKey + "' not found.");
return prop;
}

public boolean haveProperty(ResourceLocation pKey) {
return this.properties.containsKey(pKey);
}

public String key() {
return this.key;
}

public Collection<? extends Property<?>> properties() {
return Collections.unmodifiableCollection(this.properties.values());
}

public Set<ResourceLocation> propertiesKeySet() {
return ImmutableSet.copyOf(this.properties.keySet());
}

public static class PropertyImpl<T> implements WgcTag.Property<T> {

@NotNull
public static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, ResourceLocation pValueType, String pValue ) {
return new PropertyImpl<>(pKey, pValueType, pValue);
}

@NotNull
public static <B> WgcTag.Property<B> createProperty ( ResourceLocation pKey, WgcTagValue<B> pValue ) {
return new PropertyImpl<>(pKey, pValue);
}

private final ResourceLocation key;
private final WgcTagValue<T> value;

private PropertyImpl ( ResourceLocation pKey, ResourceLocation pValueType, String pValue ) {
this.key = pKey;
this.value = WgcValueTypeRegistry.get().createTagValue(pValueType, pValue);

if (this.value == null)
throw new IllegalArgumentException("Failed to create WgcTagValue for: " + pValueType);
}

private PropertyImpl ( ResourceLocation pKey, WgcTagValue<T> pValue ) {
this.key = pKey;
this.value = pValue;
}

public ResourceLocation key() {
return this.key;
}

public WgcTagValue<T> value() {
return this.value;
}

}
}

0 comments on commit 30fdd7c

Please sign in to comment.