diff --git a/src/main/java/hellfirepvp/beebetteratbees/client/gui/AbstractTreeGUIHandler.java b/src/main/java/hellfirepvp/beebetteratbees/client/gui/AbstractTreeGUIHandler.java index 22b2f22..a2e626d 100644 --- a/src/main/java/hellfirepvp/beebetteratbees/client/gui/AbstractTreeGUIHandler.java +++ b/src/main/java/hellfirepvp/beebetteratbees/client/gui/AbstractTreeGUIHandler.java @@ -41,8 +41,8 @@ import codechicken.nei.recipe.RecipeInfo; public abstract class AbstractTreeGUIHandler implements ICraftingHandler, IUsageHandler { public int cycleticks = Math.abs((int) System.currentTimeMillis()); - public LinkedList arecipes = new LinkedList(); - public LinkedList transferRects = new LinkedList(); + public LinkedList arecipes = new LinkedList<>(); + public LinkedList transferRects = new LinkedList<>(); public AbstractTreeGUIHandler() { loadTransferRects(); @@ -86,7 +86,7 @@ public abstract class AbstractTreeGUIHandler implements ICraftingHandler, IUsage public List> getRecipeTransferRectGuis() { Class clazz = getGuiClass(); if (clazz != null) { - LinkedList> list = new LinkedList>(); + LinkedList> list = new LinkedList<>(); list.add(clazz); return list; } @@ -192,9 +192,9 @@ public abstract class AbstractTreeGUIHandler implements ICraftingHandler, IUsage @Override public List handleTooltip(GuiRecipe gui, List currenttip, int recipe) { - if ((GuiContainerManager.shouldShowTooltip(gui)) && (currenttip.size() == 0)) { + if (GuiContainerManager.shouldShowTooltip(gui) && currenttip.isEmpty()) { Point offset = gui.getRecipePosition(recipe); - currenttip = transferRectTooltip(gui, this.transferRects, offset.x, offset.y, currenttip); + return transferRectTooltip(gui, this.transferRects, offset.x, offset.y, currenttip); } return currenttip; } @@ -265,7 +265,7 @@ public abstract class AbstractTreeGUIHandler implements ICraftingHandler, IUsage public static class RecipeTransferRectHandler implements IContainerInputHandler, IContainerTooltipHandler { - private static HashMap, HashSet> guiMap = new HashMap, HashSet>(); + private static final HashMap, HashSet> guiMap = new HashMap<>(); public static void registerRectsToGuis(List> classes, List rects) { @@ -273,11 +273,7 @@ public abstract class AbstractTreeGUIHandler implements ICraftingHandler, IUsage return; } for (Class clazz : classes) { - HashSet set = guiMap.get(clazz); - if (set == null) { - set = new HashSet(); - guiMap.put(clazz, set); - } + HashSet set = guiMap.computeIfAbsent(clazz, k -> new HashSet<>()); set.addAll(rects); } } @@ -346,10 +342,9 @@ public abstract class AbstractTreeGUIHandler implements ICraftingHandler, IUsage if (!canHandle(gui)) { return currenttip; } - if ((GuiContainerManager.shouldShowTooltip(gui)) && (currenttip.size() == 0)) { + if (GuiContainerManager.shouldShowTooltip(gui) && currenttip.isEmpty()) { int[] offset = RecipeInfo.getGuiOffset(gui); - currenttip = AbstractTreeGUIHandler - .transferRectTooltip(gui, guiMap.get(gui.getClass()), offset[0], offset[1], currenttip); + return transferRectTooltip(gui, guiMap.get(gui.getClass()), offset[0], offset[1], currenttip); } return currenttip; } diff --git a/src/main/java/hellfirepvp/beebetteratbees/client/gui/BBABGuiRecipeTreeHandler.java b/src/main/java/hellfirepvp/beebetteratbees/client/gui/BBABGuiRecipeTreeHandler.java index 33d92d9..5aee0d1 100644 --- a/src/main/java/hellfirepvp/beebetteratbees/client/gui/BBABGuiRecipeTreeHandler.java +++ b/src/main/java/hellfirepvp/beebetteratbees/client/gui/BBABGuiRecipeTreeHandler.java @@ -3,7 +3,6 @@ package hellfirepvp.beebetteratbees.client.gui; import java.awt.Color; import java.awt.Point; import java.awt.Rectangle; -import java.lang.reflect.Field; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -13,7 +12,6 @@ import java.util.Map; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.Tessellator; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; @@ -24,7 +22,6 @@ import org.lwjgl.opengl.GL11; import codechicken.lib.gui.GuiDraw; import codechicken.nei.guihook.GuiContainerManager; import codechicken.nei.recipe.GuiRecipe; -import cpw.mods.fml.relauncher.ReflectionHelper; import forestry.api.apiculture.IBeeMutation; import forestry.api.apiculture.IBeeRoot; import forestry.api.genetics.AlleleManager; @@ -49,8 +46,8 @@ public class BBABGuiRecipeTreeHandler extends AbstractTreeGUIHandler { private static IBeeRoot speciesRoot; public static List getMutationsWithResult(IAllele allele) { - if (speciesRoot == null) return new LinkedList(); - LinkedList out = new LinkedList(); + if (speciesRoot == null) return new LinkedList<>(); + LinkedList out = new LinkedList<>(); for (IBeeMutation mutation : speciesRoot.getMutations(false)) { if (mutation.getTemplate()[0].equals(allele)) out.add(mutation); } @@ -114,14 +111,14 @@ public class BBABGuiRecipeTreeHandler extends AbstractTreeGUIHandler { cleanupDuplicateRecipes(); } - private Map> tipBoxes = new HashMap>(); + private Map> tipBoxes = new HashMap<>(); @Override public void drawExtras(int recipe) { CachedRecipe rec = this.arecipes.get(recipe); - if (rec instanceof CachedBeeMutationTree) { - Map> boxes = new HashMap>(4); - CachedBeeMutationTree.PositionedMutationNodeStack root = ((CachedBeeMutationTree) rec).getRootStack(); + if (rec instanceof CachedBeeMutationTree cachedTree) { + Map> boxes = new HashMap<>(4); + CachedBeeMutationTree.PositionedMutationNodeStack root = cachedTree.getRootStack(); drawExtrasFrom(root, boxes); this.tipBoxes = boxes; } @@ -224,32 +221,19 @@ public class BBABGuiRecipeTreeHandler extends AbstractTreeGUIHandler { @Override public List handleTooltip(GuiRecipe gui, List currenttip, int recipe) { - if (GuiContainerManager.shouldShowTooltip(gui) && currenttip.size() == 0) { + if (GuiContainerManager.shouldShowTooltip(gui) && currenttip.isEmpty()) { Point pos = GuiDraw.getMousePosition(); - Point guiOffset = getGuiOffset(gui); + Point guiOffset = new Point(gui.guiLeft, gui.guiTop); Point relMouse = new Point(pos.x - guiOffset.x - 5, pos.y - guiOffset.y - 16); for (Rectangle rec : tipBoxes.keySet()) { if (rec.contains(relMouse)) { - return new LinkedList(tipBoxes.get(rec)); + return new LinkedList<>(tipBoxes.get(rec)); } } } return super.handleTooltip(gui, currenttip, recipe); } - private Point getGuiOffset(GuiContainer ct) { - try { - Field fP = ReflectionHelper.findField(GuiContainer.class, "guiLeft", "field_147003_i"); - fP.setAccessible(true); - int left = (Integer) fP.get(ct); - fP = ReflectionHelper.findField(GuiContainer.class, "guiTop", "field_147009_r"); - fP.setAccessible(true); - int top = (Integer) fP.get(ct); - return new Point(left, top); - } catch (Exception e) {} - return new Point(0, 0); - } - private static void drawLine(double lx, double ly, double hx, double hy, Color color) { GL11.glPushMatrix(); GL11.glDisable(GL11.GL_TEXTURE_2D); diff --git a/src/main/java/hellfirepvp/beebetteratbees/client/gui/CachedBeeMutationTree.java b/src/main/java/hellfirepvp/beebetteratbees/client/gui/CachedBeeMutationTree.java index 188d61f..871db31 100644 --- a/src/main/java/hellfirepvp/beebetteratbees/client/gui/CachedBeeMutationTree.java +++ b/src/main/java/hellfirepvp/beebetteratbees/client/gui/CachedBeeMutationTree.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import codechicken.nei.PositionedStack; import forestry.api.apiculture.IBeeMutation; @@ -30,8 +31,8 @@ public class CachedBeeMutationTree extends CachedRecipe { private static final int X_SEPERATION_THRESHOLD = 7; private static final int Y_OFFSET = 0; - private SimpleBinaryTree mutationTree; - private List evaluatedBeePositions; + private final SimpleBinaryTree mutationTree; + private final List evaluatedBeePositions; private int evaluatedMaxX; public final boolean oversized; private PositionedMutationNodeStack rootStack; @@ -39,15 +40,15 @@ public class CachedBeeMutationTree extends CachedRecipe { public CachedBeeMutationTree(IBeeMutation parentMutation) { // parentMutation.getTemplate() Gets results primary at array[0], secondary at array[1] // parentMutation.getAllele0() or getAllele1() Gets bees needed for mutation. - this.mutationTree = new SimpleBinaryTree( + this.mutationTree = new SimpleBinaryTree<>( 4, parentMutation.getTemplate()[0], - new SimpleBinaryTree.RootProvider() { + new SimpleBinaryTree.RootProvider<>() { @Override public IAllele[] provideSubNodes(IAllele superNode) { List mutations = BBABGuiRecipeTreeHandler.getMutationsWithResult(superNode); - if (mutations.size() > 0) { + if (!mutations.isEmpty()) { IBeeMutation mutation = mutations.get(0); return new IAllele[] { mutation.getAllele0(), mutation.getAllele1() }; @@ -58,12 +59,12 @@ public class CachedBeeMutationTree extends CachedRecipe { }); if (!ModConfig.showDuplicateTrees) { - List foundMutationTrees = new ArrayList(); + List foundMutationTrees = new ArrayList<>(); removeAndReplaceDuplicates(this.mutationTree.getRoot(), foundMutationTrees); } // Important: We don't need to buffer root, because that's the "result" - this.evaluatedBeePositions = new LinkedList(); + this.evaluatedBeePositions = new LinkedList<>(); int iterationDepth = 3; int maxTotalDepth = Math.min( iterationDepth, @@ -76,7 +77,7 @@ public class CachedBeeMutationTree extends CachedRecipe { int yStep = 110 / maxTotalDepth; this.oversized = checkSeparationWidth(maxTotalDepth, X_SEPERATION_THRESHOLD); - this.evaluatedMaxX = /* this.oversized ? buildNewMaxX(maxTotalDepth, X_SEPERATION_THRESHOLD) : */ MAX_X; + this.evaluatedMaxX = MAX_X; int center = (MIN_X + this.evaluatedMaxX) / 2; PositionedMutationNodeStack leftChild = placeInRenderBuffer( @@ -100,13 +101,13 @@ public class CachedBeeMutationTree extends CachedRecipe { mutationTree.getRoot() .getValue()); float ch = -1; - Collection requirements = new LinkedList(); - if (mutationsToRoot.size() > 0) { + Collection requirements = new LinkedList<>(); + if (!mutationsToRoot.isEmpty()) { IBeeMutation mut = mutationsToRoot.get(0); ch = mut.getBaseChance(); try { requirements = mut.getSpecialConditions(); - } catch (Throwable tr) {} + } catch (Throwable ignored) {} } this.rootStack = new PositionedMutationNodeStack( @@ -129,7 +130,7 @@ public class CachedBeeMutationTree extends CachedRecipe { // lines. if (discoveredMutations.contains(node.getValue())) { List mutations = getMutationsWithResult(node.getValue()); - if (mutations.size() > 0) node.removeDuplicate(); // Only if it actually has mutations that has this node as + if (!mutations.isEmpty()) node.removeDuplicate(); // Only if it actually has mutations that has this node as // result. } else { discoveredMutations.add(node.getValue()); @@ -140,13 +141,6 @@ public class CachedBeeMutationTree extends CachedRecipe { } } - /* - * private int buildNewMaxX(int maxTotalDepth, int xSeparationThreshold) { - * int maxDivision = (int) Math.pow(2, maxTotalDepth + 1); - * return MIN_X + (maxDivision * xSeparationThreshold); - * } - */ - private boolean checkSeparationWidth(int maxTotalDepth, int xSeparationThreshold) { double maxDivision = Math.pow(2, maxTotalDepth + 1); int resultingLLWidth = (int) (120 / maxDivision); // LowestLevelWidth @@ -160,14 +154,14 @@ public class CachedBeeMutationTree extends CachedRecipe { if (iterationMaxCount < 0 || node.getMaxFollowingDepth() <= 0) { List mutationsToRoot = getMutationsWithResult(node.getValue()); float ch = -1; - Collection requirements = new LinkedList(); - if (mutationsToRoot.size() > 0) { + Collection requirements = new LinkedList<>(); + if (!mutationsToRoot.isEmpty()) { IBeeMutation mut = mutationsToRoot.get(0); ch = mut.getBaseChance(); try { requirements = mut.getSpecialConditions(); } catch (Throwable tr) { - requirements = new LinkedList(); + requirements = new LinkedList<>(); } } PositionedMutationNodeStack leaf = new PositionedMutationNodeStack( // Leaf @@ -185,14 +179,14 @@ public class CachedBeeMutationTree extends CachedRecipe { List mutations = getMutationsWithResult(node.getValue()); float ch = -1; - Collection requirements = new LinkedList(); - if (mutations.size() > 0) { + Collection requirements = new LinkedList<>(); + if (!mutations.isEmpty()) { IBeeMutation mut = mutations.get(0); ch = mut.getBaseChance(); try { requirements = mut.getSpecialConditions(); } catch (Throwable tr) { - requirements = new LinkedList(); + requirements = new LinkedList<>(); } } @@ -218,7 +212,7 @@ public class CachedBeeMutationTree extends CachedRecipe { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CachedBeeMutationTree that = (CachedBeeMutationTree) o; - return !(mutationTree != null ? !mutationTree.equals(that.mutationTree) : that.mutationTree != null); + return Objects.equals(mutationTree, that.mutationTree); } @Override @@ -237,7 +231,7 @@ public class CachedBeeMutationTree extends CachedRecipe { @Override public List getIngredients() { - return new ArrayList(evaluatedBeePositions); + return new ArrayList<>(evaluatedBeePositions); } public static class PositionedMutationNodeStack extends PositionedStack { diff --git a/src/main/java/hellfirepvp/beebetteratbees/client/util/SimpleBinaryTree.java b/src/main/java/hellfirepvp/beebetteratbees/client/util/SimpleBinaryTree.java index a42a78f..f4b05d3 100644 --- a/src/main/java/hellfirepvp/beebetteratbees/client/util/SimpleBinaryTree.java +++ b/src/main/java/hellfirepvp/beebetteratbees/client/util/SimpleBinaryTree.java @@ -1,5 +1,7 @@ package hellfirepvp.beebetteratbees.client.util; +import java.util.Objects; + /** * HellFirePvP@Admin * Date: 29.04.2016 / 00:23 @@ -8,8 +10,8 @@ package hellfirepvp.beebetteratbees.client.util; */ public class SimpleBinaryTree { - private Node root; - private RootProvider rootProvider; + private final Node root; + private final RootProvider rootProvider; public SimpleBinaryTree(int maxDepth, E rootValue, RootProvider provider) { this.rootProvider = provider; @@ -27,12 +29,12 @@ public class SimpleBinaryTree { private Node buildSubTree(E superNodeValue, int maxFollowingDepth) { E[] subNodes = rootProvider.provideSubNodes(superNodeValue); if (subNodes == null || subNodes.length == 0 || maxFollowingDepth <= 0) { - return new Leaf(superNodeValue); + return new Leaf<>(superNodeValue); } else { Node left = buildSubTree(subNodes[0], maxFollowingDepth - 1); Node right = buildSubTree(subNodes[1], maxFollowingDepth - 1); int depth = Math.max(left.getMaxFollowingDepth(), right.getMaxFollowingDepth()); - return new Node(superNodeValue, left, right, depth + 1); + return new Node<>(superNodeValue, left, right, depth + 1); } } @@ -41,7 +43,7 @@ public class SimpleBinaryTree { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SimpleBinaryTree that = (SimpleBinaryTree) o; - return !(root != null ? !root.equals(that.root) : that.root != null); + return Objects.equals(root, that.root); } @Override @@ -89,7 +91,7 @@ public class SimpleBinaryTree { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; - return !(value != null ? !value.equals(node.value) : node.value != null); + return Objects.equals(value, node.value); } @Override