From ca17801c86a9fa0696199d91af0ff9739aa3f5c8 Mon Sep 17 00:00:00 2001 From: mezz Date: Fri, 3 Jun 2016 15:54:44 -0700 Subject: [PATCH] Speed up the check for if a block position is currently in a greenhouse --- .../forestry/greenhouse/GreenhouseHelper.java | 13 ++--- .../multiblock/InternalBlockCheck.java | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/main/java/forestry/greenhouse/multiblock/InternalBlockCheck.java diff --git a/src/main/java/forestry/greenhouse/GreenhouseHelper.java b/src/main/java/forestry/greenhouse/GreenhouseHelper.java index 5e1b436eb..21c96c6b6 100644 --- a/src/main/java/forestry/greenhouse/GreenhouseHelper.java +++ b/src/main/java/forestry/greenhouse/GreenhouseHelper.java @@ -19,6 +19,7 @@ import forestry.api.greenhouse.IInternalBlock; import forestry.core.multiblock.IMultiblockControllerInternal; import forestry.core.multiblock.MultiblockRegistry; import forestry.greenhouse.multiblock.IGreenhouseControllerInternal; +import forestry.greenhouse.multiblock.InternalBlockCheck; public class GreenhouseHelper implements IGreenhouseHelper { @@ -28,7 +29,7 @@ public class GreenhouseHelper implements IGreenhouseHelper { for (IMultiblockControllerInternal controllerInternal : MultiblockRegistry.getControllersFromWorld(world)) { if (controllerInternal instanceof IGreenhouseControllerInternal) { if (controllerInternal.isAssembled()) { - if (isPosiotionInGreenhouse((IGreenhouseControllerInternal) controllerInternal, pos)) { + if (isPositionInGreenhouse((IGreenhouseControllerInternal) controllerInternal, pos)) { return ((IGreenhouseControllerInternal) controllerInternal).createState(); } } @@ -38,13 +39,9 @@ public class GreenhouseHelper implements IGreenhouseHelper { return null; } - private static boolean isPosiotionInGreenhouse(IGreenhouseControllerInternal controller, BlockPos pos) { - for (IInternalBlock internalBlock : controller.getInternalBlocks()) { - if (internalBlock.getPos().equals(pos)) { - return true; - } - } - return false; + private static boolean isPositionInGreenhouse(IGreenhouseControllerInternal controller, BlockPos pos) { + IInternalBlock checkBlock = new InternalBlockCheck(pos); + return controller.getInternalBlocks().contains(checkBlock); } } diff --git a/src/main/java/forestry/greenhouse/multiblock/InternalBlockCheck.java b/src/main/java/forestry/greenhouse/multiblock/InternalBlockCheck.java new file mode 100644 index 000000000..b5ff3cce3 --- /dev/null +++ b/src/main/java/forestry/greenhouse/multiblock/InternalBlockCheck.java @@ -0,0 +1,54 @@ +package forestry.greenhouse.multiblock; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Collection; +import java.util.Collections; + +import net.minecraft.util.math.BlockPos; + +import forestry.api.greenhouse.IInternalBlock; +import forestry.api.greenhouse.IInternalBlockFace; + +/** + * Used to check if a blockPos is in a collection of IInternalBlock + */ +public class InternalBlockCheck implements IInternalBlock { + private final BlockPos pos; + + public InternalBlockCheck(BlockPos pos) { + this.pos = pos; + } + + @Nonnull + @Override + public BlockPos getPos() { + return pos; + } + + @Nullable + @Override + public IInternalBlock getRoot() { + return null; + } + + @Nonnull + @Override + public Collection getFaces() { + return Collections.emptyList(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof IInternalBlock)) { + return false; + } + IInternalBlock internalBlock = (IInternalBlock) obj; + return internalBlock.getPos().equals(getPos()); + } + + @Override + public int hashCode() { + return pos.hashCode(); + } +}