Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/GeyserMC/Geyser into serv…
Browse files Browse the repository at this point in the history
…er-inventory
  • Loading branch information
Camotoy committed Feb 16, 2021
2 parents aa47e75 + e0bd5a6 commit 72186d9
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void checkLoopback(GeyserStandaloneLogger geyserLogger) {

if (!result.contains("minecraftuwp")) {
Files.write(Paths.get(System.getenv("temp") + "/loopback_minecraft.bat"), loopbackCommand.getBytes(), new OpenOption[0]);
process = Runtime.getRuntime().exec(startScript);
Runtime.getRuntime().exec(startScript);

geyserLogger.info(ChatColor.AQUA + LanguageUtils.getLocaleStringLog("geyser.bootstrap.loopback.added"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void displayMessage() {
* @return The formatted message
*/
private String createMessage() {
String message = "";
StringBuilder message = new StringBuilder();

InputStream helpStream = IGeyserMain.class.getClassLoader().getResourceAsStream("languages/run-help/" + Locale.getDefault().toString() + ".txt");

Expand All @@ -68,10 +68,10 @@ private String createMessage() {
line = line.replace("${plugin_type}", this.getPluginType());
line = line.replace("${plugin_folder}", this.getPluginFolder());

message += line + "\n";
message.append(line).append("\n");
}

return message;
return message.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s

if (entityMetadata.getId() >= 15 && entityMetadata.getId() <= 17) {
Entity entity = session.getEntityCache().getEntityByJavaId((int) entityMetadata.getValue());
if (entity == null && session.getPlayerEntity().getEntityId() == (Integer) entityMetadata.getValue()) {
if (entity == null && session.getPlayerEntity().getEntityId() == (int) entityMetadata.getValue()) {
entity = session.getPlayerEntity();
}

Expand All @@ -62,7 +62,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
} else if (entityMetadata.getId() == 17) {
metadata.put(EntityData.WITHER_TARGET_3, targetID);
} else if (entityMetadata.getId() == 18) {
metadata.put(EntityData.WITHER_INVULNERABLE_TICKS, (int) entityMetadata.getValue());
metadata.put(EntityData.WITHER_INVULNERABLE_TICKS, entityMetadata.getValue());

// Show the shield for the first few seconds of spawning (like Java)
if ((int) entityMetadata.getValue() >= 165) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
public class EntityIdentifierRegistry {

public static NbtMap ENTITY_IDENTIFIERS;
public static final NbtMap ENTITY_IDENTIFIERS;

private EntityIdentifierRegistry() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ public CompoundTag translateToJavaNBT(String name, NbtMap tag) {
CompoundTag javaTag = new CompoundTag(name);
Map<String, Tag> javaValue = javaTag.getValue();
if (tag != null && !tag.isEmpty()) {
for (String str : tag.keySet()) {
Object bedrockTag = tag.get(str);
Tag translatedTag = translateToJavaNBT(str, bedrockTag);
for (Map.Entry<String, Object> entry : tag.entrySet()) {
Tag translatedTag = translateToJavaNBT(entry.getKey(), entry.getValue());
if (translatedTag == null)
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void translate(ServerUnloadChunkPacket packet, GeyserSession session) {
Iterator<Vector3i> iterator = session.getSkullCache().keySet().iterator();
while (iterator.hasNext()) {
Vector3i position = iterator.next();
if (Math.floor((double) position.getX() / 16) == packet.getX() && Math.floor((double) position.getZ() / 16) == packet.getZ()) {
if ((position.getX() >> 4) == packet.getX() && (position.getZ() >> 4) == packet.getZ()) {
session.getSkullCache().get(position).despawnEntity(session);
iterator.remove();
}
Expand Down
39 changes: 20 additions & 19 deletions connector/src/main/java/org/geysermc/connector/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,22 @@ public static File fileOrCopiedFromResource(String name, Function<String, String
*/
public static File fileOrCopiedFromResource(File file, String name, Function<String, String> format) throws IOException {
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
InputStream input = GeyserConnector.class.getResourceAsStream("/" + name); // resources need leading "/" prefix
try (FileOutputStream fos = new FileOutputStream(file)) {
try (InputStream input = GeyserConnector.class.getResourceAsStream("/" + name)) { // resources need leading "/" prefix
byte[] bytes = new byte[input.available()];

byte[] bytes = new byte[input.available()];
//noinspection ResultOfMethodCallIgnored
input.read(bytes);

input.read(bytes);
for(char c : format.apply(new String(bytes)).toCharArray()) {
fos.write(c);
}

for(char c : format.apply(new String(bytes)).toCharArray()) {
fos.write(c);
fos.flush();
}
}

fos.flush();
input.close();
fos.close();
}

return file;
Expand All @@ -122,14 +123,13 @@ public static void writeFile(File file, char[] data) throws IOException {
file.createNewFile();
}

FileOutputStream fos = new FileOutputStream(file);
try (FileOutputStream fos = new FileOutputStream(file)) {
for (char c : data) {
fos.write(c);
}

for (char c : data) {
fos.write(c);
fos.flush();
}

fos.flush();
fos.close();
}

/**
Expand Down Expand Up @@ -232,9 +232,10 @@ public static byte[] readAllBytes(InputStream stream) {
try {
int size = stream.available();
byte[] bytes = new byte[size];
BufferedInputStream buf = new BufferedInputStream(stream);
buf.read(bytes, 0, bytes.length);
buf.close();
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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static void loadGeyserLocale(String locale) {
// Load the locale
if (localeStream != null) {
Properties localeProp = new Properties();
try {
localeProp.load(new InputStreamReader(localeStream, StandardCharsets.UTF_8));
try (InputStreamReader reader = new InputStreamReader(localeStream, StandardCharsets.UTF_8)) {
localeProp.load(reader);
} catch (Exception e) {
throw new AssertionError(getLocaleStringLog("geyser.language.load_failed", locale), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,22 @@ private static void downloadEN_US(File localeFile) {
WebUtils.downloadFile(clientJarInfo.getUrl(), tmpFilePath.toString());

// Load in the JAR as a zip and extract the file
ZipFile localeJar = new ZipFile(tmpFilePath.toString());
InputStream fileStream = localeJar.getInputStream(localeJar.getEntry("assets/minecraft/lang/en_us.json"));
FileOutputStream outStream = new FileOutputStream(localeFile);

// Write the file to the locale dir
byte[] buf = new byte[fileStream.available()];
int length;
while ((length = fileStream.read(buf)) != -1) {
outStream.write(buf, 0, length);
}

// Flush all changes to disk and cleanup
outStream.flush();
outStream.close();
try (ZipFile localeJar = new ZipFile(tmpFilePath.toString())) {
try (InputStream fileStream = localeJar.getInputStream(localeJar.getEntry("assets/minecraft/lang/en_us.json"))) {
try (FileOutputStream outStream = new FileOutputStream(localeFile)) {

// Write the file to the locale dir
byte[] buf = new byte[fileStream.available()];
int length;
while ((length = fileStream.read(buf)) != -1) {
outStream.write(buf, 0, length);
}

fileStream.close();
localeJar.close();
// Flush all changes to disk and cleanup
outStream.flush();
}
}
}

// Store the latest jar hash
FileUtils.writeFile(GeyserConnector.getInstance().getBootstrap().getConfigFolder().resolve("locales/en_us.hash").toString(), clientJarInfo.getSha1().toCharArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
Expand Down Expand Up @@ -70,10 +72,12 @@ public static void loadPacks() {

pack.sha256 = FileUtils.calculateSHA256(file);

Stream<? extends ZipEntry> stream = null;
try {
ZipFile zip = new ZipFile(file);

zip.stream().forEach((x) -> {
stream = zip.stream();
stream.forEach((x) -> {
if (x.getName().contains("manifest.json")) {
try {
ResourcePackManifest manifest = FileUtils.loadJson(zip.getInputStream(x), ResourcePackManifest.class);
Expand All @@ -94,6 +98,10 @@ public static void loadPacks() {
} catch (Exception e) {
GeyserConnector.getInstance().getLogger().error(LanguageUtils.getLocaleStringLog("geyser.resource_pack.broken", file.getName()));
e.printStackTrace();
} finally {
if (stream != null) {
stream.close();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static boolean handleSettingsForm(GeyserSession session, String response)
}

if (Boolean.class.equals(gamerule.getType())) {
Boolean value = settingsResponse.getToggleResponses().get(offset).booleanValue();
boolean value = settingsResponse.getToggleResponses().get(offset);
if (value != session.getConnector().getWorldManager().getGameRuleBool(session, gamerule)) {
session.getConnector().getWorldManager().setGameRule(session, gamerule.getJavaID(), value);
}
Expand Down
20 changes: 10 additions & 10 deletions connector/src/main/java/org/geysermc/connector/utils/WebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,25 @@ public static String post(String reqURL, String postContent) throws IOException
*/
private static String connectionToString(HttpURLConnection con) throws IOException {
// Send the request (we dont use this but its required for getErrorStream() to work)
int code = con.getResponseCode();
con.getResponseCode();

// Read the error message if there is one if not just read normally
InputStream inputStream = con.getErrorStream();
if (inputStream == null) {
inputStream = con.getInputStream();
}

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
StringBuffer content = new StringBuffer();
StringBuilder content = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))) {
String inputLine;

while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
content.append("\n");
}
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
content.append("\n");
}

in.close();
con.disconnect();
con.disconnect();
}

return content.toString();
}
Expand Down

0 comments on commit 72186d9

Please sign in to comment.