From d5dc9a4d03f1f49961ede34258feddffd164d9f0 Mon Sep 17 00:00:00 2001 From: belohnung Date: Sun, 20 Jun 2021 16:23:11 +0200 Subject: [PATCH 1/7] Add setters to Vector3, Vector3f, BlockVector3 and updated Documentation --- .../java/cn/nukkit/math/BlockVector3.java | 12 ++++ src/main/java/cn/nukkit/math/Vector3.java | 40 +++++++++++++ src/main/java/cn/nukkit/math/Vector3f.java | 57 ++++++++++++++++++- 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/nukkit/math/BlockVector3.java b/src/main/java/cn/nukkit/math/BlockVector3.java index 77a58320f46..dc706415c00 100644 --- a/src/main/java/cn/nukkit/math/BlockVector3.java +++ b/src/main/java/cn/nukkit/math/BlockVector3.java @@ -33,6 +33,18 @@ public int getZ() { return this.z; } + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } + + public void setZ(int z) { + this.z = z; + } + public Vector3 add(double x) { return this.add(x, 0, 0); } diff --git a/src/main/java/cn/nukkit/math/Vector3.java b/src/main/java/cn/nukkit/math/Vector3.java index 1b551e3e232..be9ed2bf76d 100644 --- a/src/main/java/cn/nukkit/math/Vector3.java +++ b/src/main/java/cn/nukkit/math/Vector3.java @@ -40,6 +40,19 @@ public double getZ() { return this.z; } + public void setX(double x) { + this.x = x; + } + + public void setY(double y) { + this.y = y; + } + + public void setZ(double z) { + this.z = z; + } + + public int getFloorX() { return (int) Math.floor(this.x); } @@ -224,6 +237,11 @@ public double maxPlainDistance(Vector3 x) { return this.maxPlainDistance(x.x, x.z); } + /** + * Calculates the Length of this Vector + * + * @return The Length of this Vector. + */ public double length() { return Math.sqrt(this.lengthSquared()); } @@ -240,10 +258,22 @@ public Vector3 normalize() { return new Vector3(0, 0, 0); } + /** + * Scalar Product of this Vector and the Vector supplied. + * + * @param v Vector to calculate the scalar product to. + * @return Scalar Product + */ public double dot(Vector3 v) { return this.x * v.x + this.y * v.y + this.z * v.z; } + /** + * Calculates the cross product of this Vector and the given Vector + * + * @param v the vector to calculate the cross product with. + * @return a Vector at right angle to this and other + */ public Vector3 cross(Vector3 v) { return new Vector3( this.y * v.z - this.z * v.y, @@ -252,6 +282,16 @@ public Vector3 cross(Vector3 v) { ); } + /** + * Calculates the angle between this and the supplied Vector. + * + * @param v the Vector to calculate the angle to. + * @return the Angle between the two Vectors. + */ + public Angle angleBetween(Vector3 v) { + return Angle.fromRadian(Math.acos(Math.min(Math.max(this.normalize().dot(v.normalize()), -1.0d), 1.0d))); + } + /** * Returns a new vector with x value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. diff --git a/src/main/java/cn/nukkit/math/Vector3f.java b/src/main/java/cn/nukkit/math/Vector3f.java index 106f0044ed2..68779ed6429 100644 --- a/src/main/java/cn/nukkit/math/Vector3f.java +++ b/src/main/java/cn/nukkit/math/Vector3f.java @@ -42,6 +42,18 @@ public float getZ() { return this.z; } + public void setX(float x) { + this.x = x; + } + + public void setY(float y) { + this.y = y; + } + + public void setZ(float z) { + this.z = z; + } + public int getFloorX() { return NukkitMath.floorFloat(this.x); } @@ -204,6 +216,11 @@ public float maxPlainDistance(Vector3f x) { return this.maxPlainDistance(x.x, x.z); } + /** + * Calculates the Length of this Vector + * + * @return The Length of this Vector. + */ public double length() { return Math.sqrt(this.lengthSquared()); } @@ -220,10 +237,22 @@ public Vector3f normalize() { return new Vector3f(0, 0, 0); } + /** + * Scalar Product of this Vector and the Vector supplied. + * + * @param v Vector to calculate the scalar product to. + * @return Scalar Product + */ public float dot(Vector3f v) { return this.x * v.x + this.y * v.y + this.z * v.z; } + /** + * Calculates the cross product of this Vector and the given Vector + * + * @param v the vector to calculate the cross product with. + * @return a Vector at right angle to this and other + */ public Vector3f cross(Vector3f v) { return new Vector3f( this.y * v.z - this.z * v.y, @@ -232,9 +261,23 @@ public Vector3f cross(Vector3f v) { ); } - /* + /** + * Calculates the angle between this and the supplied Vector. + * + * @param v the Vector to calculate the angle to. + * @return the Angle between the two Vectors. + */ + public Angle angleBetween(Vector3f v) { + return Angle.fromRadian(Math.acos(Math.min(Math.max(this.normalize().dot(v.normalize()), -1.0f), 1.0f))); + } + + /** * Returns a new vector with x value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. + * + * @param v vector + * @param x x value + * @return intermediate vector */ public Vector3f getIntermediateWithXValue(Vector3f v, float x) { float xDiff = v.x - this.x; @@ -251,9 +294,13 @@ public Vector3f getIntermediateWithXValue(Vector3f v, float x) { } } - /* + /** * Returns a new vector with y value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. + * + * @param v vector + * @param y y value + * @return intermediate vector */ public Vector3f getIntermediateWithYValue(Vector3f v, float y) { float xDiff = v.x - this.x; @@ -270,9 +317,13 @@ public Vector3f getIntermediateWithYValue(Vector3f v, float y) { } } - /* + /** * Returns a new vector with z value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. + * + * @param v vector + * @param z z value + * @return intermediate vector */ public Vector3f getIntermediateWithZValue(Vector3f v, float z) { float xDiff = v.x - this.x; From 3db414b083a31bc7539b5dcb422d0f5938e36adf Mon Sep 17 00:00:00 2001 From: belohnung Date: Sun, 20 Jun 2021 16:44:40 +0200 Subject: [PATCH 2/7] Actually return the changed Vector --- src/main/java/cn/nukkit/math/BlockVector3.java | 9 ++++++--- src/main/java/cn/nukkit/math/Vector3.java | 9 ++++++--- src/main/java/cn/nukkit/math/Vector3f.java | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/cn/nukkit/math/BlockVector3.java b/src/main/java/cn/nukkit/math/BlockVector3.java index dc706415c00..696419bcf8f 100644 --- a/src/main/java/cn/nukkit/math/BlockVector3.java +++ b/src/main/java/cn/nukkit/math/BlockVector3.java @@ -33,16 +33,19 @@ public int getZ() { return this.z; } - public void setX(int x) { + public BlockVector3 setX(int x) { this.x = x; + return this; } - public void setY(int y) { + public BlockVector3 setY(int y) { this.y = y; + return this; } - public void setZ(int z) { + public BlockVector3 setZ(int z) { this.z = z; + return this; } public Vector3 add(double x) { diff --git a/src/main/java/cn/nukkit/math/Vector3.java b/src/main/java/cn/nukkit/math/Vector3.java index be9ed2bf76d..7c6eb3978e7 100644 --- a/src/main/java/cn/nukkit/math/Vector3.java +++ b/src/main/java/cn/nukkit/math/Vector3.java @@ -40,16 +40,19 @@ public double getZ() { return this.z; } - public void setX(double x) { + public Vector3 setX(double x) { this.x = x; + return this; } - public void setY(double y) { + public Vector3 setY(double y) { this.y = y; + return this; } - public void setZ(double z) { + public Vector3 setZ(double z) { this.z = z; + return this; } diff --git a/src/main/java/cn/nukkit/math/Vector3f.java b/src/main/java/cn/nukkit/math/Vector3f.java index 68779ed6429..ce466ce673a 100644 --- a/src/main/java/cn/nukkit/math/Vector3f.java +++ b/src/main/java/cn/nukkit/math/Vector3f.java @@ -42,16 +42,19 @@ public float getZ() { return this.z; } - public void setX(float x) { + public Vector3f setX(float x) { this.x = x; + return this; } - public void setY(float y) { + public Vector3f setY(float y) { this.y = y; + return this; } - public void setZ(float z) { + public Vector3f setZ(float z) { this.z = z; + return this; } public int getFloorX() { From 50d70b0575ae7872d77b76366f679e220eb6b3ac Mon Sep 17 00:00:00 2001 From: DasEtwas <18222134+DasEtwas@users.noreply.github.com> Date: Wed, 4 Aug 2021 16:54:47 +0200 Subject: [PATCH 3/7] Initial implementation of client-side behaviour packs * Duplicate loading logic for behaviour packs from new folder "client_behaviour_packs" * Add pack type detection using its modules' types * Add scripting = true if a behaviour pack contains .js files --- src/main/java/cn/nukkit/Player.java | 9 +- src/main/java/cn/nukkit/Server.java | 2 +- .../protocol/ResourcePacksInfoPacket.java | 2 +- .../resourcepacks/AbstractResourcePack.java | 33 +++++++ .../cn/nukkit/resourcepacks/ResourcePack.java | 25 +++++- .../resourcepacks/ResourcePackManager.java | 88 ++++++++++++++++--- .../resourcepacks/ZippedResourcePack.java | 17 ++++ 7 files changed, 159 insertions(+), 17 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 14329e0a148..b4e8b24c453 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -64,6 +64,7 @@ import cn.nukkit.plugin.Plugin; import cn.nukkit.potion.Effect; import cn.nukkit.resourcepacks.ResourcePack; +import cn.nukkit.resourcepacks.ResourcePackManager; import cn.nukkit.scheduler.AsyncTask; import cn.nukkit.utils.*; import co.aikar.timings.Timing; @@ -1950,8 +1951,11 @@ protected void processLogin() { } this.timeSinceRest = this.namedTag.getInt("TimeSinceRest"); + ResourcePackManager resourcePackManager = this.server.getResourcePackManager(); ResourcePacksInfoPacket infoPacket = new ResourcePacksInfoPacket(); - infoPacket.resourcePackEntries = this.server.getResourcePackManager().getResourceStack(); + infoPacket.resourcePackEntries = resourcePackManager.getResourcePacks(); + infoPacket.behaviourPackEntries = resourcePackManager.getBehaviourPacks(); + infoPacket.scripting = Arrays.stream(infoPacket.behaviourPackEntries).anyMatch(ResourcePack::requiresScripting); infoPacket.mustAccept = this.server.getForceResources(); this.dataPacket(infoPacket); } @@ -2210,7 +2214,8 @@ public void onCompletion(Server server) { case ResourcePackClientResponsePacket.STATUS_HAVE_ALL_PACKS: ResourcePackStackPacket stackPacket = new ResourcePackStackPacket(); stackPacket.mustAccept = this.server.getForceResources(); - stackPacket.resourcePackStack = this.server.getResourcePackManager().getResourceStack(); + stackPacket.resourcePackStack = this.server.getResourcePackManager().getResourcePacks(); + stackPacket.behaviourPackStack = this.server.getResourcePackManager().getBehaviourPacks(); this.dataPacket(stackPacket); break; case ResourcePackClientResponsePacket.STATUS_COMPLETED: diff --git a/src/main/java/cn/nukkit/Server.java b/src/main/java/cn/nukkit/Server.java index 39029ddedc2..1a6d61eda26 100644 --- a/src/main/java/cn/nukkit/Server.java +++ b/src/main/java/cn/nukkit/Server.java @@ -491,7 +491,7 @@ public Level remove(Object key) { convertLegacyPlayerData(); this.craftingManager = new CraftingManager(); - this.resourcePackManager = new ResourcePackManager(new File(Nukkit.DATA_PATH, "resource_packs")); + this.resourcePackManager = new ResourcePackManager(new File(Nukkit.DATA_PATH, "resource_packs"),new File(Nukkit.DATA_PATH, "client_behaviour_packs")); this.pluginManager = new PluginManager(this, this.commandMap); this.pluginManager.subscribeToPermission(Server.BROADCAST_CHANNEL_ADMINISTRATIVE, this.consoleSender); diff --git a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java index 38fc3666f33..6647c02b52c 100644 --- a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java @@ -38,7 +38,7 @@ private void encodePacks(ResourcePack[] packs) { this.putString(""); // encryption key this.putString(""); // sub-pack name this.putString(""); // content identity - this.putBoolean(false); // scripting + this.putBoolean(entry.requiresScripting()); // scripting this.putBoolean(false); // raytracing capable } } diff --git a/src/main/java/cn/nukkit/resourcepacks/AbstractResourcePack.java b/src/main/java/cn/nukkit/resourcepacks/AbstractResourcePack.java index 060ecb7d8be..9e6a5f5831a 100644 --- a/src/main/java/cn/nukkit/resourcepacks/AbstractResourcePack.java +++ b/src/main/java/cn/nukkit/resourcepacks/AbstractResourcePack.java @@ -1,13 +1,16 @@ package cn.nukkit.resourcepacks; +import cn.nukkit.Server; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import java.util.Arrays; import java.util.UUID; public abstract class AbstractResourcePack implements ResourcePack { protected JsonObject manifest; private UUID id = null; + private ResourcePack.Type type = null; protected boolean verifyManifest() { if (this.manifest.has("format_version") && this.manifest.has("header") && this.manifest.has("modules")) { @@ -45,4 +48,34 @@ public String getPackVersion() { version.get(1).getAsString(), version.get(2).getAsString()); } + + @Override + public Type getType() { + if (type == null) { + JsonArray modules = this.manifest.getAsJsonArray("modules").getAsJsonArray(); + + for (int i = 0; i < modules.size(); i++) { + JsonObject element = modules.get(i).getAsJsonObject(); + String moduleType = element.get("type").getAsString(); + + ResourcePack.Type type = null; + for (ResourcePack.Type possibleType : ResourcePack.Type.values()) { + if (Arrays.asList(possibleType.getAllowedModuleTypes()).contains(moduleType)) { + type = possibleType; + break; + } + } + + if (type == null) { + throw new IllegalArgumentException(Server.getInstance().getLanguage() + .translateString("nukkit.resources.unsupported-module-type", this.getPackName())); + } + + this.type = type; + break; + } + } + + return type; + } } diff --git a/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java b/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java index 365e8b42482..08b9134800d 100644 --- a/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java +++ b/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java @@ -2,6 +2,9 @@ import java.util.UUID; +/** + * Can either be a "Resource Pack" or a "Behaviour Pack" + */ public interface ResourcePack { String getPackName(); @@ -14,4 +17,24 @@ public interface ResourcePack { byte[] getSha256(); byte[] getPackChunk(int off, int len); -} + + Type getType(); + + boolean requiresScripting(); + + enum Type { + // Module types may not overlap. + RESOURCE_PACK(new String[]{"resources"}), + BEHAVIOUR_PACK(new String[]{"client_data"}); // only client scripts are supported + + private final String[] allowedModuleTypes; + + Type(String[] allowedModuleTypes) { + this.allowedModuleTypes = allowedModuleTypes; + } + + public String[] getAllowedModuleTypes() { + return this.allowedModuleTypes; + } + } +} \ No newline at end of file diff --git a/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java b/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java index 3460ab204a1..5a8d12b7322 100644 --- a/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java +++ b/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java @@ -7,19 +7,28 @@ import java.util.*; public class ResourcePackManager { - private final Map resourcePacksById = new HashMap<>(); - private ResourcePack[] resourcePacks; + private final Map packsById = new HashMap<>(); + private final ResourcePack[] resourcePacks; + private final ResourcePack[] behaviourPacks; - public ResourcePackManager(File path) { - if (!path.exists()) { - path.mkdirs(); - } else if (!path.isDirectory()) { + public ResourcePackManager(File resourcePacksPath, File behaviourPacksPath) { + if (!resourcePacksPath.exists()) { + resourcePacksPath.mkdirs(); + } else if (!resourcePacksPath.isDirectory()) { throw new IllegalArgumentException(Server.getInstance().getLanguage() - .translateString("nukkit.resources.invalid-path", path.getName())); + .translateString("nukkit.resources.invalid-path", resourcePacksPath.getName())); + } + + if (!behaviourPacksPath.exists()) { + behaviourPacksPath.mkdirs(); + } else if (!behaviourPacksPath.isDirectory()) { + throw new IllegalArgumentException(Server.getInstance().getLanguage() + .translateString("nukkit.resources.invalid-path", behaviourPacksPath.getName())); } List loadedResourcePacks = new ArrayList<>(); - for (File pack : path.listFiles()) { + List loadedBehaviourPacks = new ArrayList<>(); + for (File pack : resourcePacksPath.listFiles()) { try { ResourcePack resourcePack = null; @@ -37,8 +46,57 @@ public ResourcePackManager(File path) { } if (resourcePack != null) { + if (!resourcePack.getType().equals(ResourcePack.Type.RESOURCE_PACK)) { + Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() + .translateString("nukkit.resources.invalid-type-resource", pack.getName(), resourcePack.getPackName())); + + continue; + } + loadedResourcePacks.add(resourcePack); - this.resourcePacksById.put(resourcePack.getPackId(), resourcePack); + this.packsById.put(resourcePack.getPackId(), resourcePack); + + Server.getInstance().getLogger().info(Server.getInstance().getLanguage() + .translateString("nukkit.resources.loaded-resource-pack", resourcePack.getPackName())); + } + } catch (IllegalArgumentException e) { + Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() + .translateString("nukkit.resources.fail", pack.getName(), e.getMessage())); + + e.printStackTrace(); + } + } + + for (File pack : behaviourPacksPath.listFiles()) { + try { + ResourcePack behaviourPack = null; + + if (!pack.isDirectory()) { //directory resource packs temporarily unsupported + switch (Files.getFileExtension(pack.getName())) { + case "zip": + case "mcpack": + behaviourPack = new ZippedResourcePack(pack); + break; + default: + Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() + .translateString("nukkit.resources.unknown-format", pack.getName())); + break; + } + } + + if (behaviourPack != null) { + if (!behaviourPack.getType().equals(ResourcePack.Type.BEHAVIOUR_PACK)) { + Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() + .translateString("nukkit.resources.invalid-type-behaviour", pack.getName(), behaviourPack.getPackName())); + + continue; + } + + loadedBehaviourPacks.add(behaviourPack); + this.packsById.put(behaviourPack.getPackId(), behaviourPack); + + Server.getInstance().getLogger().info(Server.getInstance().getLanguage() + .translateString("nukkit.resources.loaded-behaviour-pack", behaviourPack.getPackName())); } } catch (IllegalArgumentException e) { Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() @@ -47,15 +105,21 @@ public ResourcePackManager(File path) { } this.resourcePacks = loadedResourcePacks.toArray(new ResourcePack[0]); + this.behaviourPacks = loadedBehaviourPacks.toArray(new ResourcePack[0]); + Server.getInstance().getLogger().info(Server.getInstance().getLanguage() - .translateString("nukkit.resources.success", String.valueOf(this.resourcePacks.length))); + .translateString("nukkit.resources.success", String.valueOf(this.resourcePacks.length + this.behaviourPacks.length))); } - public ResourcePack[] getResourceStack() { + public ResourcePack[] getResourcePacks() { return this.resourcePacks; } + public ResourcePack[] getBehaviourPacks() { + return this.behaviourPacks; + } + public ResourcePack getPackById(UUID id) { - return this.resourcePacksById.get(id); + return this.packsById.get(id); } } diff --git a/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java b/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java index e9bc3f631d8..672545fd83a 100644 --- a/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java +++ b/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java @@ -10,12 +10,14 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.security.MessageDigest; +import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ZippedResourcePack extends AbstractResourcePack { private File file; private byte[] sha256 = null; + private boolean requiresScripting = false; public ZippedResourcePack(File file) { if (!file.exists()) { @@ -35,6 +37,16 @@ public ZippedResourcePack(File file) { .parse(new InputStreamReader(zip.getInputStream(entry), StandardCharsets.UTF_8)) .getAsJsonObject(); } + + Enumeration entries = zip.entries(); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); + + if (zipEntry.getName().endsWith(".js")) { + // once a JavaScript file is found in the zip, we assume it will be a behaviour script + this.requiresScripting = true; + } + } } catch (IOException e) { Server.getInstance().getLogger().logException(e); } @@ -81,4 +93,9 @@ public byte[] getPackChunk(int off, int len) { return chunk; } + + @Override + public boolean requiresScripting() { + return this.getType().equals(Type.BEHAVIOUR_PACK) ? this.requiresScripting : false; + } } From d6f5d27e9825003336b982f764096b1c4ac587dc Mon Sep 17 00:00:00 2001 From: DasEtwas <18222134+DasEtwas@users.noreply.github.com> Date: Wed, 4 Aug 2021 18:01:48 +0200 Subject: [PATCH 4/7] Fix behaviour pack list encoding --- .../protocol/ResourcePacksInfoPacket.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java index 6647c02b52c..a08c30a2d33 100644 --- a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java @@ -25,11 +25,11 @@ public void encode() { this.putBoolean(this.mustAccept); this.putBoolean(this.scripting); this.putBoolean(this.forceServerPacks); - this.encodePacks(this.behaviourPackEntries); - this.encodePacks(this.resourcePackEntries); + this.encodeBehaviourPacks(this.behaviourPackEntries); + this.encodeResourcePacks(this.resourcePackEntries); } - private void encodePacks(ResourcePack[] packs) { + private void encodeBehaviourPacks(ResourcePack[] packs) { this.putLShort(packs.length); for (ResourcePack entry : packs) { this.putString(entry.getPackId().toString()); @@ -39,6 +39,19 @@ private void encodePacks(ResourcePack[] packs) { this.putString(""); // sub-pack name this.putString(""); // content identity this.putBoolean(entry.requiresScripting()); // scripting + } + } + + private void encodeResourcePacks(ResourcePack[] packs) { + this.putLShort(packs.length); + for (ResourcePack entry : packs) { + this.putString(entry.getPackId().toString()); + this.putString(entry.getPackVersion()); + this.putLLong(entry.getPackSize()); + this.putString(""); // encryption key + this.putString(""); // sub-pack name + this.putString(""); // content identity + this.putBoolean(false); // scripting this.putBoolean(false); // raytracing capable } } From 6e6d9fd8b1c332b3d517dff6ca5e91e2323eac41 Mon Sep 17 00:00:00 2001 From: DasEtwas <18222134+DasEtwas@users.noreply.github.com> Date: Wed, 4 Aug 2021 18:22:01 +0200 Subject: [PATCH 5/7] Fix ResourcePackDataInfoPacket type --- src/main/java/cn/nukkit/Player.java | 8 ++++++++ .../network/protocol/ResourcePackDataInfoPacket.java | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index b4e8b24c453..990d136918a 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -2203,6 +2203,14 @@ public void onCompletion(Server server) { } ResourcePackDataInfoPacket dataInfoPacket = new ResourcePackDataInfoPacket(); + switch (resourcePack.getType()) { + case RESOURCE_PACK: + dataInfoPacket.type = ResourcePackDataInfoPacket.TYPE_RESOURCE; + break; + case BEHAVIOUR_PACK: + dataInfoPacket.type = ResourcePackDataInfoPacket.TYPE_BEHAVIOR; + break; + } dataInfoPacket.packId = resourcePack.getPackId(); dataInfoPacket.maxChunkSize = 1048576; //megabyte dataInfoPacket.chunkCount = resourcePack.getPackSize() / dataInfoPacket.maxChunkSize; diff --git a/src/main/java/cn/nukkit/network/protocol/ResourcePackDataInfoPacket.java b/src/main/java/cn/nukkit/network/protocol/ResourcePackDataInfoPacket.java index d1960c9ec2c..aee26e58535 100644 --- a/src/main/java/cn/nukkit/network/protocol/ResourcePackDataInfoPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/ResourcePackDataInfoPacket.java @@ -26,7 +26,7 @@ public class ResourcePackDataInfoPacket extends DataPacket { public long compressedPackSize; public byte[] sha256; public boolean premium; - public int type = TYPE_RESOURCE; + public int type; @Override public void decode() { From 5229d96ad91002dcbfd38587dac79567f776a11d Mon Sep 17 00:00:00 2001 From: DasEtwas <18222134+DasEtwas@users.noreply.github.com> Date: Wed, 4 Aug 2021 18:31:48 +0200 Subject: [PATCH 6/7] Simplify encoding logic --- .../protocol/ResourcePacksInfoPacket.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java index a08c30a2d33..2e218b7924d 100644 --- a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java @@ -25,11 +25,11 @@ public void encode() { this.putBoolean(this.mustAccept); this.putBoolean(this.scripting); this.putBoolean(this.forceServerPacks); - this.encodeBehaviourPacks(this.behaviourPackEntries); - this.encodeResourcePacks(this.resourcePackEntries); + this.encodePacks(this.behaviourPackEntries); + this.encodePacks(this.resourcePackEntries); } - private void encodeBehaviourPacks(ResourcePack[] packs) { + private void encodePacks(ResourcePack[] packs) { this.putLShort(packs.length); for (ResourcePack entry : packs) { this.putString(entry.getPackId().toString()); @@ -39,20 +39,10 @@ private void encodeBehaviourPacks(ResourcePack[] packs) { this.putString(""); // sub-pack name this.putString(""); // content identity this.putBoolean(entry.requiresScripting()); // scripting - } - } - private void encodeResourcePacks(ResourcePack[] packs) { - this.putLShort(packs.length); - for (ResourcePack entry : packs) { - this.putString(entry.getPackId().toString()); - this.putString(entry.getPackVersion()); - this.putLLong(entry.getPackSize()); - this.putString(""); // encryption key - this.putString(""); // sub-pack name - this.putString(""); // content identity - this.putBoolean(false); // scripting - this.putBoolean(false); // raytracing capable + if (entry.getType().equals(ResourcePack.Type.RESOURCE_PACK)) { + this.putBoolean(false); // raytracing capable + } } } From 1d81948a672d1017b9effa62de7c07863679a5fc Mon Sep 17 00:00:00 2001 From: DasEtwas <18222134+DasEtwas@users.noreply.github.com> Date: Wed, 4 Aug 2021 19:34:16 +0200 Subject: [PATCH 7/7] Change spelling of behavior --- src/main/java/cn/nukkit/Player.java | 8 ++-- src/main/java/cn/nukkit/Server.java | 2 +- .../protocol/ResourcePackStackPacket.java | 6 +-- .../protocol/ResourcePacksInfoPacket.java | 4 +- .../cn/nukkit/resourcepacks/ResourcePack.java | 4 +- .../resourcepacks/ResourcePackManager.java | 40 +++++++++---------- .../resourcepacks/ZippedResourcePack.java | 4 +- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 990d136918a..2e7c6d97f03 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -1954,8 +1954,8 @@ protected void processLogin() { ResourcePackManager resourcePackManager = this.server.getResourcePackManager(); ResourcePacksInfoPacket infoPacket = new ResourcePacksInfoPacket(); infoPacket.resourcePackEntries = resourcePackManager.getResourcePacks(); - infoPacket.behaviourPackEntries = resourcePackManager.getBehaviourPacks(); - infoPacket.scripting = Arrays.stream(infoPacket.behaviourPackEntries).anyMatch(ResourcePack::requiresScripting); + infoPacket.behaviorPackEntries = resourcePackManager.getBehaviorPacks(); + infoPacket.scripting = Arrays.stream(infoPacket.behaviorPackEntries).anyMatch(ResourcePack::requiresScripting); infoPacket.mustAccept = this.server.getForceResources(); this.dataPacket(infoPacket); } @@ -2207,7 +2207,7 @@ public void onCompletion(Server server) { case RESOURCE_PACK: dataInfoPacket.type = ResourcePackDataInfoPacket.TYPE_RESOURCE; break; - case BEHAVIOUR_PACK: + case BEHAVIOR_PACK: dataInfoPacket.type = ResourcePackDataInfoPacket.TYPE_BEHAVIOR; break; } @@ -2223,7 +2223,7 @@ public void onCompletion(Server server) { ResourcePackStackPacket stackPacket = new ResourcePackStackPacket(); stackPacket.mustAccept = this.server.getForceResources(); stackPacket.resourcePackStack = this.server.getResourcePackManager().getResourcePacks(); - stackPacket.behaviourPackStack = this.server.getResourcePackManager().getBehaviourPacks(); + stackPacket.behaviorPackStack = this.server.getResourcePackManager().getBehaviorPacks(); this.dataPacket(stackPacket); break; case ResourcePackClientResponsePacket.STATUS_COMPLETED: diff --git a/src/main/java/cn/nukkit/Server.java b/src/main/java/cn/nukkit/Server.java index 1a6d61eda26..d4deb406c67 100644 --- a/src/main/java/cn/nukkit/Server.java +++ b/src/main/java/cn/nukkit/Server.java @@ -491,7 +491,7 @@ public Level remove(Object key) { convertLegacyPlayerData(); this.craftingManager = new CraftingManager(); - this.resourcePackManager = new ResourcePackManager(new File(Nukkit.DATA_PATH, "resource_packs"),new File(Nukkit.DATA_PATH, "client_behaviour_packs")); + this.resourcePackManager = new ResourcePackManager(new File(Nukkit.DATA_PATH, "resource_packs"),new File(Nukkit.DATA_PATH, "client_behavior_packs")); this.pluginManager = new PluginManager(this, this.commandMap); this.pluginManager.subscribeToPermission(Server.BROADCAST_CHANNEL_ADMINISTRATIVE, this.consoleSender); diff --git a/src/main/java/cn/nukkit/network/protocol/ResourcePackStackPacket.java b/src/main/java/cn/nukkit/network/protocol/ResourcePackStackPacket.java index 7a601abcd95..ff56a97542f 100644 --- a/src/main/java/cn/nukkit/network/protocol/ResourcePackStackPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/ResourcePackStackPacket.java @@ -9,7 +9,7 @@ public class ResourcePackStackPacket extends DataPacket { public static final byte NETWORK_ID = ProtocolInfo.RESOURCE_PACK_STACK_PACKET; public boolean mustAccept = false; - public ResourcePack[] behaviourPackStack = new ResourcePack[0]; + public ResourcePack[] behaviorPackStack = new ResourcePack[0]; public ResourcePack[] resourcePackStack = new ResourcePack[0]; public boolean isExperimental = false; public String gameVersion = ProtocolInfo.MINECRAFT_VERSION_NETWORK; @@ -24,8 +24,8 @@ public void encode() { this.reset(); this.putBoolean(this.mustAccept); - this.putUnsignedVarInt(this.behaviourPackStack.length); - for (ResourcePack entry : this.behaviourPackStack) { + this.putUnsignedVarInt(this.behaviorPackStack.length); + for (ResourcePack entry : this.behaviorPackStack) { this.putString(entry.getPackId().toString()); this.putString(entry.getPackVersion()); this.putString(""); //TODO: subpack name diff --git a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java index 2e218b7924d..48d5c770acd 100644 --- a/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java +++ b/src/main/java/cn/nukkit/network/protocol/ResourcePacksInfoPacket.java @@ -11,7 +11,7 @@ public class ResourcePacksInfoPacket extends DataPacket { public boolean mustAccept; public boolean scripting; public boolean forceServerPacks; - public ResourcePack[] behaviourPackEntries = new ResourcePack[0]; + public ResourcePack[] behaviorPackEntries = new ResourcePack[0]; public ResourcePack[] resourcePackEntries = new ResourcePack[0]; @Override @@ -25,7 +25,7 @@ public void encode() { this.putBoolean(this.mustAccept); this.putBoolean(this.scripting); this.putBoolean(this.forceServerPacks); - this.encodePacks(this.behaviourPackEntries); + this.encodePacks(this.behaviorPackEntries); this.encodePacks(this.resourcePackEntries); } diff --git a/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java b/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java index 08b9134800d..611f35885e1 100644 --- a/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java +++ b/src/main/java/cn/nukkit/resourcepacks/ResourcePack.java @@ -3,7 +3,7 @@ import java.util.UUID; /** - * Can either be a "Resource Pack" or a "Behaviour Pack" + * Can either be a "Resource Pack" or a "Behavior Pack" */ public interface ResourcePack { String getPackName(); @@ -25,7 +25,7 @@ public interface ResourcePack { enum Type { // Module types may not overlap. RESOURCE_PACK(new String[]{"resources"}), - BEHAVIOUR_PACK(new String[]{"client_data"}); // only client scripts are supported + BEHAVIOR_PACK(new String[]{"client_data"}); // only client scripts are supported private final String[] allowedModuleTypes; diff --git a/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java b/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java index 5a8d12b7322..f1818fd144b 100644 --- a/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java +++ b/src/main/java/cn/nukkit/resourcepacks/ResourcePackManager.java @@ -9,9 +9,9 @@ public class ResourcePackManager { private final Map packsById = new HashMap<>(); private final ResourcePack[] resourcePacks; - private final ResourcePack[] behaviourPacks; + private final ResourcePack[] behaviorPacks; - public ResourcePackManager(File resourcePacksPath, File behaviourPacksPath) { + public ResourcePackManager(File resourcePacksPath, File behaviorPacksPath) { if (!resourcePacksPath.exists()) { resourcePacksPath.mkdirs(); } else if (!resourcePacksPath.isDirectory()) { @@ -19,15 +19,15 @@ public ResourcePackManager(File resourcePacksPath, File behaviourPacksPath) { .translateString("nukkit.resources.invalid-path", resourcePacksPath.getName())); } - if (!behaviourPacksPath.exists()) { - behaviourPacksPath.mkdirs(); - } else if (!behaviourPacksPath.isDirectory()) { + if (!behaviorPacksPath.exists()) { + behaviorPacksPath.mkdirs(); + } else if (!behaviorPacksPath.isDirectory()) { throw new IllegalArgumentException(Server.getInstance().getLanguage() - .translateString("nukkit.resources.invalid-path", behaviourPacksPath.getName())); + .translateString("nukkit.resources.invalid-path", behaviorPacksPath.getName())); } List loadedResourcePacks = new ArrayList<>(); - List loadedBehaviourPacks = new ArrayList<>(); + List loadedBehaviorPacks = new ArrayList<>(); for (File pack : resourcePacksPath.listFiles()) { try { ResourcePack resourcePack = null; @@ -67,15 +67,15 @@ public ResourcePackManager(File resourcePacksPath, File behaviourPacksPath) { } } - for (File pack : behaviourPacksPath.listFiles()) { + for (File pack : behaviorPacksPath.listFiles()) { try { - ResourcePack behaviourPack = null; + ResourcePack behaviorPack = null; if (!pack.isDirectory()) { //directory resource packs temporarily unsupported switch (Files.getFileExtension(pack.getName())) { case "zip": case "mcpack": - behaviourPack = new ZippedResourcePack(pack); + behaviorPack = new ZippedResourcePack(pack); break; default: Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() @@ -84,19 +84,19 @@ public ResourcePackManager(File resourcePacksPath, File behaviourPacksPath) { } } - if (behaviourPack != null) { - if (!behaviourPack.getType().equals(ResourcePack.Type.BEHAVIOUR_PACK)) { + if (behaviorPack != null) { + if (!behaviorPack.getType().equals(ResourcePack.Type.BEHAVIOR_PACK)) { Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() - .translateString("nukkit.resources.invalid-type-behaviour", pack.getName(), behaviourPack.getPackName())); + .translateString("nukkit.resources.invalid-type-behavior", pack.getName(), behaviorPack.getPackName())); continue; } - loadedBehaviourPacks.add(behaviourPack); - this.packsById.put(behaviourPack.getPackId(), behaviourPack); + loadedBehaviorPacks.add(behaviorPack); + this.packsById.put(behaviorPack.getPackId(), behaviorPack); Server.getInstance().getLogger().info(Server.getInstance().getLanguage() - .translateString("nukkit.resources.loaded-behaviour-pack", behaviourPack.getPackName())); + .translateString("nukkit.resources.loaded-behavior-pack", behaviorPack.getPackName())); } } catch (IllegalArgumentException e) { Server.getInstance().getLogger().warning(Server.getInstance().getLanguage() @@ -105,18 +105,18 @@ public ResourcePackManager(File resourcePacksPath, File behaviourPacksPath) { } this.resourcePacks = loadedResourcePacks.toArray(new ResourcePack[0]); - this.behaviourPacks = loadedBehaviourPacks.toArray(new ResourcePack[0]); + this.behaviorPacks = loadedBehaviorPacks.toArray(new ResourcePack[0]); Server.getInstance().getLogger().info(Server.getInstance().getLanguage() - .translateString("nukkit.resources.success", String.valueOf(this.resourcePacks.length + this.behaviourPacks.length))); + .translateString("nukkit.resources.success", String.valueOf(this.resourcePacks.length + this.behaviorPacks.length))); } public ResourcePack[] getResourcePacks() { return this.resourcePacks; } - public ResourcePack[] getBehaviourPacks() { - return this.behaviourPacks; + public ResourcePack[] getBehaviorPacks() { + return this.behaviorPacks; } public ResourcePack getPackById(UUID id) { diff --git a/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java b/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java index 672545fd83a..87e7be96de2 100644 --- a/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java +++ b/src/main/java/cn/nukkit/resourcepacks/ZippedResourcePack.java @@ -43,7 +43,7 @@ public ZippedResourcePack(File file) { ZipEntry zipEntry = entries.nextElement(); if (zipEntry.getName().endsWith(".js")) { - // once a JavaScript file is found in the zip, we assume it will be a behaviour script + // once a JavaScript file is found in the zip, we assume it is a behavior script this.requiresScripting = true; } } @@ -96,6 +96,6 @@ public byte[] getPackChunk(int off, int len) { @Override public boolean requiresScripting() { - return this.getType().equals(Type.BEHAVIOUR_PACK) ? this.requiresScripting : false; + return this.getType().equals(Type.BEHAVIOR_PACK) ? this.requiresScripting : false; } }