From d1883d4b85beeac63b541fa82c63878a9185b705 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 10 Jan 2021 12:48:42 +0100 Subject: [PATCH] Dont show tnt exploded in water without -water option --- .../bausystem/tracer/TNTTracer_12.java | 6 +- .../bausystem/tracer/TraceEntity_12.java | 39 ++++++++----- .../bausystem/tracer/TNTTracer_15.java | 5 +- .../bausystem/tracer/TraceEntity_15.java | 42 ++++++++------ .../bausystem/tracer/AbstractTraceEntity.java | 6 +- .../bausystem/tracer/TNTPosition.java | 22 +++---- .../bausystem/tracer/show/Record.java | 8 ++- .../bausystem/tracer/show/mode/Advanced.java | 57 ++++++++++--------- .../bausystem/tracer/show/mode/Basic.java | 34 +++++------ 9 files changed, 118 insertions(+), 101 deletions(-) diff --git a/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java b/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java index 89b8080..52daf91 100644 --- a/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java +++ b/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java @@ -21,14 +21,12 @@ package de.steamwar.bausystem.tracer; import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; import org.bukkit.util.Vector; public class TNTTracer_12 { - public static AbstractTraceEntity create(World world, Vector tntPosition, Player player, boolean exploded, boolean tnt) { - return new TraceEntity_12(world, tntPosition, player, exploded, tnt); + public static AbstractTraceEntity create(World world, Vector tntPosition, boolean tnt) { + return new TraceEntity_12(world, tntPosition, tnt); } public static boolean inWater(World world, Vector tntPosition) { diff --git a/BauSystem_12/src/de/steamwar/bausystem/tracer/TraceEntity_12.java b/BauSystem_12/src/de/steamwar/bausystem/tracer/TraceEntity_12.java index bdb4179..c96f4bc 100644 --- a/BauSystem_12/src/de/steamwar/bausystem/tracer/TraceEntity_12.java +++ b/BauSystem_12/src/de/steamwar/bausystem/tracer/TraceEntity_12.java @@ -28,24 +28,27 @@ import org.bukkit.util.Vector; class TraceEntity_12 extends EntityFallingBlock implements AbstractTraceEntity { - private boolean tnt; + private boolean exploded; + private int references; - public TraceEntity_12(World world, Vector position, Player player, boolean exploded, boolean tnt) { + public TraceEntity_12(World world, Vector position, boolean tnt) { super(((CraftWorld) world).getHandle(), position.getX(), position.getY(), position.getZ(), tnt ? Blocks.TNT.getBlockData() : Blocks.STAINED_GLASS.getBlockData()); - this.tnt = tnt; this.setNoGravity(true); this.ticksLived = -12000; - if (exploded) { - this.setCustomNameVisible(true); - this.setCustomName("Bumm"); - } - - display(player); } @Override - public AbstractTraceEntity display(Player player) { + public void display(Player player, boolean exploded) { + if (!this.exploded && exploded) { + this.setCustomNameVisible(true); + this.setCustomName("Bumm"); + this.exploded = true; + if(references++ > 0) + sendDestroy(player); + }else if(references++ > 0) + return; + PacketPlayOutSpawnEntity packetPlayOutSpawnEntity = new PacketPlayOutSpawnEntity(this, 0, 0); PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection; playerConnection.sendPacket(packetPlayOutSpawnEntity); @@ -53,15 +56,21 @@ class TraceEntity_12 extends EntityFallingBlock implements AbstractTraceEntity { PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(getId(), datawatcher, true); playerConnection.sendPacket(packetPlayOutEntityMetadata); - return this; } @Override - public AbstractTraceEntity hide(Player player) { - PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(new int[]{getId()}); - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy); + public boolean hide(Player player, boolean force) { + if(!force && --references > 0) + return false; - return this; + sendDestroy(player); + die(); + return true; + } + + private void sendDestroy(Player player){ + PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(getId()); + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy); } } diff --git a/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java b/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java index bd0b25e..16be514 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java @@ -24,13 +24,12 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Waterlogged; -import org.bukkit.entity.Player; import org.bukkit.util.Vector; public class TNTTracer_15 { - public static AbstractTraceEntity create(World world, Vector tntPosition, Player player, boolean exploded, boolean tnt) { - return new TraceEntity_15(world, tntPosition, player, exploded, tnt); + public static AbstractTraceEntity create(World world, Vector tntPosition, boolean tnt) { + return new TraceEntity_15(world, tntPosition, tnt); } public static boolean inWater(World world, Vector tntPosition) { diff --git a/BauSystem_15/src/de/steamwar/bausystem/tracer/TraceEntity_15.java b/BauSystem_15/src/de/steamwar/bausystem/tracer/TraceEntity_15.java index 8aaf554..56ecf23 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/tracer/TraceEntity_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/tracer/TraceEntity_15.java @@ -29,42 +29,52 @@ import org.bukkit.util.Vector; class TraceEntity_15 extends EntityFallingBlock implements AbstractTraceEntity { private static final Vec3D ZERO = new Vec3D(0, 0, 0); - private Vector position; - private boolean tnt; + private final Vector position; + private final boolean tnt; - public TraceEntity_15(World world, Vector position, Player player, boolean exploded, boolean tnt) { + private boolean exploded = false; + private int references = 0; + + public TraceEntity_15(World world, Vector position, boolean tnt) { super(((CraftWorld) world).getHandle(), position.getX(), position.getY(), position.getZ(), tnt ? Blocks.TNT.getBlockData() : Blocks.WHITE_STAINED_GLASS.getBlockData()); this.position = position; this.tnt = tnt; this.setNoGravity(true); this.ticksLived = -12000; - if (exploded) { - this.setCustomNameVisible(true); - this.setCustomName(new ChatComponentText("Bumm")); - } - - display(player); } @Override - public AbstractTraceEntity display(Player player) { + public void display(Player player, boolean exploded) { + if (!this.exploded && exploded) { + this.setCustomNameVisible(true); + this.setCustomName(new ChatComponentText("Bumm")); + this.exploded = true; + if(references++ > 0) + sendDestroy(player); + }else if(references++ > 0) + return; + PacketPlayOutSpawnEntity packetPlayOutSpawnEntity = new PacketPlayOutSpawnEntity(getId(), getUniqueID(), position.getX(), position.getY(), position.getZ(), 0, 0, EntityTypes.FALLING_BLOCK, tnt ? Block.getCombinedId(Blocks.TNT.getBlockData()) : Block.getCombinedId(Blocks.WHITE_STAINED_GLASS.getBlockData()), ZERO); PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection; playerConnection.sendPacket(packetPlayOutSpawnEntity); PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(getId(), datawatcher, true); playerConnection.sendPacket(packetPlayOutEntityMetadata); - - return this; } @Override - public AbstractTraceEntity hide(Player player) { - PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(new int[]{getId()}); - ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy); + public boolean hide(Player player, boolean force) { + if(!force && --references > 0) + return false; - return this; + sendDestroy(player); + die(); + return true; } + private void sendDestroy(Player player){ + PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(getId()); + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy); + } } diff --git a/BauSystem_API/src/de/steamwar/bausystem/tracer/AbstractTraceEntity.java b/BauSystem_API/src/de/steamwar/bausystem/tracer/AbstractTraceEntity.java index b2825e5..dcad8cc 100644 --- a/BauSystem_API/src/de/steamwar/bausystem/tracer/AbstractTraceEntity.java +++ b/BauSystem_API/src/de/steamwar/bausystem/tracer/AbstractTraceEntity.java @@ -23,10 +23,8 @@ import org.bukkit.entity.Player; public interface AbstractTraceEntity { - AbstractTraceEntity display(Player player); + void display(Player player, boolean exploded); - AbstractTraceEntity hide(Player player); - - void killEntity(); + boolean hide(Player player, boolean always); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTPosition.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTPosition.java index d877ae5..1529ca8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTPosition.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTPosition.java @@ -19,26 +19,24 @@ package de.steamwar.bausystem.tracer; +import de.steamwar.bausystem.tracer.show.Record; import org.bukkit.entity.Entity; import org.bukkit.util.Vector; public class TNTPosition { - private Vector location; - private Vector previousLocation = null; - private boolean exploded; + private final Record.TNTRecord record; + private final Vector location; + private final Vector previousLocation; + private final boolean exploded; - public TNTPosition(Entity entity, Vector previousLocation, boolean exploded) { - location = entity.getLocation().toVector(); + public TNTPosition(Record.TNTRecord record, Entity entity, Vector previousLocation, boolean exploded) { + this.location = entity.getLocation().toVector(); + this.record = record; this.previousLocation = previousLocation; this.exploded = exploded; } - public TNTPosition(Vector vector) { - location = vector; - this.exploded = false; - } - public Vector getLocation() { return location; } @@ -51,6 +49,10 @@ public class TNTPosition { return exploded; } + public Record.TNTRecord getRecord(){ + return record; + } + @Override public String toString() { return "Position{" + diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/Record.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/Record.java index 1fdbd50..8942080 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/Record.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/Record.java @@ -75,9 +75,9 @@ public class Record { private void add(TNTPrimed tntPrimed, boolean exploded) { TNTPosition position; if (positions.isEmpty()) { - position = new TNTPosition(tntPrimed, null, exploded); + position = new TNTPosition(this, tntPrimed, null, exploded); } else { - position = new TNTPosition(tntPrimed, positions.get(positions.size() - 1).getLocation(), exploded); + position = new TNTPosition(this, tntPrimed, positions.get(positions.size() - 1).getLocation(), exploded); } positions.add(position); TraceShowManager.show(position); @@ -86,5 +86,9 @@ public class Record { public void explode(TNTPrimed tntPrimed) { add(tntPrimed, true); } + + public List getPositions(){ + return positions; + } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Advanced.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Advanced.java index c9bf35a..2b692f7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Advanced.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Advanced.java @@ -26,6 +26,7 @@ import de.steamwar.bausystem.tracer.RoundedTNTPosition; import de.steamwar.bausystem.tracer.TNTPosition; import de.steamwar.bausystem.tracer.show.ShowModeParameter; import org.bukkit.entity.Player; +import org.bukkit.util.Consumer; import org.bukkit.util.Vector; import java.util.HashMap; @@ -33,7 +34,7 @@ import java.util.Map; public class Advanced extends Basic { - private Map updateEntityMap = new HashMap<>(); + private final Map updateEntityMap = new HashMap<>(); public Advanced(Player player, ShowModeParameter showModeParameter) { super(player, showModeParameter); @@ -43,40 +44,42 @@ public class Advanced extends Basic { public void show(TNTPosition position) { super.show(position); - if (position.getPreviousLocation() == null) return; - Vector vector = position.getLocation().clone().subtract(position.getPreviousLocation()); - - Vector updatePointY = position.getPreviousLocation().clone().setY(position.getLocation().getY()); - Vector updatePointXZ; - if (Math.abs(vector.getX()) > Math.abs(vector.getZ())) { - updatePointXZ = updatePointY.clone().setX(position.getLocation().getX()); - } else { - updatePointXZ = updatePointY.clone().setZ(position.getLocation().getZ()); + if (!showModeParameter.isWater() && position.isExploded() && checkWater(position.getLocation())) { + for(TNTPosition pos : position.getRecord().getPositions()){ + applyOnPosition(pos, updatePointPosition -> + updateEntityMap.computeIfPresent(updatePointPosition, (p, point) -> point.hide(player, false) ? null : point)); + } + return; } - if (showModeParameter.isInterpolate_Y() && !position.getLocation().equals(updatePointY)) { - RoundedTNTPosition updatePointPosition = new RoundedTNTPosition(updatePointY); - if (!updateEntityMap.containsKey(updatePointPosition)) { - updateEntityMap.put(updatePointPosition, createEntity(updatePointY, false, false)); - } - } - if (showModeParameter.isInterpolate_XZ() && !position.getLocation().equals(updatePointXZ)) { - RoundedTNTPosition updatePointPosition = new RoundedTNTPosition(updatePointXZ); - if (!updateEntityMap.containsKey(updatePointPosition)) { - updateEntityMap.put(updatePointPosition, createEntity(updatePointXZ, false, false)); - } - } + applyOnPosition(position, updatePointPosition -> + updateEntityMap.computeIfAbsent(updatePointPosition, pos -> createEntity(position.getLocation(), false)) + .display(player, position.isExploded())); } @Override public void hide() { super.hide(); - - updateEntityMap.forEach((roundedTNTPosition, abstractTraceEntity) -> { - abstractTraceEntity.hide(player); - abstractTraceEntity.killEntity(); - }); + updateEntityMap.forEach((roundedTNTPosition, abstractTraceEntity) -> abstractTraceEntity.hide(player, true)); updateEntityMap.clear(); } + private void applyOnPosition(TNTPosition position, Consumer function){ + if (position.getPreviousLocation() == null) return; + + if (showModeParameter.isInterpolate_Y()) { + Vector updatePointY = position.getPreviousLocation().clone().setY(position.getLocation().getY()); + if(!position.getLocation().equals(updatePointY)) + function.accept(new RoundedTNTPosition(updatePointY)); + } + + if (showModeParameter.isInterpolate_XZ()){ + Vector movement = position.getLocation().clone().subtract(position.getPreviousLocation()); + Vector updatePointXZ = Math.abs(movement.getX()) > Math.abs(movement.getZ()) + ? position.getLocation().clone().setZ(position.getPreviousLocation().getZ()) + : position.getLocation().clone().setX(position.getPreviousLocation().getX()); + if(!position.getLocation().equals(updatePointXZ)) + function.accept(new RoundedTNTPosition(updatePointXZ)); + } + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Basic.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Basic.java index a91b117..475445e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Basic.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/show/mode/Basic.java @@ -15,7 +15,7 @@ public class Basic implements ShowMode { protected final Player player; protected final ShowModeParameter showModeParameter; - private Map tntEntityMap = new HashMap<>(); + private final Map tntEntityMap = new HashMap<>(); public Basic(Player player, ShowModeParameter showModeParameter) { this.player = player; @@ -24,37 +24,31 @@ public class Basic implements ShowMode { @Override public void show(TNTPosition position) { - if (showModeParameter.isWater() && checkWater(position.getLocation())) { + if (!showModeParameter.isWater() && position.isExploded() && checkWater(position.getLocation())) { + for(TNTPosition pos : position.getRecord().getPositions()){ + RoundedTNTPosition roundedTNTPosition = new RoundedTNTPosition(pos); + tntEntityMap.computeIfPresent(roundedTNTPosition, (p, tnt) -> tnt.hide(player, false) ? null : tnt); + } return; } RoundedTNTPosition roundedTNTPosition = new RoundedTNTPosition(position); - if (tntEntityMap.containsKey(roundedTNTPosition)) { - if (!position.isExploded()) { - return; - } else { - tntEntityMap.remove(roundedTNTPosition).hide(player).killEntity(); - } - } - tntEntityMap.put(roundedTNTPosition, createEntity(position.getLocation(), position.isExploded(), true)); + AbstractTraceEntity entity = tntEntityMap.computeIfAbsent(roundedTNTPosition, pos -> createEntity(position.getLocation(), true)); + entity.display(player, position.isExploded()); } protected boolean checkWater(Vector position) { - return (VersionedCallable.call(new VersionedCallable<>(() -> TNTTracer_12.inWater(player.getWorld(), position), 8), - new VersionedCallable<>(() -> TNTTracer_15.inWater(player.getWorld(), position), 14))); + return VersionedCallable.call(new VersionedCallable<>(() -> TNTTracer_12.inWater(player.getWorld(), position), 8), + new VersionedCallable<>(() -> TNTTracer_15.inWater(player.getWorld(), position), 14)); } - protected AbstractTraceEntity createEntity(Vector position, boolean exploded, boolean tnt) { - return VersionedCallable.call(new VersionedCallable<>(() -> TNTTracer_12.create(player.getWorld(), position, player, exploded, tnt), 8), - new VersionedCallable<>(() -> TNTTracer_15.create(player.getWorld(), position, player, exploded, tnt), 14)); + protected AbstractTraceEntity createEntity(Vector position, boolean tnt) { + return VersionedCallable.call(new VersionedCallable<>(() -> TNTTracer_12.create(player.getWorld(), position, tnt), 8), + new VersionedCallable<>(() -> TNTTracer_15.create(player.getWorld(), position, tnt), 14)); } @Override public void hide() { - tntEntityMap.forEach((roundedTNTPosition, abstractTraceEntity) -> { - abstractTraceEntity.hide(player); - abstractTraceEntity.killEntity(); - }); + tntEntityMap.forEach((roundedTNTPosition, abstractTraceEntity) -> abstractTraceEntity.hide(player, true)); tntEntityMap.clear(); } - }