From 1993ab606f3297d908ca65c9d65cb5143fb5dbb8 Mon Sep 17 00:00:00 2001
From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com>
Date: Sun, 13 Oct 2024 09:29:02 -0400
Subject: [PATCH] Boats+ (#7055)
* Starter Commit
* Fix Chest Boat Checks
* One more :'(
* Requested Changes
* Requested Changes - 2
Changed, syntax patterns are now automatic
Changed, to jetbrains nullable
Changed, automatic item comparison
Fixed, inability to spawn chest boats
* Fixes
Added, Nullable annotation
Fixed, Test for entities
Changed, parameter names
Removed, 'fireball' and 'wind charge' entities for test
* Requested Changes
Changed, bamboo pattern getter
Changed, patterns in lang
Added, expected/got to assert
* Requested Changes
* Cleanup
* Filler
* Fix Conflict
---
.../ch/njol/skript/entity/BoatChestData.java | 89 +++++++---------
.../java/ch/njol/skript/entity/BoatData.java | 100 ++++++++----------
src/main/resources/lang/default.lang | 47 ++++----
.../skript/tests/syntaxes/effects/EffSpawn.sk | 27 -----
.../tests/syntaxes/sections/EffSecSpawn.sk | 66 ++++++++++++
5 files changed, 178 insertions(+), 151 deletions(-)
delete mode 100644 src/test/skript/tests/syntaxes/effects/EffSpawn.sk
diff --git a/src/main/java/ch/njol/skript/entity/BoatChestData.java b/src/main/java/ch/njol/skript/entity/BoatChestData.java
index 16676637590..77d5f2b985b 100644
--- a/src/main/java/ch/njol/skript/entity/BoatChestData.java
+++ b/src/main/java/ch/njol/skript/entity/BoatChestData.java
@@ -1,43 +1,38 @@
-/**
- * This file is part of Skript.
- *
- * Skript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Skript is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Skript. If not, see .
- *
- * Copyright Peter Güttinger, SkriptLang team and contributors
- */
package ch.njol.skript.entity;
import ch.njol.skript.Skript;
-import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser;
import org.bukkit.Material;
-import org.bukkit.TreeSpecies;
+import org.bukkit.entity.Boat;
import org.bukkit.entity.ChestBoat;
-import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
+import java.util.Locale;
import java.util.Random;
public class BoatChestData extends EntityData {
+ private static final Boat.Type[] types = Boat.Type.values();
+
static {
+ // This ensures all boats are registered
+ // As well as in the correct order via 'ordinal'
+ String[] patterns = new String[types.length + 2];
+ patterns[0] = "chest boat";
+ patterns[1] = "any chest boat";
+ for (Boat.Type boat : types) {
+ String boatName;
+ if (boat == Boat.Type.BAMBOO)
+ boatName = "bamboo chest raft";
+ else
+ boatName = boat.toString().replace("_", " ").toLowerCase(Locale.ENGLISH) + " chest boat";
+ patterns[boat.ordinal() + 2] = boatName;
+ }
+
if (Skript.classExists("org.bukkit.entity.ChestBoat")) {
- EntityData.register(BoatChestData.class, "chest boat", ChestBoat.class, 0,
- "chest boat", "any chest boat", "oak chest boat", "spruce chest boat", "birch chest boat",
- "jungle chest boat", "acacia chest boat", "dark oak chest boat");
+ EntityData.register(BoatChestData.class, "chest boat", ChestBoat.class, 0, patterns);
}
}
@@ -45,7 +40,7 @@ public BoatChestData() {
this(0);
}
- public BoatChestData(@Nullable TreeSpecies type) {
+ public BoatChestData(@Nullable Boat.Type type) {
this(type != null ? type.ordinal() + 2 : 1);
}
@@ -59,23 +54,23 @@ protected boolean init(Literal>[] exprs, int matchedPattern, SkriptParser.Pars
}
@Override
- protected boolean init(@Nullable Class extends ChestBoat> c, @Nullable ChestBoat e) {
- if (e != null)
- matchedPattern = 2 + e.getWoodType().ordinal();
+ protected boolean init(@Nullable Class extends ChestBoat> clazz, @Nullable ChestBoat entity) {
+ if (entity != null)
+ matchedPattern = 2 + entity.getBoatType().ordinal();
return true;
}
@Override
public void set(ChestBoat entity) {
if (matchedPattern == 1) // If the type is 'any boat'.
- matchedPattern += new Random().nextInt(TreeSpecies.values().length); // It will spawn a random boat type in case is 'any boat'.
+ matchedPattern += new Random().nextInt(Boat.Type.values().length); // It will spawn a random boat type in case is 'any boat'.
if (matchedPattern > 1) // 0 and 1 are excluded
- entity.setWoodType(TreeSpecies.values()[matchedPattern - 2]); // Removes 2 to fix the index.
+ entity.setBoatType(Boat.Type.values()[matchedPattern - 2]); // Removes 2 to fix the index.
}
@Override
protected boolean match(ChestBoat entity) {
- return matchedPattern <= 1 || entity.getWoodType().ordinal() == matchedPattern - 2;
+ return matchedPattern <= 1 || entity.getBoatType().ordinal() == matchedPattern - 2;
}
@Override
@@ -95,34 +90,32 @@ protected int hashCode_i() {
@Override
protected boolean equals_i(EntityData> obj) {
- if (obj instanceof BoatData)
- return matchedPattern == ((BoatData) obj).matchedPattern;
+ if (obj instanceof BoatChestData boatChestData)
+ return matchedPattern == boatChestData.matchedPattern;
return false;
}
@Override
- public boolean isSupertypeOf(EntityData> e) {
- if (e instanceof BoatData)
- return matchedPattern <= 1 || matchedPattern == ((BoatData) e).matchedPattern;
+ public boolean isSupertypeOf(EntityData> entity) {
+ if (entity instanceof BoatChestData boatChestData)
+ return matchedPattern <= 1 || matchedPattern == boatChestData.matchedPattern;
return false;
}
public boolean isOfItemType(ItemType itemType) {
int ordinal = -1;
- Material type = itemType.getMaterial();
- if (type == Material.OAK_CHEST_BOAT)
+ Material material = itemType.getMaterial();
+ if (material == Material.OAK_CHEST_BOAT) {
ordinal = 0;
- else if (type == Material.SPRUCE_CHEST_BOAT)
- ordinal = TreeSpecies.REDWOOD.ordinal();
- else if (type == Material.BIRCH_CHEST_BOAT)
- ordinal = TreeSpecies.BIRCH.ordinal();
- else if (type == Material.JUNGLE_CHEST_BOAT)
- ordinal = TreeSpecies.JUNGLE.ordinal();
- else if (type == Material.ACACIA_CHEST_BOAT)
- ordinal = TreeSpecies.ACACIA.ordinal();
- else if (type == Material.DARK_OAK_CHEST_BOAT)
- ordinal = TreeSpecies.DARK_OAK.ordinal();
+ } else {
+ for (Boat.Type boat : types) {
+ if (material.name().contains(boat.toString())) {
+ ordinal = boat.ordinal();
+ break;
+ }
+ }
+ }
return hashCode_i() == ordinal + 2 || (matchedPattern + ordinal == 0) || ordinal == 0;
}
diff --git a/src/main/java/ch/njol/skript/entity/BoatData.java b/src/main/java/ch/njol/skript/entity/BoatData.java
index 7d4a88c5930..2bea81c29b7 100644
--- a/src/main/java/ch/njol/skript/entity/BoatData.java
+++ b/src/main/java/ch/njol/skript/entity/BoatData.java
@@ -1,53 +1,41 @@
-/**
- * This file is part of Skript.
- *
- * Skript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Skript is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Skript. If not, see .
- *
- * Copyright Peter Güttinger, SkriptLang team and contributors
- */
package ch.njol.skript.entity;
-import java.lang.reflect.Method;
+import java.util.Locale;
import java.util.Random;
import org.bukkit.Material;
-import org.bukkit.TreeSpecies;
import org.bukkit.entity.Boat;
-import org.bukkit.inventory.ItemStack;
-import org.jetbrains.annotations.Nullable;
-
-import ch.njol.skript.Skript;
-import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
+import org.jetbrains.annotations.Nullable;
public class BoatData extends EntityData {
+
+ private static final Boat.Type[] types = Boat.Type.values();
+
static {
- // It will only register for 1.10+,
- // See SimpleEntityData if 1.9 or lower.
- if (Skript.methodExists(Boat.class, "getWoodType")) { //The 'boat' is the same of 'oak boat', 'any boat' works as supertype and it can spawn random boat.
- EntityData.register(BoatData.class, "boat", Boat.class, 0,
- "boat", "any boat", "oak boat", "spruce boat", "birch boat", "jungle boat", "acacia boat", "dark oak boat");
+ // This ensures all boats are registered
+ // As well as in the correct order via 'ordinal'
+ String[] patterns = new String[types.length + 2];
+ patterns[0] = "chest boat";
+ patterns[1] = "any chest boat";
+ for (Boat.Type boat : types) {
+ String boatName;
+ if (boat == Boat.Type.BAMBOO)
+ boatName = "bamboo raft";
+ else
+ boatName = boat.toString().replace("_", " ").toLowerCase(Locale.ENGLISH) + " boat";
+ patterns[boat.ordinal() + 2] = boatName;
}
+ EntityData.register(BoatData.class, "boat", Boat.class, 0, patterns);
}
public BoatData(){
this(0);
}
-
- public BoatData(@Nullable TreeSpecies type){
+
+ public BoatData(@Nullable Boat.Type type){
this(type != null ? type.ordinal() + 2 : 1);
}
@@ -62,23 +50,24 @@ protected boolean init(Literal>[] exprs, int matchedPattern, ParseResult parse
}
@Override
- protected boolean init(@Nullable Class extends Boat> c, @Nullable Boat e) {
- if (e != null)
- matchedPattern = 2 + e.getWoodType().ordinal();
+ protected boolean init(@Nullable Class extends Boat> clazz, @Nullable Boat entity) {
+ if (entity != null)
+
+ matchedPattern = 2 + entity.getBoatType().ordinal();
return true;
}
@Override
public void set(Boat entity) {
if (matchedPattern == 1) // If the type is 'any boat'.
- matchedPattern += new Random().nextInt(TreeSpecies.values().length); // It will spawn a random boat type in case is 'any boat'.
+ matchedPattern += new Random().nextInt(Boat.Type.values().length); // It will spawn a random boat type in case is 'any boat'.
if (matchedPattern > 1) // 0 and 1 are excluded
- entity.setWoodType(TreeSpecies.values()[matchedPattern - 2]); // Removes 2 to fix the index.
+ entity.setBoatType(Boat.Type.values()[matchedPattern - 2]); // Removes 2 to fix the index.
}
@Override
protected boolean match(Boat entity) {
- return matchedPattern <= 1 || entity.getWoodType().ordinal() == matchedPattern - 2;
+ return matchedPattern <= 1 || entity.getBoatType().ordinal() == matchedPattern - 2;
}
@Override
@@ -98,35 +87,32 @@ protected int hashCode_i() {
@Override
protected boolean equals_i(EntityData> obj) {
- if (obj instanceof BoatData)
- return matchedPattern == ((BoatData)obj).matchedPattern;
+ if (obj instanceof BoatData boatData)
+ return matchedPattern == boatData.matchedPattern;
return false;
}
@Override
- public boolean isSupertypeOf(EntityData> e) {
- if (e instanceof BoatData)
- return matchedPattern <= 1 || matchedPattern == ((BoatData)e).matchedPattern;
+ public boolean isSupertypeOf(EntityData> entity) {
+ if (entity instanceof BoatData boatData)
+ return matchedPattern <= 1 || matchedPattern == boatData.matchedPattern;
return false;
}
- public boolean isOfItemType(ItemType i){
+ public boolean isOfItemType(ItemType itemType){
int ordinal = -1;
- Material type = i.getMaterial();
- if (type == Material.OAK_BOAT)
+ Material material = itemType.getMaterial();
+ if (material == Material.OAK_BOAT) {
ordinal = 0;
- else if (type == Material.SPRUCE_BOAT)
- ordinal = TreeSpecies.REDWOOD.ordinal();
- else if (type == Material.BIRCH_BOAT)
- ordinal = TreeSpecies.BIRCH.ordinal();
- else if (type == Material.JUNGLE_BOAT)
- ordinal = TreeSpecies.JUNGLE.ordinal();
- else if (type == Material.ACACIA_BOAT)
- ordinal = TreeSpecies.ACACIA.ordinal();
- else if (type == Material.DARK_OAK_BOAT)
- ordinal = TreeSpecies.DARK_OAK.ordinal();
+ } else {
+ for (Boat.Type boat : types) {
+ if (material.name().contains(boat.toString())) {
+ ordinal = boat.ordinal();
+ break;
+ }
+ }
+ }
return hashCode_i() == ordinal + 2 || (matchedPattern + ordinal == 0) || ordinal == 0;
-
}
}
diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang
index 808b9d0185e..47de055fd8d 100644
--- a/src/main/resources/lang/default.lang
+++ b/src/main/resources/lang/default.lang
@@ -502,37 +502,37 @@ entities:
pattern: bat(|1¦s)
boat:
name: boat¦s
- pattern: boat(|1¦s)
+ pattern: boat[1:s]
any boat:
name: boat¦s
- pattern: any boat(|1¦s)
+ pattern: any boat[1:s]
oak boat:
name: oak boat¦s
- pattern: oak boat(|1¦s)
+ pattern: oak boat[1:s]
spruce boat:
name: spruce boat¦s
- pattern: spruce boat(|1¦s)
+ pattern: spruce boat[1:s]
birch boat:
name: birch boat¦s
- pattern: birch boat(|1¦s)
+ pattern: birch boat[1:s]
jungle boat:
name: jungle boat¦s
- pattern: jungle boat(|1¦s)
+ pattern: jungle boat[1:s]
acacia boat:
name: acacia boat¦s
- pattern: acacia boat(|1¦s)
+ pattern: acacia boat[1:s]
dark oak boat:
name: dark oak boat¦s
- pattern: dark oak boat(|1¦s)
+ pattern: dark oak boat[1:s]
mangrove boat:
name: mangrove boat¦s
- pattern: mangrove boat(|1¦s)
+ pattern: mangrove boat[1:s]
bamboo raft:
name: bamboo raft¦s
- pattern: bamboo (boat|raft)(|1¦s)
+ pattern: bamboo (boat|raft)[1:s]
cherry boat:
name: cherry boat¦s
- pattern: cherry [blossom] boat(|1¦s)
+ pattern: cherry [blossom] boat[1:s]
blaze:
name: blaze¦s
pattern: blaze(|1¦s)
@@ -1202,28 +1202,37 @@ entities:
# 1.19 Entities
chest boat:
name: chest boat¦s
- pattern: chest boat(|1¦s)
+ pattern: chest boat[1:s]
any chest boat:
name: chest boat¦s
- pattern: any chest boat(|1¦s)
+ pattern: any chest boat[1:s]
oak chest boat:
name: oak chest boat¦s
- pattern: oak chest boat(|1¦s)
+ pattern: oak chest boat[1:s]
spruce chest boat:
name: spruce chest boat¦s
- pattern: spruce chest boat(|1¦s)
+ pattern: spruce chest boat[1:s]
birch chest boat:
name: birch chest boat¦s
- pattern: birch chest boat(|1¦s)
+ pattern: birch chest boat[1:s]
jungle chest boat:
name: jungle chest boat¦s
- pattern: jungle chest boat(|1¦s)
+ pattern: jungle chest boat[1:s]
acacia chest boat:
name: acacia chest boat¦s
- pattern: acacia chest boat(|1¦s)
+ pattern: acacia chest boat[1:s]
dark oak chest boat:
name: dark oak chest boat¦s
- pattern: dark oak chest boat(|1¦s)
+ pattern: dark oak chest boat[1:s]
+ cherry chest boat:
+ name: cherry chest boat¦s
+ pattern: cherry chest boat[1:s]
+ mangrove chest boat:
+ name: mangrove chest boat¦s
+ pattern: mangrove chest boat[1:s]
+ bamboo chest raft:
+ name: bamboo chest raft¦s
+ pattern: bamboo chest (raft|boat)[1:s]
frog:
name: frog¦s
pattern: frog(|1¦s)|(4¦)frog (kid(|1¦s)|child(|1¦ren))
diff --git a/src/test/skript/tests/syntaxes/effects/EffSpawn.sk b/src/test/skript/tests/syntaxes/effects/EffSpawn.sk
deleted file mode 100644
index ef24eab0162..00000000000
--- a/src/test/skript/tests/syntaxes/effects/EffSpawn.sk
+++ /dev/null
@@ -1,27 +0,0 @@
-test "spawn cats by type" when running minecraft "1.15.2":
- delete all cats
- set {_l} to location of spawn of world "world"
- spawn 5 all black cats at {_l}
- assert size of all cats = 5 with "Size of all cats is not 5"
- assert size of all all black cats = 5 with "Size of all all black cats is not 5"
- spawn 2 siamese cats at {_l}
- assert size of all cats = 7 with "Size of all cats is not 7"
- assert size of all siamese cats = 2 with "Size of all siamese cats is not 2"
- delete all siamese cats
- assert size of all cats = 5 with "Size of all cats is not 5 after delete 2 siamese cats"
- delete all cats
- assert size of all cats = 0 with "Size of all cats is greater than 0 after all were deleted"
-
-test "spawn wolves by variant" when running minecraft "1.21.0":
- delete all wolves
- set {_l} to location of spawn of world "world"
- spawn 5 ashen wolves at {_l}
- assert size of all wolves = 5 with "Size of all wolves is not 5"
- assert size of all ashen wolves = 5 with "Size of all ashen wolves is not 5"
- spawn 2 rusty wolves at {_l}
- assert size of all wolves = 7 with "Size of all wolves is not 7"
- assert size of all rusty wolves = 2 with "Size of all rusty wolves is not 2"
- delete all rusty wolves
- assert size of all wolves = 5 with "Size of all wolves is not 5 after delete 2 rusty wolves"
- delete all wolves
- assert size of all wolves = 0 with "Size of all wolves is greater than 0 after all were deleted"
diff --git a/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk b/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk
index f151f09821b..da7b84a7431 100644
--- a/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk
+++ b/src/test/skript/tests/syntaxes/sections/EffSecSpawn.sk
@@ -20,3 +20,69 @@ test "spawn section":
# Make sure variables carry over properly
assert {_before} is 10 with "value of {_before} should be 10 (got '%{_before}%')"
assert {_new var} is 5 with "value of {_new var} should be 5 (got '%{_new var}%')"
+
+test "spawn cats by type" when running minecraft "1.15.2":
+ delete all cats
+ set {_l} to location of spawn of world "world"
+ spawn 5 all black cats at {_l}
+ assert size of all cats = 5 with "Size of all cats is not 5"
+ assert size of all all black cats = 5 with "Size of all all black cats is not 5"
+ spawn 2 siamese cats at {_l}
+ assert size of all cats = 7 with "Size of all cats is not 7"
+ assert size of all siamese cats = 2 with "Size of all siamese cats is not 2"
+ delete all siamese cats
+ assert size of all cats = 5 with "Size of all cats is not 5 after delete 2 siamese cats"
+ delete all cats
+ assert size of all cats = 0 with "Size of all cats is greater than 0 after all were deleted"
+
+test "spawn wolves by variant" when running minecraft "1.21.0":
+ delete all wolves
+ set {_l} to location of spawn of world "world"
+ spawn 5 ashen wolves at {_l}
+ assert size of all wolves = 5 with "Size of all wolves is not 5"
+ assert size of all ashen wolves = 5 with "Size of all ashen wolves is not 5"
+ spawn 2 rusty wolves at {_l}
+ assert size of all wolves = 7 with "Size of all wolves is not 7"
+ assert size of all rusty wolves = 2 with "Size of all rusty wolves is not 2"
+ delete all rusty wolves
+ assert size of all wolves = 5 with "Size of all wolves is not 5 after delete 2 rusty wolves"
+ delete all wolves
+ assert size of all wolves = 0 with "Size of all wolves is greater than 0 after all were deleted"
+
+test "spawn entities":
+ set {_entities::*} to "allay", "axolotl", "bat", "bee", "blaze", "cat", "cave spider", "chicken" and "cod"
+ add "cow", "creeper", "dolphin", "donkey", "drowned", "elder guardian", "enderman" and "endermite" to {_entities::*}
+ add "evoker", "fox", "frog", "ghast", "glow squid", "goat", "guardian", "hoglin", "horse" and "husk" to {_entities::*}
+ add "iron golem", "llama", "magma cube", "mooshroom", "mule", "ocelot", "panda", "parrot" and "phantom" to {_entities::*}
+ add "pig", "piglin", "piglin brute", "pillager", "polar bear", "pufferfish", "rabbit" and "ravager" to {_entities::*}
+ add "salmon", "sheep", "shulker", "silverfish", "skeleton", "skeleton horse" and "slime" to {_entities::*}
+ add "snow golem", "spider", "squid", "stray", "strider", "tadpole", "trader llama" and "tropical fish" to {_entities::*}
+ add "turtle", "vex", "villager", "vindicator", "wandering trader", "warden", "witch" and "wither skeleton" to {_entities::*}
+ add "wolf", "zoglin", "zombie", "zombie horse", "zombie villager", "zombified piglin" to {_entities::*}
+ add "ender dragon" and "wither" to {_entities::*}
+ add "oak boat", "oak chest boat", "armor stand", "primed tnt", "firework", "arrow", "trident" and "egg" to {_entities::*}
+ add "snowball", "end crystal", "eye of ender", "regular minecart", "falling sand" and "falling gravel" to {_entities::*}
+ add "falling powder snow" to {_entities::*}
+
+ if running minecraft "1.20.0":
+ add "camel" and "sniffer" to {_entities::*}
+ if running minecraft "1.20.6":
+ add "armadillo" to {_entities::*}
+ if running minecraft "1.21":
+ add "bogged" and "breeze" to {_entities::*}
+
+ # Note: Had to remove 'fireball' and 'wind charge'
+
+ clear all entities
+ loop {_entities::*}:
+ set {_parse} to loop-value parsed as entity type
+ if {_parse} is set:
+ spawn {_parse} at spawn of "world":
+ set {_ent} to entity
+ set {_assert} to whether {_ent} is entity type within {_parse}
+ assert {_assert} is true with "", expected (entity type within {_parse}), got {_ent}
+ clear entity within {_ent}
+ clear {_ent}
+ else:
+ assert false is true with "Failed to parse '%loop-value%' as an entity type", expected loop-value, got {_parse}
+ clear all entities