From 5ef109c0374a6decc08c61fc43f48d7c050fb65b Mon Sep 17 00:00:00 2001 From: Matsv Date: Wed, 31 May 2017 21:02:34 +0200 Subject: [PATCH] Rewrite SoundRewriter and generate placeholders for 1.11 --- README.md | 3 +- .../nl/matsv/viabackwards/ViaBackwards.java | 1 + .../api/rewriters/SoundIdRewriter.java | 51 -------- .../api/rewriters/SoundRewriter.java | 75 ++++++++++++ .../Protocol1_10To1_11.java | 2 + .../packets/SoundPackets.java | 112 ++++++++++++++++++ .../packets/SoundPackets.java | 45 +++---- 7 files changed, 215 insertions(+), 74 deletions(-) delete mode 100644 core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundIdRewriter.java create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java create mode 100644 core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets.java diff --git a/README.md b/README.md index a090bec7..1ddc651b 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,5 @@ TODO: - Entity names that changed? - llama inventory slots -- Rewrite Shulker box name \ No newline at end of file +- Rewrite Shulker box name +- Play Guardian effect for totems? \ No newline at end of file diff --git a/core/src/main/java/nl/matsv/viabackwards/ViaBackwards.java b/core/src/main/java/nl/matsv/viabackwards/ViaBackwards.java index ff454619..c7eec7f8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/ViaBackwards.java +++ b/core/src/main/java/nl/matsv/viabackwards/ViaBackwards.java @@ -23,4 +23,5 @@ public class ViaBackwards { ViaBackwards.platform = platform; } + } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundIdRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundIdRewriter.java deleted file mode 100644 index d48e5b70..00000000 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundIdRewriter.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2016 Matsv - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -package nl.matsv.viabackwards.api.rewriters; - -import nl.matsv.viabackwards.api.BackwardsProtocol; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public abstract class SoundIdRewriter extends Rewriter { - private Map soundRewriter = new ConcurrentHashMap<>(); - private Map pitchRewriter = new ConcurrentHashMap<>(); - - protected void rewriteSound(int newId, int oldId) { - soundRewriter.put(newId, oldId); - } - - protected void rewriteSound(int newId, int oldId, float newPitch) { - rewriteSound(newId, oldId); - pitchRewriter.put(newId, newPitch); - } - - public int handleSounds(int soundId) { - int newSoundId = soundId; - if (soundRewriter.containsKey(soundId)) - newSoundId = soundId = soundRewriter.get(soundId); - for (Integer i : soundRewriter.keySet()) { - if (soundId > i) - newSoundId--; - } - return newSoundId; - } - - public boolean hasPitch(int soundId) { - return pitchRewriter.containsKey(soundId); - } - - public float handlePitch(int soundId) { - if (pitchRewriter.containsKey(soundId)) - return pitchRewriter.get(soundId); - return -1; - } -} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java new file mode 100644 index 00000000..75749748 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2016 Matsv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package nl.matsv.viabackwards.api.rewriters; + +import lombok.AllArgsConstructor; +import lombok.Data; +import nl.matsv.viabackwards.api.BackwardsProtocol; + +import java.util.HashMap; +import java.util.Map; + +public abstract class SoundRewriter extends Rewriter { + private Map soundRewrites = new HashMap<>(); + + public SoundData added(int id, int replacement) { + return added(id, replacement, -1); + } + + public SoundData added(int id, int replacement, float newPitch) { + SoundData data = new SoundData(replacement, true, newPitch, true); + soundRewrites.put(id, data); + return data; + } + + public SoundData removed(int id) { + SoundData data = new SoundData(-1, false, -1, false); + soundRewrites.put(id, data); + return data; + } + + public int handleSounds(int soundId) { + int newSoundId = soundId; + if (soundRewrites.containsKey(soundId)) + return soundRewrites.get(soundId).getReplacementSound(); + for (Map.Entry entry : soundRewrites.entrySet()) { + if (soundId > entry.getKey()) { + if (entry.getValue().isAdded()) { + newSoundId--; + } else { + newSoundId++; + } + } + } + return newSoundId; + } + + public boolean hasPitch(int soundId) { + if (soundRewrites.containsKey(soundId)) + return soundRewrites.get(soundId).isChangePitch(); + return false; + } + + public float handlePitch(int soundId) { + if (soundRewrites.containsKey(soundId)) + return soundRewrites.get(soundId).getNewPitch(); + return 1f; + } + + @Data + @AllArgsConstructor + public class SoundData { + private int replacementSound; + private boolean changePitch = false; + private float newPitch = 1f; + private boolean added; + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java index ca0ce077..7f03477f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java @@ -16,6 +16,7 @@ import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets.BlockItemPackets; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets.EntityPackets; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets.PlayerPackets; +import nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets.SoundPackets; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; @@ -28,6 +29,7 @@ public class Protocol1_10To1_11 extends BackwardsProtocol { (entityPackets = new EntityPackets()).register(this); new PlayerPackets().register(this); new BlockItemPackets().register(this); + new SoundPackets().register(this); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets.java new file mode 100644 index 00000000..b36fd05f --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/SoundPackets.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016 Matsv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets; + +import nl.matsv.viabackwards.api.rewriters.SoundRewriter; +import nl.matsv.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; + +public class SoundPackets extends SoundRewriter { + @Override + protected void registerPackets(Protocol1_10To1_11 protocol) { + // Named sound effect + protocol.registerOutgoing(State.PLAY, 0x19, 0x19, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); // 0 - Sound name + map(Type.VAR_INT); // 1 - Sound Category + map(Type.INT); // 2 - x + map(Type.INT); // 3 - y + map(Type.INT); // 4 - z + map(Type.FLOAT); // 5 - Volume + map(Type.FLOAT); // 6 - Pitch + } + }); + + // Sound effect + protocol.registerOutgoing(State.PLAY, 0x46, 0x46, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Sound name + map(Type.VAR_INT); // 1 - Sound Category + map(Type.INT); // 2 - x + map(Type.INT); // 3 - y + map(Type.INT); // 4 - z + map(Type.FLOAT); // 5 - Volume + map(Type.FLOAT); // 6 - Pitch + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int oldId = wrapper.get(Type.VAR_INT, 0); + int newId = handleSounds(oldId); + if (newId == -1) + wrapper.cancel(); + else { + if (hasPitch(oldId)) + wrapper.set(Type.FLOAT, 0, handlePitch(oldId)); + wrapper.set(Type.VAR_INT, 0, newId); + } + } + }); + } + }); + } + + @Override + protected void registerRewrites() { + // TODO find good replacement sounds + // Automatically generated from PAaaS + added(85, -1); // block.shulker_box.close + added(86, -1); // block.shulker_box.open + + added(176, -1); // entity.elder_guardian.flop + + removed(196); // entity.experience_orb.touch + + added(197, -1); // entity.evocation_fangs.attack + added(198, -1); // entity.evocation_illager.ambient + added(199, -1); // entity.evocation_illager.cast_spell + added(200, -1); // entity.evocation_illager.death + added(201, -1); // entity.evocation_illager.hurt + added(202, -1); // entity.evocation_illager.prepare_attack + added(203, -1); // entity.evocation_illager.prepare_summon + added(204, -1); // entity.evocation_illager.prepare_wololo + + added(279, -1); // entity.llama.ambient + added(280, -1); // entity.llama.angry + added(281, -1); // entity.llama.chest + added(282, -1); // entity.llama.death + added(283, -1); // entity.llama.eat + added(284, -1); // entity.llama.hurt + added(285, -1); // entity.llama.spit + added(286, -1); // entity.llama.step + added(287, -1); // entity.llama.swag + added(296, -1); // entity.mule.chest + + added(390, -1); // entity.vex.ambient + added(391, -1); // entity.vex.charge + added(392, -1); // entity.vex.death + added(393, -1); // entity.vex.hurt + + added(400, -1); // entity.vindication_illager.ambient + added(401, -1); // entity.vindication_illager.death + added(402, -1); // entity.vindication_illager.hurt + + added(450, -1); // item.armor.equip_elytra + added(455, -1); // item.bottle.empty + added(470, -1); // item.totem.use + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets.java index 726cc698..714f55a8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/SoundPackets.java @@ -10,7 +10,7 @@ package nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.packets; -import nl.matsv.viabackwards.api.rewriters.SoundIdRewriter; +import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.Protocol1_9_4To1_10; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -19,7 +19,7 @@ import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; -public class SoundPackets extends SoundIdRewriter { +public class SoundPackets extends SoundRewriter { protected static ValueTransformer toOldPitch = new ValueTransformer(Type.UNSIGNED_BYTE) { public Short transform(PacketWrapper packetWrapper, Float inputValue) throws Exception { return (short) Math.round(inputValue * 63.5F); @@ -74,32 +74,33 @@ public class SoundPackets extends SoundIdRewriter { @Override protected void registerRewrites() { - rewriteSound(24, -1); // Enchantment table sound - + added(24, -1); // Enchantment table sound + // Husk - rewriteSound(249, 400); // Husk -> Zombie ambient - rewriteSound(250, 404); // Husk -> Zombie death - rewriteSound(251, 405); // Husk -> Zombie hurt - rewriteSound(252, 407); // Husk -> Zombie step + added(249, 381); // Husk -> Zombie ambient + added(250, 385); // Husk -> Zombie death + added(251, 386); // Husk -> Zombie hurt + added(252, 388); // Husk -> Zombie step // Polar bear - rewriteSound(301, 400, .6F); // Polar bear ambient - rewriteSound(302, 400, 1.9F); // Polar baby bear ambient - rewriteSound(303, 404, .7F); // Polar bear death - rewriteSound(304, 320, .6F); // Polar bear hurt - rewriteSound(305, 241, .6F); // Polar bear step - rewriteSound(306, 393, 1.2F); // Polar bear warning + added(301, 381, .6F); // Polar bear ambient -> Zombie ambient + added(302, 381, 1.9F); // Polar baby bear ambient -> Zombie ambient + added(303, 385, .7F); // Polar bear death -> Zombie death + added(304, 309, .6F); // Polar bear hurt -> Shulker hurt + added(305, 240, .6F); // Polar bear step -> Horse step + added(306, 374, 1.2F); // Polar bear warning -> Wolf growl // Stray - rewriteSound(365, 331); // Stray -> Skeleton ambient - rewriteSound(366, 332); // Stray -> Skeleton death - rewriteSound(367, 333); // Stray -> Skeleton hurt - rewriteSound(368, 335); // Stray -> Skeleton step + added(365, 320); // Stray -> Skeleton ambient + added(366, 321); // Stray -> Skeleton death + added(367, 322); // Stray -> Skeleton hurt + added(368, 324); // Stray -> Skeleton step // Wither skeleton - rewriteSound(387, 331); // Wither skeleton -> Skeleton ambient - rewriteSound(388, 332); // Wither skeleton -> Skeleton death - rewriteSound(389, 333); // Wither skeleton -> Skeleton hurt - rewriteSound(390, 335); // Wither skeleton -> Skeleton step + added(387, 320); // Wither skeleton -> Skeleton ambient + added(388, 321); // Wither skeleton -> Skeleton death + added(389, 322); // Wither skeleton -> Skeleton hurt + added(390, 324); // Wither skeleton -> Skeleton step } + }