mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-13 17:04:22 +00:00
[ci skip] spotlessApply with the new settings
This commit is contained in:
1 parent
8d0e42f2a3
commit
2242bbbdf4
12 files changed
+281
-204
No files matched your search
@@ -1,14 +1,15 @@
|
||||
package com.gtnewhorizon.gtnhlib;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.util.AboveHotbarHUD;
|
||||
import com.gtnewhorizon.gtnhlib.util.AnimatedTooltipHandler;
|
||||
import cpw.mods.fml.common.event.*;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.util.AboveHotbarHUD;
|
||||
import com.gtnewhorizon.gtnhlib.util.AnimatedTooltipHandler;
|
||||
import cpw.mods.fml.common.event.*;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class ClientProxy extends CommonProxy {
|
||||
|
||||
@@ -106,10 +107,10 @@ public class ClientProxy extends CommonProxy {
|
||||
/**
|
||||
* Prints a message above the hotbar
|
||||
*
|
||||
* @param message Color it with EnumChatFormatting
|
||||
* @param message Color it with EnumChatFormatting
|
||||
* @param displayDuration in ticks
|
||||
* @param drawShadow Should the message be drawn with a drawShadow
|
||||
* @param shouldFade Should the message fade away with time
|
||||
* @param drawShadow Should the message be drawn with a drawShadow
|
||||
* @param shouldFade Should the message fade away with time
|
||||
*/
|
||||
@Override
|
||||
public void printMessageAboveHotbar(String message, int displayDuration, boolean drawShadow, boolean shouldFade) {
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.gtnewhorizon.gtnhlib;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.network.NetworkHandler;
|
||||
import com.gtnewhorizon.gtnhlib.network.PacketMessageAboveHotbar;
|
||||
|
||||
import cpw.mods.fml.common.event.FMLInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
@@ -10,9 +15,6 @@ import cpw.mods.fml.common.event.FMLServerStartedEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStartingEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStoppedEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraftforge.common.util.FakePlayer;
|
||||
|
||||
public class CommonProxy {
|
||||
|
||||
@@ -58,16 +60,13 @@ public class CommonProxy {
|
||||
|
||||
/**
|
||||
* Sends packet from server to client that will display message above hotbar.
|
||||
*
|
||||
* @see ClientProxy#printMessageAboveHotbar
|
||||
*/
|
||||
public void sendMessageAboveHotbar(
|
||||
EntityPlayerMP player,
|
||||
IChatComponent chatComponent,
|
||||
int displayDuration,
|
||||
boolean drawShadow,
|
||||
boolean shouldFade) {
|
||||
public void sendMessageAboveHotbar(EntityPlayerMP player, IChatComponent chatComponent, int displayDuration,
|
||||
boolean drawShadow, boolean shouldFade) {
|
||||
if (player instanceof FakePlayer) return;
|
||||
NetworkHandler.instance.sendTo(
|
||||
new PacketMessageAboveHotbar(chatComponent, displayDuration, drawShadow, shouldFade), player);
|
||||
NetworkHandler.instance
|
||||
.sendTo(new PacketMessageAboveHotbar(chatComponent, displayDuration, drawShadow, shouldFade), player);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.gtnewhorizon.gtnhlib;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.SidedProxy;
|
||||
import cpw.mods.fml.common.event.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]")
|
||||
public class GTNHLib {
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
package com.gtnewhorizon.gtnhlib.core;
|
||||
|
||||
import cpw.mods.fml.relauncher.IFMLLoadingPlugin;
|
||||
import java.util.Map;
|
||||
|
||||
import cpw.mods.fml.relauncher.IFMLLoadingPlugin;
|
||||
|
||||
@IFMLLoadingPlugin.MCVersion("1.7.10")
|
||||
@IFMLLoadingPlugin.SortingIndex(-1000)
|
||||
public class GTNHLibCore implements IFMLLoadingPlugin {
|
||||
|
||||
/*
|
||||
* Doesn't currently do anything, other than force the mod to load with coremods so Hodgepodge can use it's functions in mixins/asm
|
||||
* Doesn't currently do anything, other than force the mod to load with coremods so Hodgepodge can use it's
|
||||
* functions in mixins/asm
|
||||
*/
|
||||
@Override
|
||||
public String[] getASMTransformerClass() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.network;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.Tags;
|
||||
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.gtnewhorizon.gtnhlib.network;
|
||||
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.GTNHLib;
|
||||
|
||||
import cpw.mods.fml.common.network.ByteBufUtils;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
public class PacketMessageAboveHotbar implements IMessage {
|
||||
|
||||
@@ -18,8 +20,8 @@ public class PacketMessageAboveHotbar implements IMessage {
|
||||
@SuppressWarnings("unused")
|
||||
public PacketMessageAboveHotbar() {}
|
||||
|
||||
public PacketMessageAboveHotbar(
|
||||
IChatComponent chatComponent, int displayDuration, boolean drawShadow, boolean shouldFade) {
|
||||
public PacketMessageAboveHotbar(IChatComponent chatComponent, int displayDuration, boolean drawShadow,
|
||||
boolean shouldFade) {
|
||||
this.chatComponent = chatComponent;
|
||||
this.displayDuration = displayDuration;
|
||||
this.drawShadow = drawShadow;
|
||||
@@ -43,6 +45,7 @@ public class PacketMessageAboveHotbar implements IMessage {
|
||||
}
|
||||
|
||||
public static class HandlerMessageAboveHotbar implements IMessageHandler<PacketMessageAboveHotbar, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketMessageAboveHotbar message, MessageContext ctx) {
|
||||
GTNHLib.proxy.printMessageAboveHotbar(
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.gtnewhorizon.gtnhlib.reflect;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
@@ -8,15 +7,20 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
|
||||
/**
|
||||
* Utilities for {@link java.lang.reflect.Field} reflection, compatible with Java 8-19.
|
||||
* Can read and write to final static fields unlike the regular reflection API.
|
||||
* Utilities for {@link java.lang.reflect.Field} reflection, compatible with Java 8-19. Can read and write to final
|
||||
* static fields unlike the regular reflection API.
|
||||
*/
|
||||
public class Fields {
|
||||
|
||||
private static final Unsafe UNSAFE;
|
||||
private static final MethodHandles.Lookup mhLookup = MethodHandles.lookup();
|
||||
|
||||
@@ -36,6 +40,7 @@ public class Fields {
|
||||
* How to look up a field in the class?
|
||||
*/
|
||||
public enum LookupType {
|
||||
|
||||
/** Look at the public API (using {@link Class#getField(String)}) */
|
||||
PUBLIC,
|
||||
/** Look at the fields declared in the exact class specified (using {@link Class#getDeclaredField(String)}) */
|
||||
@@ -84,14 +89,15 @@ public class Fields {
|
||||
}
|
||||
|
||||
public static class ClassFields<C> {
|
||||
|
||||
public final Class<C> klass;
|
||||
|
||||
private ClassFields(Class<C> klass) {
|
||||
this.klass = klass;
|
||||
}
|
||||
|
||||
private java.lang.reflect.Field getCheckedFieldImpl(
|
||||
@Nonnull LookupType strategy, @Nonnull String name, @Nullable Class<?> expectedType) {
|
||||
private java.lang.reflect.Field getCheckedFieldImpl(@Nonnull LookupType strategy, @Nonnull String name,
|
||||
@Nullable Class<?> expectedType) {
|
||||
java.lang.reflect.Field javaField = strategy.lookup(klass, name);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
@@ -99,9 +105,13 @@ public class Fields {
|
||||
if (expectedType != null) {
|
||||
Class<?> realType = javaField.getType();
|
||||
if (!realType.equals(expectedType)) {
|
||||
throw new ClassCastException(String.format(
|
||||
"Trying to access field %s#%s of type %s as type %s",
|
||||
klass.getName(), name, realType.getName(), expectedType.getName()));
|
||||
throw new ClassCastException(
|
||||
String.format(
|
||||
"Trying to access field %s#%s of type %s as type %s",
|
||||
klass.getName(),
|
||||
name,
|
||||
realType.getName(),
|
||||
expectedType.getName()));
|
||||
}
|
||||
}
|
||||
return javaField;
|
||||
@@ -109,13 +119,16 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a field in the class using the reflection API and provides non-type-safe accessors to it.
|
||||
*
|
||||
* @param strategy The lookup strategy, see {@link LookupType}
|
||||
* @param name Name of the field we are searching for
|
||||
* @param name Name of the field we are searching for
|
||||
* @return A field wrapper for the found field, or null if not found.
|
||||
*/
|
||||
public Field<?> getUntypedField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), null);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
null);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -124,14 +137,15 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a field in the class using the reflection API and provides type-safe accessors to it.
|
||||
* @param strategy The lookup strategy, see {@link LookupType}
|
||||
* @param name Name of the field we are searching for
|
||||
*
|
||||
* @param strategy The lookup strategy, see {@link LookupType}
|
||||
* @param name Name of the field we are searching for
|
||||
* @param expectedType The type we want to access the field as
|
||||
* @return A field wrapper for the found field, or null if not found.
|
||||
* @throws ClassCastException If the field exists under the given name, but is not of the given type.
|
||||
*/
|
||||
public <F> Field<F> getField(
|
||||
@Nonnull LookupType strategy, @Nonnull String name, @Nonnull Class<F> expectedType) {
|
||||
public <F> Field<F> getField(@Nonnull LookupType strategy, @Nonnull String name,
|
||||
@Nonnull Class<F> expectedType) {
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
@@ -144,11 +158,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Boolean> getBooleanField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), boolean.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
boolean.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -157,11 +174,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Byte> getByteField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), byte.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
byte.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -170,11 +190,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Short> getShortField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), short.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
short.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -183,11 +206,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Integer> getIntField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), int.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
int.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -196,11 +222,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Long> getLongField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), long.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
long.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -209,11 +238,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Character> getCharField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), char.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
char.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -222,11 +254,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Float> getFloatField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), float.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
float.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -235,11 +270,14 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* Looks up a primitive field in the class.
|
||||
*
|
||||
* @see ClassFields#getField(LookupType, String, Class)
|
||||
*/
|
||||
public Field<Double> getDoubleField(@Nonnull LookupType strategy, @Nonnull String name) {
|
||||
java.lang.reflect.Field javaField =
|
||||
getCheckedFieldImpl(Objects.requireNonNull(strategy), Objects.requireNonNull(name), double.class);
|
||||
java.lang.reflect.Field javaField = getCheckedFieldImpl(
|
||||
Objects.requireNonNull(strategy),
|
||||
Objects.requireNonNull(name),
|
||||
double.class);
|
||||
if (javaField == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -247,6 +285,7 @@ public class Fields {
|
||||
}
|
||||
|
||||
public class Field<F> {
|
||||
|
||||
public final java.lang.reflect.Field javaField;
|
||||
public final Class<F> accessType;
|
||||
public final boolean isPrimitive, isStatic, isFinal;
|
||||
@@ -273,8 +312,8 @@ public class Fields {
|
||||
}
|
||||
};
|
||||
} else {
|
||||
final MethodHandle exactGetterHandle =
|
||||
getterHandle.asType(MethodType.methodType(Object.class, Object.class));
|
||||
final MethodHandle exactGetterHandle = getterHandle
|
||||
.asType(MethodType.methodType(Object.class, Object.class));
|
||||
this.getAccessor = obj -> {
|
||||
try {
|
||||
return (F) exactGetterHandle.invokeExact((Object) obj);
|
||||
@@ -286,11 +325,11 @@ public class Fields {
|
||||
// Generate setter
|
||||
BiConsumer<C, F> setAccessor = null;
|
||||
try { // Try the MethodHandle way first, fall back to unsafe is not possible (e.g. static final
|
||||
// fields)
|
||||
// fields)
|
||||
final MethodHandle setterHandle = mhLookup.unreflectSetter(javaField);
|
||||
if (this.isStatic) {
|
||||
final MethodHandle exactSetterHandle =
|
||||
setterHandle.asType(MethodType.methodType(void.class, Object.class));
|
||||
final MethodHandle exactSetterHandle = setterHandle
|
||||
.asType(MethodType.methodType(void.class, Object.class));
|
||||
setAccessor = (obj, val) -> {
|
||||
try {
|
||||
exactSetterHandle.invokeExact((Object) val);
|
||||
@@ -299,8 +338,8 @@ public class Fields {
|
||||
}
|
||||
};
|
||||
} else {
|
||||
final MethodHandle exactSetterHandle =
|
||||
setterHandle.asType(MethodType.methodType(void.class, Object.class, Object.class));
|
||||
final MethodHandle exactSetterHandle = setterHandle
|
||||
.asType(MethodType.methodType(void.class, Object.class, Object.class));
|
||||
setAccessor = (obj, val) -> {
|
||||
try {
|
||||
exactSetterHandle.invokeExact((Object) obj, (Object) val);
|
||||
@@ -312,37 +351,37 @@ public class Fields {
|
||||
} catch (IllegalAccessException e) {
|
||||
// Unsafe fallback
|
||||
final Object staticBase = isStatic ? UNSAFE.staticFieldBase(javaField) : null;
|
||||
final long fieldOffset =
|
||||
isStatic ? UNSAFE.staticFieldOffset(javaField) : UNSAFE.objectFieldOffset(javaField);
|
||||
final long fieldOffset = isStatic ? UNSAFE.staticFieldOffset(javaField)
|
||||
: UNSAFE.objectFieldOffset(javaField);
|
||||
if (!isPrimitive) {
|
||||
setAccessor = (obj, val) ->
|
||||
UNSAFE.putObject(unsafeBaseHelper(staticBase, obj, val), fieldOffset, val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putObject(unsafeBaseHelper(staticBase, obj, val), fieldOffset, val);
|
||||
} else {
|
||||
final Class<?> fieldType = javaField.getType();
|
||||
if (fieldType.equals(boolean.class)) {
|
||||
setAccessor = (obj, val) -> UNSAFE.putBoolean(
|
||||
unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Boolean) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putBoolean(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Boolean) val);
|
||||
} else if (fieldType.equals(byte.class)) {
|
||||
setAccessor = (obj, val) ->
|
||||
UNSAFE.putByte(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Byte) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putByte(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Byte) val);
|
||||
} else if (fieldType.equals(short.class)) {
|
||||
setAccessor = (obj, val) -> UNSAFE.putShort(
|
||||
unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Short) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putShort(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Short) val);
|
||||
} else if (fieldType.equals(int.class)) {
|
||||
setAccessor = (obj, val) -> UNSAFE.putInt(
|
||||
unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Integer) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putInt(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Integer) val);
|
||||
} else if (fieldType.equals(long.class)) {
|
||||
setAccessor = (obj, val) ->
|
||||
UNSAFE.putLong(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Long) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putLong(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Long) val);
|
||||
} else if (fieldType.equals(char.class)) {
|
||||
setAccessor = (obj, val) -> UNSAFE.putChar(
|
||||
unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Character) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putChar(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Character) val);
|
||||
} else if (fieldType.equals(float.class)) {
|
||||
setAccessor = (obj, val) -> UNSAFE.putFloat(
|
||||
unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Float) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putFloat(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Float) val);
|
||||
} else if (fieldType.equals(double.class)) {
|
||||
setAccessor = (obj, val) -> UNSAFE.putDouble(
|
||||
unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Double) val);
|
||||
setAccessor = (obj, val) -> UNSAFE
|
||||
.putDouble(unsafeBaseHelper(staticBase, obj, val), fieldOffset, (Double) val);
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
@@ -351,18 +390,23 @@ public class Fields {
|
||||
this.setAccessor = Objects.requireNonNull(setAccessor);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(
|
||||
"Couldn't create a Field accessor for " + klass.getName() + "#" + javaField.getName(), e);
|
||||
"Couldn't create a Field accessor for " + klass.getName() + "#" + javaField.getName(),
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
private Object unsafeBaseHelper(Object staticBase, Object obj, Object value) {
|
||||
if (!isStatic && !klass.isInstance(obj)) {
|
||||
throw new ClassCastException("Illegal reflective access to field of " + klass
|
||||
+ " using object of type " + obj.getClass());
|
||||
throw new ClassCastException(
|
||||
"Illegal reflective access to field of " + klass
|
||||
+ " using object of type "
|
||||
+ obj.getClass());
|
||||
}
|
||||
if (!accessType.isAssignableFrom(value.getClass())) {
|
||||
throw new ClassCastException("Illegal reflective set of value of type " + value.getClass()
|
||||
+ " to field of type " + javaField.getType());
|
||||
throw new ClassCastException(
|
||||
"Illegal reflective set of value of type " + value.getClass()
|
||||
+ " to field of type "
|
||||
+ javaField.getType());
|
||||
}
|
||||
return isStatic ? staticBase : obj;
|
||||
}
|
||||
@@ -377,7 +421,7 @@ public class Fields {
|
||||
|
||||
/**
|
||||
* @param object Instance of the class, or null if accessing a static field
|
||||
* @param value New value to set in the field
|
||||
* @param value New value to set in the field
|
||||
*/
|
||||
public void setValue(C object, F value) {
|
||||
this.setAccessor.accept(object, value);
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.gtnewhorizon.gtnhlib.util;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class AboveHotbarHUD {
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.gtnewhorizon.gtnhlib.util;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
@@ -14,37 +12,24 @@ import net.minecraft.util.StatCollector;
|
||||
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
|
||||
public class AnimatedTooltipHandler {
|
||||
|
||||
private static final Map<ItemStack, Supplier<String>> tooltipMap = new ItemStackMap<>(false);
|
||||
|
||||
public static final String BLACK,
|
||||
DARK_BLUE,
|
||||
DARK_GREEN,
|
||||
DARK_AQUA,
|
||||
DARK_RED,
|
||||
DARK_PURPLE,
|
||||
GOLD,
|
||||
GRAY,
|
||||
DARK_GRAY,
|
||||
BLUE,
|
||||
GREEN,
|
||||
AQUA,
|
||||
RED,
|
||||
LIGHT_PURPLE,
|
||||
YELLOW,
|
||||
WHITE,
|
||||
OBFUSCATED,
|
||||
BOLD,
|
||||
STRIKETHROUGH,
|
||||
UNDERLINE,
|
||||
ITALIC,
|
||||
public static final String BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY, DARK_GRAY,
|
||||
BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE, OBFUSCATED, BOLD, STRIKETHROUGH, UNDERLINE, ITALIC,
|
||||
RESET;
|
||||
|
||||
public static final Supplier<String> NEW_LINE;
|
||||
|
||||
/**
|
||||
* Helper method to concatenate multiple texts
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
@SafeVarargs
|
||||
@@ -60,6 +45,7 @@ public class AnimatedTooltipHandler {
|
||||
|
||||
/**
|
||||
* Helper method to create a static text
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static Supplier<String> text(String text) {
|
||||
@@ -68,6 +54,7 @@ public class AnimatedTooltipHandler {
|
||||
|
||||
/**
|
||||
* Helper method to create a formatted and static text
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static Supplier<String> text(String format, Object... args) {
|
||||
@@ -76,6 +63,7 @@ public class AnimatedTooltipHandler {
|
||||
|
||||
/**
|
||||
* Helper method to create a translated and static text
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static Supplier<String> translatedText(String translationKey) {
|
||||
@@ -84,6 +72,7 @@ public class AnimatedTooltipHandler {
|
||||
|
||||
/**
|
||||
* Helper method to create a translated, formatted and static text
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static Supplier<String> translatedText(String translationKey, Object... args) {
|
||||
@@ -93,11 +82,15 @@ public class AnimatedTooltipHandler {
|
||||
/**
|
||||
* Helper method to create an animated text
|
||||
* <p>
|
||||
* Taken and adapted from <a href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
* @param text The text to be animated
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry, depending on {@code posstep} and {@code delay}. Wraps around, if shorter than {@code formattingArray}.
|
||||
* Taken and adapted from <a
|
||||
* href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
*
|
||||
* @param text The text to be animated
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry,
|
||||
* depending on {@code posstep} and {@code delay}. Wraps around, if shorter than
|
||||
* {@code formattingArray}.
|
||||
* @author TTFTCUTS, glowredman
|
||||
*/
|
||||
public static Supplier<String> animatedText(String text, int posstep, int delay, String... formattingArray) {
|
||||
@@ -122,55 +115,71 @@ public class AnimatedTooltipHandler {
|
||||
/**
|
||||
* Helper method to create an formatted and animated text
|
||||
* <p>
|
||||
* Taken and adapted from <a href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
* @param format The text to be formatted and animated
|
||||
* @param args The formatting arguments
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry, depending on {@code posstep} and {@code delay}. Wraps around, if shorter than {@code formattingArray}.
|
||||
* Taken and adapted from <a
|
||||
* href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
*
|
||||
* @param format The text to be formatted and animated
|
||||
* @param args The formatting arguments
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry,
|
||||
* depending on {@code posstep} and {@code delay}. Wraps around, if shorter than
|
||||
* {@code formattingArray}.
|
||||
* @author TTFTCUTS, glowredman
|
||||
*/
|
||||
public static Supplier<String> animatedText(
|
||||
String format, Object[] args, int posstep, int delay, String... formattingArray) {
|
||||
public static Supplier<String> animatedText(String format, Object[] args, int posstep, int delay,
|
||||
String... formattingArray) {
|
||||
return animatedText(String.format(Locale.ROOT, format, args), posstep, delay, formattingArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create an translated and animated text
|
||||
* <p>
|
||||
* Taken and adapted from <a href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
* @param translationKey The key used to look up the translation
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry, depending on {@code posstep} and {@code delay}. Wraps around, if shorter than {@code formattingArray}.
|
||||
* Taken and adapted from <a
|
||||
* href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
*
|
||||
* @param translationKey The key used to look up the translation
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry,
|
||||
* depending on {@code posstep} and {@code delay}. Wraps around, if shorter than
|
||||
* {@code formattingArray}.
|
||||
* @author TTFTCUTS, glowredman
|
||||
*/
|
||||
public static Supplier<String> translatedAnimatedText(
|
||||
String translationKey, int posstep, int delay, String... formattingArray) {
|
||||
public static Supplier<String> translatedAnimatedText(String translationKey, int posstep, int delay,
|
||||
String... formattingArray) {
|
||||
return animatedText(StatCollector.translateToLocal(translationKey), posstep, delay, formattingArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create an translated, formatted and animated text
|
||||
* <p>
|
||||
* Taken and adapted from <a href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
* @param translationKey The key used to look up the translation
|
||||
* @param args The formatting arguments
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry, depending on {@code posstep} and {@code delay}. Wraps around, if shorter than {@code formattingArray}.
|
||||
* Taken and adapted from <a
|
||||
* href=https://github.com/GTNewHorizons/Avaritia/blob/7b7eaed652f6be320b10f33d8f8e6a04e66ca14f/src/main/java/fox/spiteful/avaritia/LudicrousText.java#L19>Avaritia</a>
|
||||
*
|
||||
* @param translationKey The key used to look up the translation
|
||||
* @param args The formatting arguments
|
||||
* @param posstep How many steps {@code formattingArray} is shifted each {@code delay}
|
||||
* @param delay How many milliseconds are between each shift of {@code formattingArray}
|
||||
* @param formattingArray An array of formatting codes. Each char of {@code text} will be prefixed by one entry,
|
||||
* depending on {@code posstep} and {@code delay}. Wraps around, if shorter than
|
||||
* {@code formattingArray}.
|
||||
* @author TTFTCUTS, glowredman
|
||||
*/
|
||||
public static Supplier<String> translatedAnimatedText(
|
||||
String translationKey, Object[] args, int posstep, int delay, String... formattingArray) {
|
||||
public static Supplier<String> translatedAnimatedText(String translationKey, Object[] args, int posstep, int delay,
|
||||
String... formattingArray) {
|
||||
return animatedText(
|
||||
StatCollector.translateToLocalFormatted(translationKey, args), posstep, delay, formattingArray);
|
||||
StatCollector.translateToLocalFormatted(translationKey, args),
|
||||
posstep,
|
||||
delay,
|
||||
formattingArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@code tooltip} to all items with {@code oredictName}.
|
||||
* <br><b>Note:</b> The items must be registered to the {@link OreDictionary} when this method is called.
|
||||
* <br><b>Note:</b> Items with equal registry name and meta but different NBT are considered equal.
|
||||
* Add {@code tooltip} to all items with {@code oredictName}. <br>
|
||||
* <b>Note:</b> The items must be registered to the {@link OreDictionary} when this method is called. <br>
|
||||
* <b>Note:</b> Items with equal registry name and meta but different NBT are considered equal.
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static void addOredictTooltip(String oredictName, Supplier<String> tooltip) {
|
||||
@@ -180,10 +189,11 @@ public class AnimatedTooltipHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@code tooltip} to item specified by {@code modID}, {@code registryName} and {@code meta}.
|
||||
* <br><b>Note:</b> The item must be registered to the {@link GameRegistry} when this method is called.
|
||||
* <br><b>Note:</b> Items with equal registry name and meta but different NBT are considered equal.
|
||||
* <br><b>Note:</b> Using {@link OreDictionary#WILDCARD_VALUE} as {@code meta} is allowed.
|
||||
* Add {@code tooltip} to item specified by {@code modID}, {@code registryName} and {@code meta}. <br>
|
||||
* <b>Note:</b> The item must be registered to the {@link GameRegistry} when this method is called. <br>
|
||||
* <b>Note:</b> Items with equal registry name and meta but different NBT are considered equal. <br>
|
||||
* <b>Note:</b> Using {@link OreDictionary#WILDCARD_VALUE} as {@code meta} is allowed.
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static void addItemTooltip(String modID, String registryName, int meta, Supplier<String> tooltip) {
|
||||
@@ -193,9 +203,10 @@ public class AnimatedTooltipHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add {@code tooltip} to {@code item}.
|
||||
* <br><b>Note:</b> Items with equal registry name and meta but different NBT are considered equal.
|
||||
* <br><b>Note:</b> Using {@link OreDictionary#WILDCARD_VALUE} as meta is allowed.
|
||||
* Add {@code tooltip} to {@code item}. <br>
|
||||
* <b>Note:</b> Items with equal registry name and meta but different NBT are considered equal. <br>
|
||||
* <b>Note:</b> Using {@link OreDictionary#WILDCARD_VALUE} as meta is allowed.
|
||||
*
|
||||
* @author glowredman
|
||||
*/
|
||||
public static void addItemTooltip(ItemStack item, Supplier<String> tooltip) {
|
||||
|
||||
@@ -1,29 +1,18 @@
|
||||
package com.gtnewhorizon.gtnhlib.util.map;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2022, GTNH Team, glee8e, Code Chicken,
|
||||
*
|
||||
* This file is originally part of NotEnoughtItem by Code Chicken.
|
||||
* It is adapted to implement the standard Map interface by glee8e
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* Copyright (c) 2022, GTNH Team, glee8e, Code Chicken, This file is originally part of NotEnoughtItem by Code Chicken.
|
||||
* It is adapted to implement the standard Map interface by glee8e This program is free software: you can redistribute
|
||||
* it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 2.1 of the License, or (at your option) any later version. This program is distributed in
|
||||
* the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have
|
||||
* received a copy of the GNU Lesser General Public License along with this program. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import static net.minecraftforge.oredict.OreDictionary.WILDCARD_VALUE;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.Iterators;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.HashMap;
|
||||
@@ -33,13 +22,18 @@ import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.Iterators;
|
||||
|
||||
/**
|
||||
* A map class for ItemStack keys with wildcard damage/NBT. Optimised for lookup.
|
||||
* <p>
|
||||
@@ -54,6 +48,7 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
|
||||
public static final NBTTagCompound WILDCARD_TAG;
|
||||
|
||||
static {
|
||||
@@ -110,8 +105,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
@Override
|
||||
public T put(ItemStack key, T value) {
|
||||
if (key == null || key.getItem() == null || value == null) return null;
|
||||
return itemMap.computeIfAbsent(key.getItem(), k -> new DetailMap(this.NBTSensitive))
|
||||
.put(key, value);
|
||||
return itemMap.computeIfAbsent(key.getItem(), k -> new DetailMap(this.NBTSensitive)).put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -160,6 +154,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private static class StackMetaKey {
|
||||
|
||||
public final int damage;
|
||||
public final NBTTagCompound tag;
|
||||
|
||||
@@ -184,6 +179,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private static class DetailIter<T> implements Iterator<Map.Entry<ItemStack, T>> {
|
||||
|
||||
private final Item owner;
|
||||
private final ItemStackMap<T>.DetailMap backing;
|
||||
|
||||
@@ -202,8 +198,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
private DetailIter(Map.Entry<Item, ItemStackMap<T>.DetailMap> input) {
|
||||
this.owner = input.getKey();
|
||||
this.backing = input.getValue();
|
||||
damageIter =
|
||||
backing.damageMap != null ? backing.damageMap.entrySet().iterator() : null;
|
||||
damageIter = backing.damageMap != null ? backing.damageMap.entrySet().iterator() : null;
|
||||
tagIter = backing.tagMap != null ? backing.tagMap.entrySet().iterator() : null;
|
||||
metaIter = backing.metaMap != null ? backing.metaMap.entrySet().iterator() : null;
|
||||
}
|
||||
@@ -248,7 +243,9 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private enum DetailIterState {
|
||||
|
||||
NOT_STARTED {
|
||||
|
||||
@Override
|
||||
<T> Map.Entry<ItemStack, T> get(DetailIter<T> iter) {
|
||||
throw new AssertionError("Should not call get on NOT_STARTED");
|
||||
@@ -260,9 +257,11 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
},
|
||||
WILDCARD {
|
||||
|
||||
@Override
|
||||
<T> Map.Entry<ItemStack, T> get(DetailIter<T> iter) {
|
||||
return new Map.Entry<ItemStack, T>() {
|
||||
|
||||
private final ItemStack key = wildcard(iter.owner, iter.backing.NBTSensitive);
|
||||
|
||||
@Override
|
||||
@@ -290,13 +289,17 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
},
|
||||
DAMAGE {
|
||||
|
||||
@Override
|
||||
<T> Map.Entry<ItemStack, T> get(DetailIter<T> iter) {
|
||||
assert iter.damageIter != null;
|
||||
Map.Entry<Integer, T> entry = iter.damageIter.next();
|
||||
return new ItemStackEntry<>(
|
||||
newItemStack(
|
||||
iter.owner, 1, entry.getKey(), iter.backing.NBTSensitive ? WILDCARD_TAG : null),
|
||||
iter.owner,
|
||||
1,
|
||||
entry.getKey(),
|
||||
iter.backing.NBTSensitive ? WILDCARD_TAG : null),
|
||||
entry);
|
||||
}
|
||||
|
||||
@@ -307,6 +310,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
},
|
||||
TAG {
|
||||
|
||||
@Override
|
||||
<T> Map.Entry<ItemStack, T> get(DetailIter<T> iter) {
|
||||
assert iter.tagIter != null;
|
||||
@@ -321,12 +325,14 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
},
|
||||
META {
|
||||
|
||||
@Override
|
||||
<T> Map.Entry<ItemStack, T> get(DetailIter<T> iter) {
|
||||
assert iter.metaIter != null;
|
||||
Map.Entry<StackMetaKey, T> entry = iter.metaIter.next();
|
||||
return new ItemStackEntry<>(
|
||||
newItemStack(iter.owner, 1, entry.getKey().damage, entry.getKey().tag), entry);
|
||||
newItemStack(iter.owner, 1, entry.getKey().damage, entry.getKey().tag),
|
||||
entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -336,6 +342,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
},
|
||||
DONE {
|
||||
|
||||
@Override
|
||||
<T> Map.Entry<ItemStack, T> get(DetailIter<T> iter) {
|
||||
throw new AssertionError("Should not call get on DONE");
|
||||
@@ -352,6 +359,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
abstract <T> void remove(DetailIter<T> iter);
|
||||
|
||||
private static class ItemStackEntry<T> implements Map.Entry<ItemStack, T> {
|
||||
|
||||
private final ItemStack key;
|
||||
private final Map.Entry<?, T> entry;
|
||||
|
||||
@@ -379,6 +387,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private class DetailMap {
|
||||
|
||||
private boolean hasWildcard;
|
||||
private T wildcard;
|
||||
private HashMap<Integer, T> damageMap;
|
||||
@@ -460,8 +469,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private void updateSize() {
|
||||
int newSize = (hasWildcard ? 1 : 0)
|
||||
+ (metaMap != null ? metaMap.size() : 0)
|
||||
int newSize = (hasWildcard ? 1 : 0) + (metaMap != null ? metaMap.size() : 0)
|
||||
+ (tagMap != null ? tagMap.size() : 0)
|
||||
+ (damageMap != null ? damageMap.size() : 0);
|
||||
|
||||
@@ -521,6 +529,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private class SetView extends AbstractSet<Map.Entry<ItemStack, T>> {
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
if (!(o instanceof Map.Entry)) return false;
|
||||
@@ -530,8 +539,7 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
|
||||
@Override
|
||||
public boolean add(Map.Entry<ItemStack, T> itemStackTEntry) {
|
||||
return itemStackTEntry.getKey() != null
|
||||
&& itemStackTEntry.getValue() != null
|
||||
return itemStackTEntry.getKey() != null && itemStackTEntry.getValue() != null
|
||||
&& put(itemStackTEntry.getKey(), itemStackTEntry.getValue()) == null;
|
||||
}
|
||||
|
||||
@@ -555,11 +563,13 @@ public final class ItemStackMap<T> extends AbstractMap<ItemStack, T> {
|
||||
}
|
||||
|
||||
private enum KeyType {
|
||||
|
||||
NotWildcard, // MetaMap
|
||||
WildcardMeta, // TagMap
|
||||
WildcardTag, // DamageMap
|
||||
WildcardAll, // WildCard
|
||||
;
|
||||
|
||||
private static final KeyType[] VALUES = values();
|
||||
|
||||
public KeyType withWildcardMeta() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.test.reflect;
|
||||
|
||||
public class FieldsTestSubject {
|
||||
|
||||
static {
|
||||
// Prevent constant inlining by javac
|
||||
staticFinalChar = Character.valueOf('0');
|
||||
|
||||
@@ -2,9 +2,10 @@ package com.gtnewhorizon.gtnhlib.test.reflect;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.reflect.Fields;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.reflect.Fields;
|
||||
|
||||
public class FieldsTests {
|
||||
|
||||
@Test
|
||||
@@ -18,12 +19,12 @@ public class FieldsTests {
|
||||
void objectFields() {
|
||||
final FieldsTestSubject subject = new FieldsTestSubject();
|
||||
final Fields.ClassFields<FieldsTestSubject> classFields = Fields.ofClass(FieldsTestSubject.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<String> dynField =
|
||||
classFields.getField(Fields.LookupType.DECLARED, "dynamicObject", String.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<String> staField =
|
||||
classFields.getField(Fields.LookupType.DECLARED, "staticObject", String.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<String> finField =
|
||||
classFields.getField(Fields.LookupType.DECLARED, "staticFinalObject", String.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<String> dynField = classFields
|
||||
.getField(Fields.LookupType.DECLARED, "dynamicObject", String.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<String> staField = classFields
|
||||
.getField(Fields.LookupType.DECLARED, "staticObject", String.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<String> finField = classFields
|
||||
.getField(Fields.LookupType.DECLARED, "staticFinalObject", String.class);
|
||||
|
||||
assertAll(
|
||||
() -> assertThrows(
|
||||
@@ -78,16 +79,16 @@ public class FieldsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
void objectUntypedFields() {
|
||||
final FieldsTestSubject subject = new FieldsTestSubject();
|
||||
final Fields.ClassFields classFields = Fields.ofClass(FieldsTestSubject.class);
|
||||
final Fields.ClassFields.Field dynField =
|
||||
classFields.getUntypedField(Fields.LookupType.DECLARED, "dynamicObject");
|
||||
final Fields.ClassFields.Field staField =
|
||||
classFields.getUntypedField(Fields.LookupType.DECLARED, "staticObject");
|
||||
final Fields.ClassFields.Field finField =
|
||||
classFields.getUntypedField(Fields.LookupType.DECLARED, "staticFinalObject");
|
||||
final Fields.ClassFields.Field dynField = classFields
|
||||
.getUntypedField(Fields.LookupType.DECLARED, "dynamicObject");
|
||||
final Fields.ClassFields.Field staField = classFields
|
||||
.getUntypedField(Fields.LookupType.DECLARED, "staticObject");
|
||||
final Fields.ClassFields.Field finField = classFields
|
||||
.getUntypedField(Fields.LookupType.DECLARED, "staticFinalObject");
|
||||
|
||||
assertAll(
|
||||
() -> assertThrows(
|
||||
@@ -145,12 +146,12 @@ public class FieldsTests {
|
||||
void intFields() {
|
||||
final FieldsTestSubject subject = new FieldsTestSubject();
|
||||
final Fields.ClassFields<FieldsTestSubject> classFields = Fields.ofClass(FieldsTestSubject.class);
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<Integer> dynField =
|
||||
classFields.getIntField(Fields.LookupType.DECLARED, "dynamicInt");
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<Integer> staField =
|
||||
classFields.getIntField(Fields.LookupType.DECLARED, "staticInt");
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<Integer> finField =
|
||||
classFields.getIntField(Fields.LookupType.DECLARED, "staticFinalInt");
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<Integer> dynField = classFields
|
||||
.getIntField(Fields.LookupType.DECLARED, "dynamicInt");
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<Integer> staField = classFields
|
||||
.getIntField(Fields.LookupType.DECLARED, "staticInt");
|
||||
final Fields.ClassFields<FieldsTestSubject>.Field<Integer> finField = classFields
|
||||
.getIntField(Fields.LookupType.DECLARED, "staticFinalInt");
|
||||
|
||||
assertAll(
|
||||
() -> assertThrows(
|
||||
|
||||
Reference in new issue
Block a user