Skip to content

Commit

Permalink
Add WGC Value types
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBunnyDc committed Sep 27, 2024
1 parent cb92c02 commit 7e5daff
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.wonejo.wuidebook.api.wgc;

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

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class WgcValueTypeRegistry {
private static WgcValueTypeRegistry INSTANCE;

public static WgcValueTypeRegistry get () {
if ( WgcValueTypeRegistry.INSTANCE == null ) WgcValueTypeRegistry.INSTANCE = new WgcValueTypeRegistry();
return WgcValueTypeRegistry.INSTANCE;
}

private final Map<ResourceLocation, Function<String, WgcTagValue<?>>> tagValues = new HashMap<>();

private WgcValueTypeRegistry () {}

public <T> void registerType( ResourceLocation typeName, Function<String, WgcTagValue<?>> valueCreator ) {
tagValues.put(typeName, valueCreator);
}

@SuppressWarnings("unchecked")
public <T> WgcTagValue<T> createTagValue ( ResourceLocation pInputType, String pValue ) {
Function<String, WgcTagValue<?>> valueCreator = this.tagValues.get(pInputType);
if ( valueCreator == null ) throw new RuntimeException("No matching tag value creator found for type: " + pInputType);
return (WgcTagValue<T>) valueCreator.apply(pValue);
}

public <T> WgcTagValue<T> createTagValue (@NotNull WgcValueTagType<T> pInputType, String pValue ) {
return this.createTagValue(pInputType.id(), pValue);
}

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

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

public final class BooleanWgcValue implements WgcTagValue<TriState> {
public static final WgcValueTagType<TriState> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_bool"), TriState.class);

private final TriState boolState;

public BooleanWgcValue(TriState pBoolState) {
this.boolState = pBoolState;
}

public WgcTagValue<TriState> copy() {
return new BooleanWgcValue(this.boolState);
}

public WgcValueTagType<TriState> type() {
return TYPE;
}

public TriState getValue() {
return this.boolState;
}
}

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

import de.wonejo.wuidebook.api.util.ResourceLocationUtils;
import org.jetbrains.annotations.NotNull;

public final class ByteWgcValue implements WgcNumericTagValue<Byte> {
public static final WgcValueTagType<Byte> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_byte"), Byte.class);

private final byte value;

public ByteWgcValue(byte pValue) {
this.value = pValue;
}

public Number getAsNumber() {
return this.value;
}

@NotNull public WgcTagValue<Byte> copy() {
return new ByteWgcValue(this.value);
}

public WgcValueTagType<Byte> type() {
return TYPE;
}

public Byte getValue() {
return this.value;
}

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

import de.wonejo.wuidebook.api.util.ResourceLocationUtils;
import org.jetbrains.annotations.NotNull;

public final class DoubleWgcValue implements WgcNumericTagValue<Double> {
public static final WgcValueTagType<Double> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_double"), Double.class);

private final double value;

public DoubleWgcValue(double pValue) {
this.value = pValue;
}

public Number getAsNumber() {
return this.value;
}

@NotNull public WgcTagValue<Double> copy() {
return new DoubleWgcValue(this.value);
}

public WgcValueTagType<Double> type() {
return TYPE;
}

public Double getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.wonejo.wuidebook.api.wgc.value;

import de.wonejo.wuidebook.api.util.ResourceLocationUtils;
import org.jetbrains.annotations.NotNull;

public final class FloatWgcValue implements WgcNumericTagValue<Float> {
public static final WgcValueTagType<Float> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_float"), Float.class);

private final float value;

public FloatWgcValue(float pValue) {
this.value = pValue;
}

public Number getAsNumber() {
return this.value;
}

@NotNull public WgcTagValue<Float> copy() {
return new FloatWgcValue(this.value);
}

public WgcValueTagType<Float> type() {
return TYPE;
}

public Float getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.wonejo.wuidebook.api.wgc.value;

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

public final class IntWgcValue implements WgcNumericTagValue<Integer> {
public static final WgcValueTagType<Integer> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_int"), Integer.class);

private final int value;

public IntWgcValue(int pValue) {
this.value = pValue;
}

public Number getAsNumber() {
return this.value;
}

public WgcTagValue<Integer> copy() {
return new IntWgcValue(this.value);
}

public WgcValueTagType<Integer> type() {
return TYPE;
}

public Integer getValue() {
return this.value;
}

public String toString() {
return "int(" + this.value + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.wonejo.wuidebook.api.wgc.value;

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

public final class ShortWgcValue implements WgcNumericTagValue<Short> {
public static final WgcValueTagType<Short> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_short"), Short.class);

private final short value;

public ShortWgcValue(short pValue) {
this.value = pValue;
}

public Number getAsNumber() {
return this.value;
}

public WgcTagValue<Short> copy() {
return new ShortWgcValue(this.value);
}

public WgcValueTagType<Short> type() {
return TYPE;
}

public Short getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.wonejo.wuidebook.api.wgc.value;

import de.wonejo.wuidebook.api.util.ResourceLocationUtils;
import org.jetbrains.annotations.NotNull;

public final class StringWgcValue implements WgcTagValue<String> {
public static final WgcValueTagType<String> TYPE = new WgcValueTagType<>(ResourceLocationUtils.wuidebook("wgc_str"), String.class);

private final String value;

public StringWgcValue(String pValue) {
this.value = pValue;
}

@NotNull public WgcTagValue<String> copy() {
return new StringWgcValue(this.value);
}

public WgcValueTagType<String> type() {
return TYPE;
}

public String getValue() {
return this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.wonejo.wuidebook.api.wgc.value;

public interface WgcNumericTagValue<T extends Number> extends WgcTagValue<T> {

default byte getAsByte () { return this.getAsNumber().byteValue(); }
default short getAsShort () { return this.getAsNumber().shortValue(); }
default int getAsInt () { return this.getAsNumber().intValue(); }
default float getAsFloat () { return this.getAsNumber().floatValue(); }
default double getAsDouble () { return this.getAsNumber().doubleValue(); }

Number getAsNumber ();

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


public interface WgcTagValue<T> {

default String getAsString () { return this.toString(); }

WgcTagValue<T> copy ();
WgcValueTagType<T> type ();

T getValue ();

String toString ();

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

import net.minecraft.resources.ResourceLocation;

public record WgcValueTagType<T>(ResourceLocation id, Class<T> classType) {
}

0 comments on commit 7e5daff

Please sign in to comment.