From 2ea50116cf9d3f5bc22c00f9cc86a145f70da448 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Fri, 24 Jun 2022 23:13:35 -0400 Subject: [PATCH] Forward cooldowns for shields and goats These are server-controlled as of 1.19. Ender pearls and chorus fruit still appear to be client-controlled. --- .../inventory/item/StoredItemMappings.java | 2 + .../java/level/JavaCooldownTranslator.java | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaCooldownTranslator.java diff --git a/core/src/main/java/org/geysermc/geyser/inventory/item/StoredItemMappings.java b/core/src/main/java/org/geysermc/geyser/inventory/item/StoredItemMappings.java index c4375efba..8f9eb415f 100644 --- a/core/src/main/java/org/geysermc/geyser/inventory/item/StoredItemMappings.java +++ b/core/src/main/java/org/geysermc/geyser/inventory/item/StoredItemMappings.java @@ -50,6 +50,7 @@ public class StoredItemMappings { private final ItemMapping fishingRod; private final int flintAndSteel; private final int frogspawn; + private final int goatHorn; private final int glassBottle; private final int goldenApple; private final int goldIngot; @@ -82,6 +83,7 @@ public class StoredItemMappings { this.fishingRod = load(itemMappings, "fishing_rod"); this.flintAndSteel = load(itemMappings, "flint_and_steel").getJavaId(); this.frogspawn = load(itemMappings, "frogspawn").getBedrockId(); + this.goatHorn = load(itemMappings, "goat_horn").getJavaId(); this.glassBottle = load(itemMappings, "glass_bottle").getBedrockId(); this.goldenApple = load(itemMappings, "golden_apple").getJavaId(); this.goldIngot = load(itemMappings, "gold_ingot").getJavaId(); diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaCooldownTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaCooldownTranslator.java new file mode 100644 index 000000000..f222682ae --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaCooldownTranslator.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * 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. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.translator.protocol.java.level; + +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundCooldownPacket; +import com.nukkitx.protocol.bedrock.packet.PlayerStartItemCooldownPacket; +import org.geysermc.geyser.inventory.item.StoredItemMappings; +import org.geysermc.geyser.session.GeyserSession; +import org.geysermc.geyser.translator.protocol.PacketTranslator; +import org.geysermc.geyser.translator.protocol.Translator; + +@Translator(packet = ClientboundCooldownPacket.class) +public class JavaCooldownTranslator extends PacketTranslator { + + @Override + public void translate(GeyserSession session, ClientboundCooldownPacket packet) { + StoredItemMappings itemMappings = session.getItemMappings().getStoredItems(); + + int itemId = packet.getItemId(); + // Not every item, as of 1.19, appears to be server-driven. Just these two. + // Use a map here if it gets too big. + String cooldownCategory; + if (itemId == itemMappings.goatHorn()) { + cooldownCategory = "goat_horn"; + } else if (itemId == itemMappings.shield().getJavaId()) { + cooldownCategory = "shield"; + } else { + cooldownCategory = null; + } + + if (cooldownCategory != null) { + PlayerStartItemCooldownPacket bedrockPacket = new PlayerStartItemCooldownPacket(); + bedrockPacket.setItemCategory(cooldownCategory); + bedrockPacket.setCooldownDuration(packet.getCooldownTicks()); + session.sendUpstreamPacket(bedrockPacket); + } + } +}