Minor cleanups

This commit is contained in:
Camotoy 2022-01-19 19:30:54 -05:00
parent 001a1a7a15
commit a6004af083
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 12 additions and 28 deletions

View file

@ -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");

View file

@ -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

View file

@ -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
*