From a6004af083394e6b1c5be9ef9e7c8986ae42d8f4 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Wed, 19 Jan 2022 19:30:54 -0500 Subject: [PATCH] Minor cleanups --- .../BedrockBlockEntityDataTranslator.java | 12 ++++++---- .../entity/JavaEntityEventTranslator.java | 4 ++-- .../org/geysermc/geyser/util/FileUtils.java | 24 +++---------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockEntityDataTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockEntityDataTranslator.java index 93ce71a3d..d00914fb1 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockEntityDataTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockEntityDataTranslator.java @@ -41,12 +41,14 @@ public class BedrockBlockEntityDataTranslator extends PacketTranslator<BlockEnti @Override public void translate(GeyserSession session, BlockEntityDataPacket packet) { NbtMap tag = packet.getData(); - if (tag.getString("id").equals("Sign")) { + String id = tag.getString("id"); + if (id.equals("Sign")) { + String text = tag.getString("Text"); // This is the reason why this all works - Bedrock sends packets every time you update the sign, Java only wants the final packet // But Bedrock sends one final packet when you're done editing the sign, which should be equal to the last message since there's no edits // So if the latest update does not match the last cached update then it's still being edited - if (!tag.getString("Text").equals(session.getLastSignMessage())) { - session.setLastSignMessage(tag.getString("Text")); + if (!text.equals(session.getLastSignMessage())) { + session.setLastSignMessage(text); return; } // Otherwise the two messages are identical and we can get to work deconstructing @@ -59,7 +61,7 @@ public class BedrockBlockEntityDataTranslator extends PacketTranslator<BlockEnti // If it goes over the maximum, we need to start a new line to match Java int widthCount = 0; // This converts the message into the array'd message Java wants - for (char character : tag.getString("Text").toCharArray()) { + for (char character : text.toCharArray()) { widthCount += SignUtils.getCharacterWidth(character); // If we get a return in Bedrock, or go over the character width max, that signals to use the next line. if (character == '\n' || widthCount > SignUtils.JAVA_CHARACTER_WIDTH_MAX) { @@ -111,7 +113,7 @@ public class BedrockBlockEntityDataTranslator extends PacketTranslator<BlockEnti // We set the sign text cached in the session to null to indicate there is no work-in-progress sign session.setLastSignMessage(null); - } else if (tag.getString("id").equals("JigsawBlock")) { + } else if (id.equals("JigsawBlock")) { // Client has just sent a jigsaw block update Position pos = new Position(tag.getInt("x"), tag.getInt("y"), tag.getInt("z")); String name = tag.getString("name"); diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaEntityEventTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaEntityEventTranslator.java index f34f7bd17..de4a2c22b 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaEntityEventTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaEntityEventTranslator.java @@ -245,8 +245,8 @@ public class JavaEntityEventTranslator extends PacketTranslator<ClientboundEntit float baseX = position.getX(); float baseY = position.getY(); float baseZ = position.getZ(); - float height = entity.getDefinition().height(); - float width = entity.getDefinition().width(); + float height = entity.getBoundingBoxHeight(); + float width = entity.getBoundingBoxWidth(); Random random = ThreadLocalRandom.current(); for (int i = 0; i < 20; i++) { // Reconstruct the Java Edition (1.18.1) logic, but in floats diff --git a/core/src/main/java/org/geysermc/geyser/util/FileUtils.java b/core/src/main/java/org/geysermc/geyser/util/FileUtils.java index a56386477..6df9c2177 100644 --- a/core/src/main/java/org/geysermc/geyser/util/FileUtils.java +++ b/core/src/main/java/org/geysermc/geyser/util/FileUtils.java @@ -169,8 +169,8 @@ public class FileUtils { * @return The byte array of the file */ public static byte[] readAllBytes(File file) { - try (InputStream inputStream = new FileInputStream(file)) { - return readAllBytes(inputStream); + try (InputStream stream = new FileInputStream(file)) { + return stream.readAllBytes(); } catch (IOException e) { throw new RuntimeException("Cannot read " + file); } @@ -182,30 +182,12 @@ public class FileUtils { */ public static byte[] readAllBytes(String resource) { try (InputStream stream = GeyserImpl.getInstance().getBootstrap().getResource(resource)) { - return readAllBytes(stream); + return stream.readAllBytes(); } catch (IOException e) { throw new RuntimeException("Error while trying to read internal input stream!", e); } } - /** - * @param stream the InputStream to read off of - * @return the byte array of an InputStream - */ - public static byte[] readAllBytes(InputStream stream) { - try { - int size = stream.available(); - byte[] bytes = new byte[size]; - try (BufferedInputStream buf = new BufferedInputStream(stream)) { - //noinspection ResultOfMethodCallIgnored - buf.read(bytes, 0, bytes.length); - } - return bytes; - } catch (IOException e) { - throw new RuntimeException("Error while trying to read input stream!", e); - } - } - /** * Read the lines of a file and return it as a stream *