diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java b/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java index aa040e2a..6d76c3e3 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java @@ -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) { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java b/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java index 2cc392ba..25b3c17d 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java @@ -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); } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLib.java b/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLib.java index dc80c271..c5f5705d 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLib.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/GTNHLib.java @@ -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 { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java index 0a8bd3dd..ac4b6c64 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/core/GTNHLibCore.java @@ -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() { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java index 3f507881..a01d3b82 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java @@ -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; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java index c9283e57..ea652f7d 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/PacketMessageAboveHotbar.java @@ -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 { + @Override public IMessage onMessage(PacketMessageAboveHotbar message, MessageContext ctx) { GTNHLib.proxy.printMessageAboveHotbar( diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/reflect/Fields.java b/src/main/java/com/gtnewhorizon/gtnhlib/reflect/Fields.java index 6079be5c..4d477f0f 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/reflect/Fields.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/reflect/Fields.java @@ -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 { + public final Class klass; private ClassFields(Class 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 Field getField( - @Nonnull LookupType strategy, @Nonnull String name, @Nonnull Class expectedType) { + public Field getField(@Nonnull LookupType strategy, @Nonnull String name, + @Nonnull Class 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 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 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 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 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 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 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 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 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 { + public final java.lang.reflect.Field javaField; public final Class 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 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); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/util/AboveHotbarHUD.java b/src/main/java/com/gtnewhorizon/gtnhlib/util/AboveHotbarHUD.java index 20370c28..98c2a42e 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/util/AboveHotbarHUD.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/util/AboveHotbarHUD.java @@ -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 { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/util/AnimatedTooltipHandler.java b/src/main/java/com/gtnewhorizon/gtnhlib/util/AnimatedTooltipHandler.java index e1ccf270..2d4d323c 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/util/AnimatedTooltipHandler.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/util/AnimatedTooltipHandler.java @@ -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> 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 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 text(String text) { @@ -68,6 +54,7 @@ public class AnimatedTooltipHandler { /** * Helper method to create a formatted and static text + * * @author glowredman */ public static Supplier 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 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 translatedText(String translationKey, Object... args) { @@ -93,11 +82,15 @@ public class AnimatedTooltipHandler { /** * Helper method to create an animated text *

- * Taken and adapted from Avaritia - * @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 Avaritia + * + * @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 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 *

- * Taken and adapted from Avaritia - * @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 Avaritia + * + * @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 animatedText( - String format, Object[] args, int posstep, int delay, String... formattingArray) { + public static Supplier 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 *

- * Taken and adapted from Avaritia - * @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 Avaritia + * + * @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 translatedAnimatedText( - String translationKey, int posstep, int delay, String... formattingArray) { + public static Supplier 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 *

- * Taken and adapted from Avaritia - * @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 Avaritia + * + * @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 translatedAnimatedText( - String translationKey, Object[] args, int posstep, int delay, String... formattingArray) { + public static Supplier 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}. - *
Note: The items must be registered to the {@link OreDictionary} when this method is called. - *
Note: Items with equal registry name and meta but different NBT are considered equal. + * Add {@code tooltip} to all items with {@code oredictName}.
+ * Note: The items must be registered to the {@link OreDictionary} when this method is called.
+ * Note: Items with equal registry name and meta but different NBT are considered equal. + * * @author glowredman */ public static void addOredictTooltip(String oredictName, Supplier tooltip) { @@ -180,10 +189,11 @@ public class AnimatedTooltipHandler { } /** - * Add {@code tooltip} to item specified by {@code modID}, {@code registryName} and {@code meta}. - *
Note: The item must be registered to the {@link GameRegistry} when this method is called. - *
Note: Items with equal registry name and meta but different NBT are considered equal. - *
Note: Using {@link OreDictionary#WILDCARD_VALUE} as {@code meta} is allowed. + * Add {@code tooltip} to item specified by {@code modID}, {@code registryName} and {@code meta}.
+ * Note: The item must be registered to the {@link GameRegistry} when this method is called.
+ * Note: Items with equal registry name and meta but different NBT are considered equal.
+ * Note: Using {@link OreDictionary#WILDCARD_VALUE} as {@code meta} is allowed. + * * @author glowredman */ public static void addItemTooltip(String modID, String registryName, int meta, Supplier tooltip) { @@ -193,9 +203,10 @@ public class AnimatedTooltipHandler { } /** - * Add {@code tooltip} to {@code item}. - *
Note: Items with equal registry name and meta but different NBT are considered equal. - *
Note: Using {@link OreDictionary#WILDCARD_VALUE} as meta is allowed. + * Add {@code tooltip} to {@code item}.
+ * Note: Items with equal registry name and meta but different NBT are considered equal.
+ * Note: Using {@link OreDictionary#WILDCARD_VALUE} as meta is allowed. + * * @author glowredman */ public static void addItemTooltip(ItemStack item, Supplier tooltip) { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/util/map/ItemStackMap.java b/src/main/java/com/gtnewhorizon/gtnhlib/util/map/ItemStackMap.java index 49548365..b2304ccb 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/util/map/ItemStackMap.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/util/map/ItemStackMap.java @@ -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 . + * 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 + * . */ 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. *

@@ -54,6 +48,7 @@ import net.minecraft.nbt.NBTTagCompound; */ @SuppressWarnings("unused") public final class ItemStackMap extends AbstractMap { + public static final NBTTagCompound WILDCARD_TAG; static { @@ -110,8 +105,7 @@ public final class ItemStackMap extends AbstractMap { @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 extends AbstractMap { } private static class StackMetaKey { + public final int damage; public final NBTTagCompound tag; @@ -184,6 +179,7 @@ public final class ItemStackMap extends AbstractMap { } private static class DetailIter implements Iterator> { + private final Item owner; private final ItemStackMap.DetailMap backing; @@ -202,8 +198,7 @@ public final class ItemStackMap extends AbstractMap { private DetailIter(Map.Entry.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 extends AbstractMap { } private enum DetailIterState { + NOT_STARTED { + @Override Map.Entry get(DetailIter iter) { throw new AssertionError("Should not call get on NOT_STARTED"); @@ -260,9 +257,11 @@ public final class ItemStackMap extends AbstractMap { } }, WILDCARD { + @Override Map.Entry get(DetailIter iter) { return new Map.Entry() { + private final ItemStack key = wildcard(iter.owner, iter.backing.NBTSensitive); @Override @@ -290,13 +289,17 @@ public final class ItemStackMap extends AbstractMap { } }, DAMAGE { + @Override Map.Entry get(DetailIter iter) { assert iter.damageIter != null; Map.Entry 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 extends AbstractMap { } }, TAG { + @Override Map.Entry get(DetailIter iter) { assert iter.tagIter != null; @@ -321,12 +325,14 @@ public final class ItemStackMap extends AbstractMap { } }, META { + @Override Map.Entry get(DetailIter iter) { assert iter.metaIter != null; Map.Entry 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 extends AbstractMap { } }, DONE { + @Override Map.Entry get(DetailIter iter) { throw new AssertionError("Should not call get on DONE"); @@ -352,6 +359,7 @@ public final class ItemStackMap extends AbstractMap { abstract void remove(DetailIter iter); private static class ItemStackEntry implements Map.Entry { + private final ItemStack key; private final Map.Entry entry; @@ -379,6 +387,7 @@ public final class ItemStackMap extends AbstractMap { } private class DetailMap { + private boolean hasWildcard; private T wildcard; private HashMap damageMap; @@ -460,8 +469,7 @@ public final class ItemStackMap extends AbstractMap { } 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 extends AbstractMap { } private class SetView extends AbstractSet> { + @Override public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; @@ -530,8 +539,7 @@ public final class ItemStackMap extends AbstractMap { @Override public boolean add(Map.Entry 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 extends AbstractMap { } private enum KeyType { + NotWildcard, // MetaMap WildcardMeta, // TagMap WildcardTag, // DamageMap WildcardAll, // WildCard ; + private static final KeyType[] VALUES = values(); public KeyType withWildcardMeta() { diff --git a/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTestSubject.java b/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTestSubject.java index c3a081ef..869530a8 100644 --- a/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTestSubject.java +++ b/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTestSubject.java @@ -1,6 +1,7 @@ package com.gtnewhorizon.gtnhlib.test.reflect; public class FieldsTestSubject { + static { // Prevent constant inlining by javac staticFinalChar = Character.valueOf('0'); diff --git a/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTests.java b/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTests.java index 8f6ce2a7..594d6093 100644 --- a/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTests.java +++ b/src/test/java/com/gtnewhorizon/gtnhlib/test/reflect/FieldsTests.java @@ -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 classFields = Fields.ofClass(FieldsTestSubject.class); - final Fields.ClassFields.Field dynField = - classFields.getField(Fields.LookupType.DECLARED, "dynamicObject", String.class); - final Fields.ClassFields.Field staField = - classFields.getField(Fields.LookupType.DECLARED, "staticObject", String.class); - final Fields.ClassFields.Field finField = - classFields.getField(Fields.LookupType.DECLARED, "staticFinalObject", String.class); + final Fields.ClassFields.Field dynField = classFields + .getField(Fields.LookupType.DECLARED, "dynamicObject", String.class); + final Fields.ClassFields.Field staField = classFields + .getField(Fields.LookupType.DECLARED, "staticObject", String.class); + final Fields.ClassFields.Field 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 classFields = Fields.ofClass(FieldsTestSubject.class); - final Fields.ClassFields.Field dynField = - classFields.getIntField(Fields.LookupType.DECLARED, "dynamicInt"); - final Fields.ClassFields.Field staField = - classFields.getIntField(Fields.LookupType.DECLARED, "staticInt"); - final Fields.ClassFields.Field finField = - classFields.getIntField(Fields.LookupType.DECLARED, "staticFinalInt"); + final Fields.ClassFields.Field dynField = classFields + .getIntField(Fields.LookupType.DECLARED, "dynamicInt"); + final Fields.ClassFields.Field staField = classFields + .getIntField(Fields.LookupType.DECLARED, "staticInt"); + final Fields.ClassFields.Field finField = classFields + .getIntField(Fields.LookupType.DECLARED, "staticFinalInt"); assertAll( () -> assertThrows(