From 3679aa618f61bffee7a9ba4a899e3355c45ec4c5 Mon Sep 17 00:00:00 2001 From: Hugo Kerstens Date: Sat, 26 Mar 2016 15:17:00 +0100 Subject: [PATCH] Fix for block place sounds --- .../protocol1_9to1_8/Protocol1_9TO1_8.java | 2 ++ .../listeners/BlockListener.java | 25 +++++++++++++ .../packets/WorldPackets.java | 36 ++++++++++++++++--- .../protocol1_9to1_8/sounds/SoundEffect.java | 2 +- .../storage/EntityTracker.java | 24 +++++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/BlockListener.java diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java index 6aaa003ab..fde396e9c 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java @@ -15,6 +15,7 @@ import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.ArmorListener; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.BlockListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners.CommandBlockListener; import us.myles.ViaVersion.protocols.protocol1_9to1_8.packets.*; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; @@ -96,6 +97,7 @@ public class Protocol1_9TO1_8 extends Protocol { ViaVersionPlugin plugin = (ViaVersionPlugin) Bukkit.getPluginManager().getPlugin("ViaVersion"); Bukkit.getPluginManager().registerEvents(new ArmorListener(plugin), plugin); Bukkit.getPluginManager().registerEvents(new CommandBlockListener(plugin), plugin); + Bukkit.getPluginManager().registerEvents(new BlockListener(plugin), plugin); } @Override diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/BlockListener.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/BlockListener.java new file mode 100644 index 000000000..3dd5fef31 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/listeners/BlockListener.java @@ -0,0 +1,25 @@ +package us.myles.ViaVersion.protocols.protocol1_9to1_8.listeners; + +import lombok.RequiredArgsConstructor; +import org.bukkit.block.Block; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; +import us.myles.ViaVersion.ViaVersionPlugin; +import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker; + +@RequiredArgsConstructor +public class BlockListener implements Listener{ + + private final ViaVersionPlugin plugin; + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void placeBlock(BlockPlaceEvent e) { + if(plugin.isPorted(e.getPlayer())) { + Block b = e.getBlockPlaced(); + plugin.getConnection(e.getPlayer()).get(EntityTracker.class).addBlockInteraction(new Position((long)b.getX(), (long)b.getY(), (long)b.getZ())); + } + } +} diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 28165d8f8..e6b5d8c16 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -74,15 +74,21 @@ public class WorldPackets { int catid = 0; String newname = name; if (effect != null) { - if (effect.isBreaksound()) { - wrapper.cancel(); - return; - } catid = effect.getCategory().getId(); newname = effect.getNewName(); } wrapper.set(Type.STRING, 0, newname); wrapper.write(Type.VAR_INT, catid); // Write Category ID + if(effect != null && effect.isBreaksound()) { + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + int x = wrapper.passthrough(Type.INT); //Position X + int y = wrapper.passthrough(Type.INT); //Position Y + int z = wrapper.passthrough(Type.INT); //Position Z + if(tracker.interactedBlockRecently((int)Math.floor(x/8.0),(int)Math.floor(y/8.0),(int)Math.floor(z/8.0))) { + wrapper.cancel(); + return; + } + } } }); } @@ -322,6 +328,28 @@ public class WorldPackets { } }); + //Register block place to fix sounds + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int face = wrapper.get(Type.BYTE,0); + if(face == 255) + return; + Position p = wrapper.get(Type.POSITION, 0); + long x = p.getX(); long y = p.getY(); long z = p.getZ(); + switch(face) { + case 0: y--; break; + case 1: y++; break; + case 2: z--; break; + case 3: z++; break; + case 4: x--; break; + case 5: x++; break; + } + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.addBlockInteraction(new Position(x,y,z)); + } + }); + } }); } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/SoundEffect.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/SoundEffect.java index f36759f52..2b00b4b88 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/SoundEffect.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/SoundEffect.java @@ -155,7 +155,7 @@ public enum SoundEffect { MOB_CAT_HISS("mob.cat.hiss", "entity.cat.hiss", SoundCategory.NEUTRAL), NOTE_BD("note.bd", "block.note.basedrum", SoundCategory.RECORD), MOB_SPIDER_SAY("mob.spider.say", "entity.spider.hurt", SoundCategory.HOSTILE), - STEP_STONE("step.stone", "block.anvil.hit", SoundCategory.NEUTRAL), + STEP_STONE("step.stone", "block.anvil.hit", SoundCategory.NEUTRAL, true), //Is used for glass placement sound RANDOM_LEVELUP("random.levelup", "entity.player.levelup", SoundCategory.PLAYER), LIQUID_LAVAPOP("liquid.lavapop", "block.lava.pop", SoundCategory.BLOCK), MOB_SHEEP_SAY("mob.sheep.say", "entity.sheep.ambient", SoundCategory.NEUTRAL), diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java index 7304543fa..94dd08ec6 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/EntityTracker.java @@ -1,9 +1,12 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.storage; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import io.netty.buffer.ByteBuf; import lombok.Getter; import lombok.Setter; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import us.myles.ViaVersion.ViaVersionPlugin; @@ -14,12 +17,14 @@ import us.myles.ViaVersion.api.boss.BossColor; import us.myles.ViaVersion.api.boss.BossStyle; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import java.util.*; +import java.util.concurrent.TimeUnit; @Getter public class EntityTracker extends StoredObject { @@ -29,6 +34,7 @@ public class EntityTracker extends StoredObject { private final Map bossBarMap = new HashMap<>(); private final Set validBlocking = new HashSet<>(); private final Set knownHolograms = new HashSet<>(); + private final Cache blockInteractions = CacheBuilder.newBuilder().maximumSize(10).expireAfterAccess(250, TimeUnit.MILLISECONDS).build(); @Setter private boolean blocking = false; @Setter @@ -78,6 +84,24 @@ public class EntityTracker extends StoredObject { } } + public boolean interactedBlockRecently(int x, int y, int z) { + if(blockInteractions.size() == 0) + return false; + Iterator it = blockInteractions.asMap().keySet().iterator(); + while(it.hasNext()) { + Position p = it.next(); + if(p.getX() == x) + if(p.getY() == y) + if(p.getZ() == z) + return true; + } + return false; + } + + public void addBlockInteraction(Position p) { + blockInteractions.put(p,Material.AIR); + } + public void handleMetadata(int entityID, List metadataList) { if (!clientEntityTypes.containsKey(entityID)) return;