From 36efdd9a5f1466d999fd9d0c9ea657a3a2ca8a11 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 7 Aug 2022 17:55:39 +0200 Subject: [PATCH] Fix warp entity sound Signed-off-by: yoyosource --- .../bausystem/entities/WarpEntity15.java | 32 ++++++++--- .../bausystem/entities/WarpEntity18.java | 32 ++++++++--- .../bausystem/entities/WarpEntity19.java | 32 ++++++++--- .../features/warp/AbstractWarpEntity.java | 7 +++ .../bausystem/features/warp/WarpListener.java | 54 ++++++++++++++----- 5 files changed, 125 insertions(+), 32 deletions(-) diff --git a/BauSystem_15/src/de/steamwar/bausystem/entities/WarpEntity15.java b/BauSystem_15/src/de/steamwar/bausystem/entities/WarpEntity15.java index ba0934a6..03929086 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/entities/WarpEntity15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/entities/WarpEntity15.java @@ -22,33 +22,34 @@ package de.steamwar.bausystem.entities; import de.steamwar.bausystem.features.warp.AbstractWarpEntity; import de.steamwar.bausystem.shared.BaseArmorStand15; import net.minecraft.server.v1_15_R1.ChatComponentText; +import net.minecraft.server.v1_15_R1.PacketPlayOutEntityMetadata; +import net.minecraft.server.v1_15_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_15_R1.PlayerConnection; import org.bukkit.World; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.util.Vector; public class WarpEntity15 extends BaseArmorStand15 implements AbstractWarpEntity { - - private String name; public WarpEntity15(World world, Vector position, String name) { super(world, position); setInvisible(true); setSmall(true); - this.name = name; + setName(name); this.setNoGravity(true); this.ticksLived = -12000; } @Override public void display(Player player) { - this.setCustomNameVisible(true); - this.setCustomName(new ChatComponentText(name)); sendEntity(player); } @Override public void setName(String name) { - this.name = name; + this.setCustomNameVisible(true); + this.setCustomName(new ChatComponentText(name)); } @Override @@ -57,4 +58,23 @@ public class WarpEntity15 extends BaseArmorStand15 implements AbstractWarpEntity die(); return true; } + + @Override + public void sendMetaData(Player player) { + PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(getId(), datawatcher, true); + PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection; + playerConnection.sendPacket(packetPlayOutEntityMetadata); + } + + @Override + public void teleport(Player player, Vector position) { + setPositionRaw(position.getX(), position.getY(), position.getZ()); + PacketPlayOutEntityTeleport packetPlayOutEntityTeleport = new PacketPlayOutEntityTeleport(this); + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityTeleport); + } + + @Override + public Vector getPosition() { + return new Vector(locX(), locY(), locZ()); + } } diff --git a/BauSystem_18/src/de/steamwar/bausystem/entities/WarpEntity18.java b/BauSystem_18/src/de/steamwar/bausystem/entities/WarpEntity18.java index ef61a4b4..35f8287a 100644 --- a/BauSystem_18/src/de/steamwar/bausystem/entities/WarpEntity18.java +++ b/BauSystem_18/src/de/steamwar/bausystem/entities/WarpEntity18.java @@ -22,33 +22,34 @@ package de.steamwar.bausystem.entities; import de.steamwar.bausystem.features.warp.AbstractWarpEntity; import de.steamwar.bausystem.shared.BaseArmorStand18; import net.minecraft.network.chat.ChatComponentText; +import net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata; +import net.minecraft.network.protocol.game.PacketPlayOutEntityTeleport; +import net.minecraft.server.network.PlayerConnection; import org.bukkit.World; +import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.util.Vector; public class WarpEntity18 extends BaseArmorStand18 implements AbstractWarpEntity { - - private String name; public WarpEntity18(World world, Vector position, String name) { super(world, position); this.j(true); this.a(true); - this.name = name; + setName(name); this.e(true); this.S = -12000; } @Override public void display(Player player) { - this.n(true); - this.a(new ChatComponentText(name)); sendEntity(player); } @Override public void setName(String name) { - this.name = name; + this.n(true); + this.a(new ChatComponentText(name)); } @Override @@ -57,4 +58,23 @@ public class WarpEntity18 extends BaseArmorStand18 implements AbstractWarpEntity ag(); return true; } + + @Override + public void sendMetaData(Player player) { + PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(ae(), Y, true); + PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().b; + playerConnection.a(packetPlayOutEntityMetadata); + } + + @Override + public void teleport(Player player, Vector position) { + setPosRaw(position.getX(), position.getY(), position.getZ(), false); + PacketPlayOutEntityTeleport packetPlayOutEntityTeleport = new PacketPlayOutEntityTeleport(this); + ((CraftPlayer) player).getHandle().b.a(packetPlayOutEntityTeleport); + } + + @Override + public Vector getPosition() { + return new Vector(dc(), de(), di()); + } } diff --git a/BauSystem_19/src/de/steamwar/bausystem/entities/WarpEntity19.java b/BauSystem_19/src/de/steamwar/bausystem/entities/WarpEntity19.java index d97d252d..a86a653e 100644 --- a/BauSystem_19/src/de/steamwar/bausystem/entities/WarpEntity19.java +++ b/BauSystem_19/src/de/steamwar/bausystem/entities/WarpEntity19.java @@ -23,33 +23,34 @@ import de.steamwar.bausystem.features.warp.AbstractWarpEntity; import de.steamwar.bausystem.shared.BaseArmorStand19; import net.minecraft.network.chat.IChatMutableComponent; import net.minecraft.network.chat.contents.LiteralContents; +import net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata; +import net.minecraft.network.protocol.game.PacketPlayOutEntityTeleport; +import net.minecraft.server.network.PlayerConnection; import org.bukkit.World; +import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.util.Vector; public class WarpEntity19 extends BaseArmorStand19 implements AbstractWarpEntity { - - private String name; public WarpEntity19(World world, Vector position, String name) { super(world, position); this.j(true); this.a(true); - this.name = name; + setName(name); this.e(true); this.S = -12000; } @Override public void display(Player player) { - this.n(true); - this.b(IChatMutableComponent.a(new LiteralContents(name))); sendEntity(player); } @Override public void setName(String name) { - this.name = name; + this.n(true); + this.b(IChatMutableComponent.a(new LiteralContents(name))); } @Override @@ -58,4 +59,23 @@ public class WarpEntity19 extends BaseArmorStand19 implements AbstractWarpEntity ag(); return true; } + + @Override + public void sendMetaData(Player player) { + PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(ae(), Y, true); + PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().b; + playerConnection.a(packetPlayOutEntityMetadata); + } + + @Override + public void teleport(Player player, Vector position) { + setPosRaw(position.getX(), position.getY(), position.getZ(), false); + PacketPlayOutEntityTeleport packetPlayOutEntityTeleport = new PacketPlayOutEntityTeleport(this); + ((CraftPlayer) player).getHandle().b.a(packetPlayOutEntityTeleport); + } + + @Override + public Vector getPosition() { + return new Vector(df(), dh(), dj()); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java index 7cf33676..4671d2f0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.features.warp; import de.steamwar.bausystem.shared.AbstractEntity; import org.bukkit.entity.Player; +import org.bukkit.util.Vector; public interface AbstractWarpEntity extends AbstractEntity { @@ -29,4 +30,10 @@ public interface AbstractWarpEntity extends AbstractEntity { void setName(String name); boolean hide(Player player); + + void sendMetaData(Player player); + + void teleport(Player player, Vector position); + + Vector getPosition(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java index 01f5a5fa..406b8c5e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java @@ -33,10 +33,8 @@ import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Consumer; @Linked(LinkageType.LISTENER) public class WarpListener implements Listener { @@ -63,14 +61,41 @@ public class WarpListener implements Listener { createStuff(event.getPlayer(), event.getPlayer().getInventory().getItemInMainHand().getType(), event.isSneaking()); } - private void createStuff(Player p, Material material, boolean sneaking) { - if (warpsShown.containsKey(p)) { - warpsShown.get(p).forEach(warp -> { - warp.hide(p); - }); - warpsShown.remove(p); + private static final class WarpEntityIterator { + + private final List warps; + + public WarpEntityIterator(List warps) { + this.warps = warps == null ? Collections.emptyList() : warps; } + + public AbstractWarpEntity next(Player p, Vector position, String name) { + if (!warps.isEmpty()) { + AbstractWarpEntity abstractWarpEntity = warps.stream().min(Comparator.comparingDouble(o -> o.getPosition().distanceSquared(position))).get(); + warps.remove(abstractWarpEntity); + abstractWarpEntity.teleport(p, position); + abstractWarpEntity.setName(name); + abstractWarpEntity.sendMetaData(p); + return abstractWarpEntity; + } else { + AbstractWarpEntity warp = createEntity(p, position, name); + warp.display(p); + return warp; + } + } + + public void forEach(Consumer consumer) { + warps.forEach(consumer); + } + } + + private void createStuff(Player p, Material material, boolean sneaking) { + WarpEntityIterator it = new WarpEntityIterator(warpsShown.remove(p)); if (material != Material.COMPASS) { + it.forEach(abstractWarpEntity -> { + abstractWarpEntity.teleport(p, new Vector(0, 100000, 0)); + abstractWarpEntity.hide(p); + }); return; } @@ -111,13 +136,14 @@ public class WarpListener implements Listener { name = "§a§l" + name; selected.computeIfAbsent(p, player -> new ArrayList<>()).add(location); } - warps.add(createEntity(p, position, name)); + warps.add(it.next(p, position, name)); }); - warpsShown.put(p, warps); - warps.forEach(warp -> { - warp.display(p); + it.forEach(abstractWarpEntity -> { + abstractWarpEntity.teleport(p, new Vector(0, 100000, 0)); + abstractWarpEntity.hide(p); }); + warpsShown.put(p, warps); } @EventHandler(priority = EventPriority.LOWEST)