From 497aa1301c45331a3ec686373729e2f03e37c559 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 1 Nov 2024 02:08:11 -0400 Subject: [PATCH 1/6] Starter Commit --- .../skript/expressions/ExprSkullOwner.java | 74 +++++++++++++------ 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java index bc102126022..ebb471be3b8 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java @@ -1,5 +1,6 @@ package ch.njol.skript.expressions; +import ch.njol.skript.aliases.ItemType; import ch.njol.skript.bukkitutil.ItemUtils; import ch.njol.skript.classes.Changer.ChangeMode; import ch.njol.skript.doc.Description; @@ -7,34 +8,41 @@ import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.util.slot.Slot; import ch.njol.util.coll.CollectionUtils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; -import org.bukkit.block.BlockState; import org.bukkit.block.Skull; import org.bukkit.event.Event; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; import org.jetbrains.annotations.Nullable; @Name("Skull Owner") @Description("The skull owner of a player skull.") @Examples({ "set {_owner} to the skull owner of event-block", - "set skull owner of {_block} to \"Njol\" parsed as offlineplayer" + "set skull owner of {_block} to \"Njol\" parsed as offlineplayer", + "set skull owner of player's tool to \"Njol\" parsed as offlineplayer" }) -@Since("2.9.0") -public class ExprSkullOwner extends SimplePropertyExpression { +@Since("2.9.0, INSERT VERSION (items)") +public class ExprSkullOwner extends SimplePropertyExpression { static { - register(ExprSkullOwner.class, OfflinePlayer.class, "(head|skull) owner", "blocks"); + register(ExprSkullOwner.class, OfflinePlayer.class, "(head|skull) owner", "slots/itemtypes/itemstacks/blocks"); } @Override - public @Nullable OfflinePlayer convert(Block block) { - BlockState state = block.getState(); - if (!(state instanceof Skull)) - return null; - return ((Skull) state).getOwningPlayer(); + public @Nullable OfflinePlayer convert(Object object) { + if (object instanceof Block block && block.getState() instanceof Skull skull) { + return skull.getOwningPlayer(); + } else { + ItemStack skullItem = ItemUtils.asItemStack(object); + if (skullItem == null || !(skullItem.getItemMeta() instanceof SkullMeta skullMeta)) + return null; + return skullMeta.getOwningPlayer(); + } } @Override @@ -48,22 +56,40 @@ public Class[] acceptChange(ChangeMode mode) { @Override public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { OfflinePlayer offlinePlayer = (OfflinePlayer) delta[0]; - for (Block block : getExpr().getArray(event)) { - BlockState state = block.getState(); - if (!(state instanceof Skull)) - continue; - - Skull skull = (Skull) state; - if (offlinePlayer.getName() != null) { - skull.setOwningPlayer(offlinePlayer); - } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { - //noinspection deprecation - skull.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + for (Object object : getExpr().getArray(event)) { + if (object instanceof Block block && block.getState() instanceof Skull skull) { + if (offlinePlayer.getName() != null) { + skull.setOwningPlayer(offlinePlayer); + } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { + //noinspection deprecation + skull.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + } else { + //noinspection deprecation + skull.setOwner(""); + } + skull.update(true, false); } else { - //noinspection deprecation - skull.setOwner(""); + ItemStack skullItem = ItemUtils.asItemStack(object); + if (skullItem == null || !(skullItem.getItemMeta() instanceof SkullMeta skullMeta)) + continue; + if (offlinePlayer.getName() != null) { + skullMeta.setOwningPlayer(offlinePlayer); + } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { + //noinspection deprecation + skullMeta.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + } else { + //noinspection deprecation + skullMeta.setOwner(""); + } + skullItem.setItemMeta(skullMeta); + if (object instanceof Slot slot) { + slot.setItem(skullItem); + } else if (object instanceof ItemType itemType) { + itemType.setItemMeta(skullMeta); + } else if (object instanceof ItemStack itemStack) { + itemStack.setItemMeta(skullMeta); + } } - skull.update(true, false); } } From 33cab64e0dd3072da7dc94291ebb65b0631be16c Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:08:57 -0400 Subject: [PATCH 2/6] Requested Changes --- .../njol/skript/expressions/ExprSkullOwner.java | 7 +++---- .../syntaxes/expressions/ExprSkullOwner.sk | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk diff --git a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java index ebb471be3b8..b1875eb319f 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java @@ -24,9 +24,9 @@ @Examples({ "set {_owner} to the skull owner of event-block", "set skull owner of {_block} to \"Njol\" parsed as offlineplayer", - "set skull owner of player's tool to \"Njol\" parsed as offlineplayer" + "set head owner of player's tool to {_player}" }) -@Since("2.9.0, INSERT VERSION (items)") +@Since("2.9.0, INSERT VERSION (of items)") public class ExprSkullOwner extends SimplePropertyExpression { static { @@ -46,8 +46,7 @@ public class ExprSkullOwner extends SimplePropertyExpression[] acceptChange(ChangeMode mode) { + public Class @Nullable [] acceptChange(ChangeMode mode) { if (mode == ChangeMode.SET) return CollectionUtils.array(OfflinePlayer.class); return null; diff --git a/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk b/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk new file mode 100644 index 00000000000..8917d5b5771 --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk @@ -0,0 +1,17 @@ + +test "skull owner - block": + set {_loc} to spawn of world "world" ~ vector(3,3,3) + set {_old} to block at {_loc} + set block at {_loc} to player head + assert skull owner of (block at {_loc}) is not set with "Block Skull Owner should not be set" + set {_player} to "Sovde" parsed as offline player + set skull owner of (block at {_loc}) to {_player} + assert skull owner of (block at {_loc}) is {_player} with "Block Skull Owner did not change" + set block at {_loc} to {_old} + +test "skull owner - item": + set {_skull} to a player head + assert skull owner of {_skull} is not set with "Item Skull Owner should not be set" + set {_player} to "Sovde" parsed as offline player + set skull owner of {_skull} to {_player} + assert skull owner of {_skull} is {_player} with "Item Skull Owner did not change" From e37d3691fd51dce1b53ecb4c532eaad311395dfc Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 1 Nov 2024 15:45:19 -0400 Subject: [PATCH 3/6] Consumers --- .../skript/expressions/ExprSkullOwner.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java index b1875eb319f..1c5252efe1e 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java @@ -19,6 +19,8 @@ import org.bukkit.inventory.meta.SkullMeta; import org.jetbrains.annotations.Nullable; +import java.util.function.Consumer; + @Name("Skull Owner") @Description("The skull owner of a player skull.") @Examples({ @@ -55,31 +57,17 @@ public class ExprSkullOwner extends SimplePropertyExpression blockChanger = getBlockChanger(offlinePlayer); + Consumer metaChanger = getMetaChanger(offlinePlayer); for (Object object : getExpr().getArray(event)) { if (object instanceof Block block && block.getState() instanceof Skull skull) { - if (offlinePlayer.getName() != null) { - skull.setOwningPlayer(offlinePlayer); - } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { - //noinspection deprecation - skull.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); - } else { - //noinspection deprecation - skull.setOwner(""); - } + blockChanger.accept(skull); skull.update(true, false); } else { ItemStack skullItem = ItemUtils.asItemStack(object); if (skullItem == null || !(skullItem.getItemMeta() instanceof SkullMeta skullMeta)) continue; - if (offlinePlayer.getName() != null) { - skullMeta.setOwningPlayer(offlinePlayer); - } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { - //noinspection deprecation - skullMeta.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); - } else { - //noinspection deprecation - skullMeta.setOwner(""); - } + metaChanger.accept(skullMeta); skullItem.setItemMeta(skullMeta); if (object instanceof Slot slot) { slot.setItem(skullItem); @@ -92,6 +80,28 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { } } + private Consumer getBlockChanger(OfflinePlayer offlinePlayer) { + if (offlinePlayer.getName() != null) { + return skull -> skull.setOwningPlayer(offlinePlayer); + } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { + //noinspection deprecation + return skull -> skull.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + } + //noinspection deprecation + return skull -> skull.setOwner(""); + } + + private Consumer getMetaChanger(OfflinePlayer offlinePlayer) { + if (offlinePlayer.getName() != null) { + return skullMeta -> skullMeta.setOwningPlayer(offlinePlayer); + } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { + //noinspection deprecation + return skullMeta -> skullMeta.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + } + //noinspection deprecation + return skullMeta -> skullMeta.setOwner(""); + } + @Override public Class getReturnType() { return OfflinePlayer.class; From 28bf394ff9dad8c1cbaf39fe11596547dfe6dd3c Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 1 Nov 2024 15:55:05 -0400 Subject: [PATCH 4/6] Reduce Profile Creation --- .../java/ch/njol/skript/expressions/ExprSkullOwner.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java index 1c5252efe1e..84e4b32ef93 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java @@ -17,6 +17,7 @@ import org.bukkit.event.Event; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.profile.PlayerProfile; import org.jetbrains.annotations.Nullable; import java.util.function.Consumer; @@ -85,7 +86,9 @@ private Consumer getBlockChanger(OfflinePlayer offlinePlayer) { return skull -> skull.setOwningPlayer(offlinePlayer); } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { //noinspection deprecation - return skull -> skull.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + PlayerProfile profile = Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), ""); + //noinspection deprecation + return skull -> skull.setOwnerProfile(profile); } //noinspection deprecation return skull -> skull.setOwner(""); @@ -96,7 +99,9 @@ private Consumer getMetaChanger(OfflinePlayer offlinePlayer) { return skullMeta -> skullMeta.setOwningPlayer(offlinePlayer); } else if (ItemUtils.CAN_CREATE_PLAYER_PROFILE) { //noinspection deprecation - return skullMeta -> skullMeta.setOwnerProfile(Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), "")); + PlayerProfile profile = Bukkit.createPlayerProfile(offlinePlayer.getUniqueId(), ""); + //noinspection deprecation + return skullMeta -> skullMeta.setOwnerProfile(profile); } //noinspection deprecation return skullMeta -> skullMeta.setOwner(""); From ae369c05623b4d0b4efbfc25d2e1085ecd7613b2 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:04:27 -0400 Subject: [PATCH 5/6] Minor Changes --- src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk b/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk index 8917d5b5771..4369944c01e 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk @@ -1,6 +1,5 @@ - test "skull owner - block": - set {_loc} to spawn of world "world" ~ vector(3,3,3) + set {_loc} to test-location set {_old} to block at {_loc} set block at {_loc} to player head assert skull owner of (block at {_loc}) is not set with "Block Skull Owner should not be set" From 8ee242ce647685fb0ec3f2469124325f9ec3b726 Mon Sep 17 00:00:00 2001 From: SirSmurfy2 <82696841+TheAbsolutionism@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:38:28 -0500 Subject: [PATCH 6/6] Update ExprSkullOwner --- src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java index 84e4b32ef93..204354bf567 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java +++ b/src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java @@ -69,8 +69,8 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { if (skullItem == null || !(skullItem.getItemMeta() instanceof SkullMeta skullMeta)) continue; metaChanger.accept(skullMeta); - skullItem.setItemMeta(skullMeta); if (object instanceof Slot slot) { + skullItem.setItemMeta(skullMeta); slot.setItem(skullItem); } else if (object instanceof ItemType itemType) { itemType.setItemMeta(skullMeta);