Skip to content

Commit 5434c6d

Browse files
authored
Merge branch 'dev/feature' into dev/folia
2 parents 4fdf56f + fd1963a commit 5434c6d

File tree

13 files changed

+219
-51
lines changed

13 files changed

+219
-51
lines changed

src/main/java/com/shanebeestudios/skbee/api/nbt/NBTApi.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil;
2020
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
2121
import org.apache.commons.lang3.ArrayUtils;
22+
import org.bukkit.Bukkit;
2223
import org.bukkit.OfflinePlayer;
2324
import org.bukkit.block.Block;
2425
import org.bukkit.block.BlockState;
@@ -44,6 +45,8 @@ public class NBTApi {
4445
private static boolean ENABLED;
4546
public static final boolean HAS_ITEM_COMPONENTS = Skript.isRunningMinecraft(1, 20, 5);
4647
static final String TAG_NAME = HAS_ITEM_COMPONENTS ? "components" : "tag";
48+
@SuppressWarnings("deprecation")
49+
private static final int DATA_VERSION = Bukkit.getUnsafe().getDataVersion();
4750

4851
/**
4952
* Initialize this NBT API
@@ -866,10 +869,10 @@ public static void addNBTToEntity(Entity entity, NBTCompound compound) {
866869
* @return ItemStack from NBT
867870
*/
868871
public static ItemStack convertNBTtoItem(@NotNull NBTCompound nbtcompound) {
869-
if (!nbtcompound.hasTag("DataVersion") || nbtcompound.getInteger("DataVersion") != NBTReflection.getDataVersion()) {
872+
if (!nbtcompound.hasTag("DataVersion") || nbtcompound.getInteger("DataVersion") != getDataVersion()) {
870873
int dataVersion = nbtcompound.hasTag("DataVersion") ? nbtcompound.getInteger("DataVersion") : DataFixerUtil.VERSION1_20_4;
871874
try {
872-
ReadWriteNBT fixedItemNBT = DataFixerUtil.fixUpItemData(nbtcompound, dataVersion, NBTReflection.getDataVersion());
875+
ReadWriteNBT fixedItemNBT = DataFixerUtil.fixUpItemData(nbtcompound, dataVersion, getDataVersion());
873876
return NBTItem.convertNBTtoItem((NBTCompound) fixedItemNBT);
874877
} catch (NoSuchFieldException | IllegalAccessException e) {
875878
throw new RuntimeException(e);
@@ -878,4 +881,13 @@ public static ItemStack convertNBTtoItem(@NotNull NBTCompound nbtcompound) {
878881
return NBTItem.convertNBTtoItem(nbtcompound);
879882
}
880883

884+
/**
885+
* Get the Minecraft DataVersion
886+
*
887+
* @return DataVersion from MC
888+
*/
889+
public static int getDataVersion() {
890+
return NBTApi.DATA_VERSION;
891+
}
892+
881893
}

src/main/java/com/shanebeestudios/skbee/api/nbt/NBTCustomItemStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static NBTCompound getContainer(NBTCompound itemContainer, boolean isFul
4141
if (isFull) {
4242
// TODO temp solution until NBT API handles this
4343
// DataVersion is used for deserializing and running thru DataFixerUpper
44-
itemContainer.setInteger("DataVersion", NBTReflection.getDataVersion());
44+
itemContainer.setInteger("DataVersion", NBTApi.getDataVersion());
4545
// TODO end
4646
return itemContainer;
4747
}

src/main/java/com/shanebeestudios/skbee/api/nbt/NBTReflection.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
@SuppressWarnings({"SequencedCollectionMethodCanBeUsed", "CallToPrintStackTrace", "DataFlowIssue"})
1616
public class NBTReflection {
1717

18-
@SuppressWarnings("deprecation")
19-
private static final int DATA_VERSION = Bukkit.getUnsafe().getDataVersion();
20-
2118
// Classes
2219
private static Class<?> CRAFT_ITEM_STACK_CLASS;
2320

@@ -70,15 +67,6 @@ public class NBTReflection {
7067
}
7168
}
7269

73-
/**
74-
* Get the Minecraft DataVersion
75-
*
76-
* @return DataVersion from MC
77-
*/
78-
public static int getDataVersion() {
79-
return DATA_VERSION;
80-
}
81-
8270
/**
8371
* Get the vanilla version of NBT of an item
8472
* <br>This will show components which don't normally show in NBT

src/main/java/com/shanebeestudios/skbee/api/property/PropertyPrinter.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import ch.njol.skript.classes.ClassInfo;
44
import ch.njol.skript.registrations.Classes;
5+
import com.shanebeestudios.skbee.api.util.Util;
56

7+
import java.io.File;
68
import java.io.IOException;
79
import java.io.PrintWriter;
810
import java.nio.charset.StandardCharsets;
@@ -18,6 +20,7 @@
1820
public class PropertyPrinter {
1921

2022
public static void printAll() {
23+
long start = System.currentTimeMillis();
2124
Map<Class<?>, List<Property<?, ?>>> mapByClass = new HashMap<>();
2225
List<Holder> holderList = new ArrayList<>();
2326

@@ -44,7 +47,25 @@ public static void printAll() {
4447
});
4548

4649
try {
47-
PrintWriter writer = new PrintWriter("plugins/SkBee/properties/all.txt", StandardCharsets.UTF_8);
50+
File file = new File("plugins/SkBee/properties/all-properties.md");
51+
if (!file.exists()) {
52+
File parentFile = file.getParentFile();
53+
if (!parentFile.exists()) {
54+
if (!parentFile.mkdirs()) {
55+
Util.skriptError("Unable to create properties directory.");
56+
return;
57+
} else {
58+
Util.log("Created properties directory.");
59+
}
60+
}
61+
if (!file.createNewFile()) {
62+
Util.skriptError("Unable to create properties file.");
63+
return;
64+
} else {
65+
Util.log("Created properties file.");
66+
}
67+
}
68+
PrintWriter writer = new PrintWriter(file, StandardCharsets.UTF_8);
4869
writer.println("# Properties");
4970
writer.println("Properties are simplied versions of full expression, which are used in the [Property Expression](https://skripthub.net/docs/?id=13236)");
5071
writer.println();
@@ -62,6 +83,8 @@ public static void printAll() {
6283
holder.properties.forEach(property -> printProperty(writer, property));
6384
});
6485
writer.close();
86+
long fin = System.currentTimeMillis() - start;
87+
Util.log("Properties written to file in %s ms.", fin);
6588
} catch (IOException e) {
6689
throw new RuntimeException(e);
6790
}

src/main/java/com/shanebeestudios/skbee/elements/bound/conditions/CondBoundContainsLocation.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,50 @@
1010
import ch.njol.skript.lang.SkriptParser.ParseResult;
1111
import ch.njol.util.Kleenean;
1212
import com.shanebeestudios.skbee.api.bound.Bound;
13+
import com.shanebeestudios.skbee.api.skript.base.SimpleExpression;
1314
import org.bukkit.Location;
1415
import org.bukkit.event.Event;
1516
import org.jetbrains.annotations.NotNull;
1617

1718
@Name("Bound - Contains Location")
1819
@Description("Check if a location is within the bounds of a bounding box.")
1920
@Examples({"on break:",
20-
"\tif location of event-block is within bound with id \"spawn.bound\":",
21-
"\t\tcancel event", "",
22-
"on damage of a player:",
23-
"\tif victim is within bound {spawn}:",
24-
"\t\tcancel event"})
21+
"\tif location of event-block is within bound with id \"spawn.bound\":",
22+
"\t\tcancel event", "",
23+
"on damage of a player:",
24+
"\tif victim is within bound {spawn}:",
25+
"\t\tcancel event"})
2526
@Since("1.0.0")
2627
public class CondBoundContainsLocation extends Condition {
2728

2829
static {
29-
PropertyCondition.register(CondBoundContainsLocation.class, "[with]in [bound] %bound%", "locations");
30+
PropertyCondition.register(CondBoundContainsLocation.class, "[with]in [bound[s]] %bounds%", "locations");
3031
}
3132

32-
private Expression<Bound> bound;
33+
private Expression<Bound> bounds;
3334
private Expression<Location> locations;
3435

35-
@SuppressWarnings({"unchecked", "null", "NullableProblems"})
36+
@SuppressWarnings("unchecked")
3637
@Override
3738
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean kleenean, ParseResult parseResult) {
38-
bound = (Expression<Bound>) exprs[1];
39-
locations = (Expression<Location>) exprs[0];
39+
this.bounds = (Expression<Bound>) exprs[1];
40+
this.locations = (Expression<Location>) exprs[0];
4041
setNegated(matchedPattern == 1);
4142
return true;
4243
}
4344

44-
@SuppressWarnings("NullableProblems")
4545
@Override
4646
public boolean check(Event event) {
47-
Bound bound = this.bound.getSingle(event);
48-
if (bound == null) return false;
49-
return locations.check(event, bound::isInRegion, isNegated());
47+
Location[] locs = this.locations.getArray(event);
48+
boolean and = this.locations.getAnd();
49+
return this.bounds.check(event, bound ->
50+
SimpleExpression.check(locs, bound::isInRegion, isNegated(), and));
5051
}
5152

5253
@Override
5354
public @NotNull String toString(Event e, boolean d) {
5455
return PropertyCondition.toString(this, PropertyCondition.PropertyType.BE, e, d, locations,
55-
"in the bound " + bound.toString(e, d));
56+
"in the bound[s] " + this.bounds.toString(e, d));
5657
}
5758

5859
}

src/main/java/com/shanebeestudios/skbee/elements/bound/expressions/ExprBoundFromID.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,29 @@ public class ExprBoundFromID extends SimpleExpression<Bound> {
3333
private Expression<String> ids;
3434
private static final BoundConfig boundConfig = SkBee.getPlugin().getBoundConfig();
3535

36-
@SuppressWarnings({"unchecked", "NullableProblems"})
36+
@SuppressWarnings("unchecked")
3737
@Override
3838
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean kleenean, ParseResult parseResult) {
3939
this.ids = (Expression<String>) exprs[0];
4040
return true;
4141
}
4242

43-
@SuppressWarnings("NullableProblems")
4443
@Override
4544
protected Bound[] get(Event event) {
4645
List<Bound> bounds = new ArrayList<>();
47-
for (String id : this.ids.getArray(event)) {
46+
for (String id : this.ids.getAll(event)) {
4847
Bound bound = boundConfig.getBoundFromID(id);
4948
if (bounds.contains(bound)) continue;
5049
bounds.add(bound);
5150
}
5251
return bounds.toArray(new Bound[0]);
5352
}
5453

54+
@Override
55+
public boolean getAnd() {
56+
return this.ids.getAnd();
57+
}
58+
5559
@Override
5660
public boolean isSingle() {
5761
return ids.isSingle();
@@ -64,7 +68,7 @@ public boolean isSingle() {
6468

6569
@Override
6670
public @NotNull String toString(Event event, boolean debug) {
67-
return "bound from id " + this.ids.toString(event, debug);
71+
return "bound[s] from id " + this.ids.toString(event, debug);
6872
}
6973

7074
}

src/main/java/com/shanebeestudios/skbee/elements/nbt/expressions/ExprNbtCompound.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
@Since("1.6.0")
8282
public class ExprNbtCompound extends PropertyExpression<Object, NBTCompound> {
8383

84+
// My reflection requires Paper/Mojmap. I cant be bothered to re-write this for Spigot
85+
private static final boolean HAS_VANILLA_NBT = Skript.classExists("net.minecraft.server.level.ServerPlayer") && NBTApi.HAS_ITEM_COMPONENTS;
8486
private static final boolean ALLOW_UNSAFE_OPERATIONS = SkBee.getPlugin().getPluginConfig().NBT_ALLOW_UNSAFE_OPERATIONS;
8587

8688
static {
@@ -101,7 +103,11 @@ public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNul
101103
Expression<?> expr = LiteralUtils.defendExpression(exprs[0]);
102104
setExpr(expr);
103105
this.isFullItem = parseResult.hasTag("full");
104-
this.isVanilla = parseResult.hasTag("vanilla") && NBTApi.HAS_ITEM_COMPONENTS;
106+
this.isVanilla = parseResult.hasTag("vanilla");
107+
if (this.isVanilla && !HAS_VANILLA_NBT) {
108+
Skript.error("Vanilla NBT requires a PaperMC server (or any other MojMapped server).");
109+
return false;
110+
}
105111
this.isCustom = parseResult.hasTag("custom");
106112
this.isCopy = parseResult.hasTag("copy");
107113
this.isFile = matchedPattern == 1;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.shanebeestudios.skbee.elements.other.effects;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.doc.Description;
5+
import ch.njol.skript.doc.Examples;
6+
import ch.njol.skript.doc.Name;
7+
import ch.njol.skript.doc.Since;
8+
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.SkriptParser.ParseResult;
10+
import ch.njol.skript.lang.SyntaxStringBuilder;
11+
import ch.njol.util.Kleenean;
12+
import com.shanebeestudios.skbee.SkBee;
13+
import com.shanebeestudios.skbee.api.skript.base.Effect;
14+
import org.bukkit.Bukkit;
15+
import org.bukkit.command.CommandSender;
16+
import org.bukkit.event.Event;
17+
import org.jetbrains.annotations.Nullable;
18+
19+
@Name("Dispatch Command")
20+
@Description({"Similar to Skript's command effect, with the option to use any command sender",
21+
"(vs. being restricted to only player/console), and the option to temporarily attach a permission.",
22+
"The attached permission will only last for 1 tick, and automatically remove. This is not persistent."})
23+
@Examples({"dispatch player command \"give %player% stick\" with permission \"minecraft.command.give\"",
24+
"dispatch (random element of all mobs) command \"/tell %player% hi\""})
25+
@Since("INSERT VERSION")
26+
public class EffDispatchCommand extends Effect {
27+
28+
private static final SkBee PLUGIN = SkBee.getPlugin();
29+
30+
static {
31+
Skript.registerEffect(EffDispatchCommand.class,
32+
"dispatch %commandsender% command %string% [with permission %-string%]");
33+
}
34+
35+
private Expression<CommandSender> commandSender;
36+
private Expression<String> command;
37+
private Expression<String> permission;
38+
39+
@SuppressWarnings("unchecked")
40+
@Override
41+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
42+
this.commandSender = (Expression<CommandSender>) exprs[0];
43+
this.command = (Expression<String>) exprs[1];
44+
this.permission = (Expression<String>) exprs[2];
45+
return true;
46+
}
47+
48+
@Override
49+
protected void execute(Event event) {
50+
CommandSender commandSender = this.commandSender.getSingle(event);
51+
String command = this.command.getSingle(event);
52+
if (commandSender == null || command == null) return;
53+
54+
if (this.permission != null) {
55+
String permission = this.permission.getSingle(event);
56+
if (permission == null) return;
57+
58+
commandSender.addAttachment(PLUGIN, permission, true, 1);
59+
}
60+
if (command.startsWith("/")) command = command.substring(1);
61+
62+
Bukkit.dispatchCommand(commandSender, command);
63+
}
64+
65+
@Override
66+
public String toString(@Nullable Event e, boolean d) {
67+
String perm = this.permission == null ? "" : " with permission " + this.permission.getSingle(e);
68+
return new SyntaxStringBuilder(e, d)
69+
.append("dispatch", this.commandSender, "command", this.command)
70+
.append(perm)
71+
.toString();
72+
}
73+
74+
}

src/main/java/com/shanebeestudios/skbee/elements/other/events/OtherEvents.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
import org.bukkit.block.BlockState;
1818
import org.bukkit.block.CreatureSpawner;
1919
import org.bukkit.block.data.BlockData;
20+
import org.bukkit.command.CommandSender;
2021
import org.bukkit.entity.Entity;
2122
import org.bukkit.entity.Item;
2223
import org.bukkit.entity.LivingEntity;
2324
import org.bukkit.entity.Player;
2425
import org.bukkit.entity.Projectile;
2526
import org.bukkit.entity.Spellcaster;
2627
import org.bukkit.event.Event;
27-
import org.bukkit.event.block.BellRingEvent;
2828
import org.bukkit.event.block.BlockDamageAbortEvent;
2929
import org.bukkit.event.block.BlockDropItemEvent;
3030
import org.bukkit.event.block.BlockExplodeEvent;
3131
import org.bukkit.event.block.BlockPlaceEvent;
3232
import org.bukkit.event.block.MoistureChangeEvent;
33+
import org.bukkit.event.command.UnknownCommandEvent;
3334
import org.bukkit.event.entity.EntityAirChangeEvent;
3435
import org.bukkit.event.entity.EntityBreedEvent;
3536
import org.bukkit.event.entity.EntityChangeBlockEvent;
@@ -608,6 +609,18 @@ public PlayerSpawnChangeEvent.Cause get(PlayerSpawnChangeEvent event) {
608609
}
609610
}, EventValues.TIME_FUTURE);
610611
}
612+
613+
if (Skript.classExists("org.bukkit.event.command.UnknownCommandEvent")) {
614+
Skript.registerEvent("Unknown Command", OtherEvents.class, UnknownCommandEvent.class, "unknown command")
615+
.description("This event is fired when a player executes a command that is not defined.",
616+
"`event-string` = The command that was sent.",
617+
"`event-sender/player` = Who sent the command.")
618+
.examples("")
619+
.since("INSERT VERSION");
620+
621+
EventValues.registerEventValue(UnknownCommandEvent.class, String.class, UnknownCommandEvent::getCommandLine);
622+
EventValues.registerEventValue(UnknownCommandEvent.class, CommandSender.class, UnknownCommandEvent::getSender);
623+
}
611624
}
612625

613626
}

0 commit comments

Comments
 (0)