diff --git a/src/main/java/vazkii/botania/common/block/subtile/functional/SubTileVinculotus.java b/src/main/java/vazkii/botania/common/block/subtile/functional/SubTileVinculotus.java index 521862367..4d6f96acf 100644 --- a/src/main/java/vazkii/botania/common/block/subtile/functional/SubTileVinculotus.java +++ b/src/main/java/vazkii/botania/common/block/subtile/functional/SubTileVinculotus.java @@ -11,10 +11,13 @@ package vazkii.botania.common.block.subtile.functional; import net.minecraft.entity.monster.EntityEnderman; +import net.minecraft.util.math.BlockPos; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.living.EnderTeleportEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; import vazkii.botania.api.lexicon.LexiconEntry; import vazkii.botania.api.subtile.RadiusDescriptor; import vazkii.botania.api.subtile.SubTileFunctional; @@ -25,21 +28,26 @@ import vazkii.botania.common.lib.LibMisc; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.WeakHashMap; @Mod.EventBusSubscriber(modid = LibMisc.MOD_ID) public class SubTileVinculotus extends SubTileFunctional { - public static final Set existingFlowers = Collections.newSetFromMap(new WeakHashMap<>()); + // Must store position since red string spoofers are only active during onUpdate + // But our main logic runs outside, in the event handler + public static final Map existingFlowers = new WeakHashMap<>(); private static final int RANGE = 64; @Override public void onUpdate() { super.onUpdate(); - if(!supertile.getWorld().isRemote && !existingFlowers.contains(this)) { - existingFlowers.add(this); + if(!supertile.getWorld().isRemote) { + BlockPos pos = existingFlowers.get(this); + if (pos == null || !pos.equals(getPos())) + existingFlowers.put(this, getPos()); } } @@ -73,25 +81,30 @@ public class SubTileVinculotus extends SubTileFunctional { int cost = 50; if(event.getEntityLiving() instanceof EntityEnderman) { - List possibleFlowers = new ArrayList<>(); - for(SubTileVinculotus flower : existingFlowers) { - if(flower.redstoneSignal > 0 || flower.mana <= cost || flower.supertile.getWorld() != event.getEntityLiving().world || flower.supertile.getWorld().getTileEntity(flower.supertile.getPos()) != flower.supertile) + List> possibleFlowers = new ArrayList<>(); + for(Map.Entry e : existingFlowers.entrySet()) { + SubTileVinculotus flower = e.getKey(); + BlockPos activePos = e.getValue(); + + if(flower == null || flower.redstoneSignal > 0 || flower.mana <= cost || flower.supertile.getWorld() != event.getEntityLiving().world || flower.supertile.getWorld().getTileEntity(flower.supertile.getPos()) != flower.supertile) continue; - double x = flower.supertile.getPos().getX() + 0.5; - double y = flower.supertile.getPos().getY() + 1.5; - double z = flower.supertile.getPos().getZ() + 0.5; + double x = activePos.getX() + 0.5; + double y = activePos.getY() + 1.5; + double z = activePos.getZ() + 0.5; if(MathHelper.pointDistanceSpace(x, y, z, event.getTargetX(), event.getTargetY(), event.getTargetZ()) < RANGE) - possibleFlowers.add(flower); + possibleFlowers.add(ImmutablePair.of(flower, activePos)); } if(!possibleFlowers.isEmpty()) { - SubTileVinculotus flower = possibleFlowers.get(event.getEntityLiving().world.rand.nextInt(possibleFlowers.size())); + Pair p = possibleFlowers.get(event.getEntityLiving().world.rand.nextInt(possibleFlowers.size())); + SubTileVinculotus flower = p.getLeft(); + BlockPos activePos = p.getRight(); - double x = flower.supertile.getPos().getX() + 0.5; - double y = flower.supertile.getPos().getY() + 1.5; - double z = flower.supertile.getPos().getZ() + 0.5; + double x = activePos.getX() + 0.5; + double y = activePos.getY() + 1.5; + double z = activePos.getZ() + 0.5; event.setTargetX(x + Math.random() * 3 - 1); event.setTargetY(y);