Skip to content

Commit

Permalink
Merge branch 'dev/patch' into feature/rename-potion-type
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth authored Jul 31, 2024
2 parents ddc907b + 22a2bfb commit 46f0d25
Show file tree
Hide file tree
Showing 43 changed files with 438 additions and 145 deletions.
1 change: 0 additions & 1 deletion .github/workflows/archive-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
jobs:
archive-docs:
if: "! contains(toJSON(github.event.commits.*.message), '[ci skip]')"
needs: release-docs
runs-on: ubuntu-latest
steps:
- name: Configure workflow
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel=true

groupid=ch.njol
name=skript
version=2.9.0-pre1
version=2.9.0
jarName=Skript.jar
testEnv=java21/paper-1.21.0
testEnvJavaVersion=21
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public boolean equals(final @Nullable Object obj) {
return false;

ItemData other = (ItemData) obj;
if (isAlias) { // This is alias, other item might not be
if (isAlias()) { // This is alias, other item might not be
return other.matchAlias(this).isAtLeast(MatchQuality.SAME_ITEM);
} else { // This is not alias, but other might be
return matchAlias(other).isAtLeast(MatchQuality.SAME_ITEM);
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,27 @@ public ItemType clone() {
.collect(Collectors.toList());
if (datas.isEmpty())
return null;
int numItems = datas.size();
int index = random.nextInt(numItems);
ItemStack is = datas.get(index).getStack();
ItemStack is = datas.get(random.nextInt(datas.size())).getStack();
assert is != null; // verified above
is = is.clone();
is.setAmount(getAmount());
return is;
}

/**
* @return One random ItemStack or Material that this ItemType represents.
* A Material may only be returned for ItemStacks containing a Material where {@link Material#isItem()} is false.
*/
public Object getRandomStackOrMaterial() {
ItemData randomData = types.get(random.nextInt(types.size()));
ItemStack stack = randomData.getStack();
if (stack == null)
return randomData.getType();
stack = stack.clone();
stack.setAmount(getAmount());
return stack;
}

/**
* Test whether this ItemType can be put into the given inventory completely.
* <p>
Expand Down Expand Up @@ -1396,8 +1408,11 @@ public void clearItemMeta() {
globalMeta = null;
}

/**
* @return A random Material this ItemType represents.
*/
public Material getMaterial() {
ItemData data = types.get(0);
ItemData data = types.get(random.nextInt(types.size()));
if (data == null)
throw new IllegalStateException("material not found");
return data.getType();
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/ch/njol/skript/bukkitutil/InventoryUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package ch.njol.skript.bukkitutil;

import ch.njol.skript.Skript;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.eclipse.jdt.annotation.Nullable;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
* Utilities for inventories.
* In newer versions (1.21+), InventoryView is an interface instead of an abstract class
* Directing calling InventoryView#getTitle on 1.20.6 and below results in an IncompatibleClassChangeError
* as an interface, not an abstract class, is expected.
*/
public class InventoryUtils {

private static final @Nullable MethodHandle GET_TITLE;
private static final @Nullable MethodHandle GET_INVENTORY;
private static final @Nullable MethodHandle CONVERT_SLOT;
private static final @Nullable MethodHandle GET_TOP_INVENTORY;
private static final @Nullable MethodHandle GET_BOTTOM_INVENTORY;

static {
MethodHandle getTitle = null;
MethodHandle getInventory = null;
MethodHandle convertSlot = null;
MethodHandle getTopInventory = null;
MethodHandle getBottomInventory = null;
if (!InventoryView.class.isInterface()) { // initialize legacy support as it's likely an abstract class
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
getTitle = lookup.findVirtual(InventoryView.class, "getTitle", MethodType.methodType(String.class));
getInventory = lookup.findVirtual(InventoryView.class, "getInventory", MethodType.methodType(Inventory.class, int.class));
convertSlot = lookup.findVirtual(InventoryView.class, "convertSlot", MethodType.methodType(int.class, int.class));
getTopInventory = lookup.findVirtual(InventoryView.class, "getTopInventory", MethodType.methodType(Inventory.class));
getBottomInventory = lookup.findVirtual(InventoryView.class, "getBottomInventory", MethodType.methodType(Inventory.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
Skript.exception(e, "Failed to load old inventory view support.");
}
}
GET_TITLE = getTitle;
GET_INVENTORY = getInventory;
CONVERT_SLOT = convertSlot;
GET_TOP_INVENTORY = getTopInventory;
GET_BOTTOM_INVENTORY = getBottomInventory;
}

/**
* @see InventoryView#getTitle()
*/
public static @Nullable String getTitle(InventoryView inventoryView) {
if (GET_TITLE == null)
return inventoryView.getTitle();
try {
return (String) GET_TITLE.invoke(inventoryView);
} catch (Throwable ignored) { }
return null;
}

/**
* @see InventoryView#getInventory(int)
*/
public static @Nullable Inventory getInventory(InventoryView inventoryView, int rawSlot) {
if (GET_INVENTORY == null)
return inventoryView.getInventory(rawSlot);
try {
return (Inventory) GET_INVENTORY.invoke(inventoryView, rawSlot);
} catch (Throwable ignored) { }
return null;
}

/**
* @see InventoryView#convertSlot(int)
*/
public static @Nullable Integer convertSlot(InventoryView inventoryView, int rawSlot) {
if (CONVERT_SLOT == null)
return inventoryView.convertSlot(rawSlot);
try {
return (Integer) CONVERT_SLOT.invoke(inventoryView, rawSlot);
} catch (Throwable ignored) { }
return null;
}

/**
* @see InventoryView#getTopInventory()
*/
public static @Nullable Inventory getTopInventory(InventoryView inventoryView) {
if (GET_TOP_INVENTORY == null)
return inventoryView.getTopInventory();
try {
return (Inventory) GET_TOP_INVENTORY.invoke(inventoryView);
} catch (Throwable ignored) { }
return null;
}

/**
* @see InventoryView#getBottomInventory()
*/
public static @Nullable Inventory getBottomInventory(InventoryView inventoryView) {
if (GET_BOTTOM_INVENTORY == null)
return inventoryView.getBottomInventory();
try {
return (Inventory) GET_BOTTOM_INVENTORY.invoke(inventoryView);
} catch (Throwable ignored) { }
return null;
}

}
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void setMaxDamage(ItemStack itemStack, int maxDamage) {
public static void setDamage(ItemStack itemStack, int damage) {
ItemMeta meta = itemStack.getItemMeta();
if (meta instanceof Damageable) {
((Damageable) meta).setDamage(damage);
((Damageable) meta).setDamage(Math.max(0, damage));
itemStack.setItemMeta(meta);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public BlockValues getBlockValues(BlockState blockState) {
@Override
public @Nullable BlockValues getBlockValues(Material material) {
if (material.isBlock())
return new NewBlockValues(material, Bukkit.createBlockData(material), false);
return new NewBlockValues(material, Bukkit.createBlockData(material), true);
return null;
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,10 @@ public ItemStack parse(final String s, final ParseContext context) {
}

final ItemStack i = t.getRandom();
assert i != null;
if (i == null) {
Skript.error("'" + s + "' cannot represent an item");
return null;
}
return i;
}

Expand Down Expand Up @@ -1409,7 +1412,8 @@ public String toVariableNameString(FireworkEffect effect) {
if (BukkitUtils.registryExists("CAT_VARIANT")) {
catTypeClassInfo = new RegistryClassInfo<>(Cat.Type.class, Registry.CAT_VARIANT, "cattype", "cat types");
} else {
catTypeClassInfo = new EnumClassInfo<>(Cat.Type.class, "cattype", "cat types");
//noinspection unchecked, rawtypes - it is an enum on other versions
catTypeClassInfo = new EnumClassInfo<>((Class) Cat.Type.class, "cattype", "cat types");
}
Classes.registerClass(catTypeClassInfo
.user("cat ?(type|race)s?")
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.InventoryUtils;
import ch.njol.skript.command.CommandEvent;
import ch.njol.skript.events.bukkit.ScriptEvent;
import ch.njol.skript.events.bukkit.SkriptStartEvent;
Expand Down Expand Up @@ -1252,15 +1253,15 @@ public Slot[] get(InventoryDragEvent event) {
List<Slot> slots = new ArrayList<>(event.getRawSlots().size());
InventoryView view = event.getView();
for (Integer rawSlot : event.getRawSlots()) {
Inventory inventory = view.getInventory(rawSlot);
int slot = view.convertSlot(rawSlot);
if (inventory == null)
Inventory inventory = InventoryUtils.getInventory(view, rawSlot);
Integer slot = InventoryUtils.convertSlot(view, rawSlot);
if (inventory == null || slot == null)
continue;
// Not all indices point to inventory slots. Equipment, for example
if (inventory instanceof PlayerInventory && slot >= 36) {
slots.add(new ch.njol.skript.util.slot.EquipmentSlot(((PlayerInventory) view.getBottomInventory()).getHolder(), slot));
} else {
slots.add(new InventorySlot(inventory, view.convertSlot(rawSlot)));
slots.add(new InventorySlot(inventory, slot));
}
}
return slots.toArray(new Slot[0]);
Expand All @@ -1280,7 +1281,9 @@ public Inventory[] get(InventoryDragEvent event) {
Set<Inventory> inventories = new HashSet<>();
InventoryView view = event.getView();
for (Integer rawSlot : event.getRawSlots()) {
inventories.add(view.getInventory(rawSlot));
Inventory inventory = InventoryUtils.getInventory(view, rawSlot);
if (inventory != null)
inventories.add(inventory);
}
return inventories.toArray(new Inventory[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ public RegistrySerializer(Registry<R> registry) {
@Override
public @NotNull Fields serialize(R o) {
Fields fields = new Fields();
fields.putPrimitive("name", o.getKey().toString());
fields.putObject("name", o.getKey().toString());
return fields;
}

@Override
protected R deserialize(Fields fields) {
try {
String name = fields.getAndRemovePrimitive("name", String.class);
NamespacedKey namespacedKey;
if (!name.contains(":")) {
// Old variables
namespacedKey = NamespacedKey.minecraft(name);
} else {
namespacedKey = NamespacedKey.fromString(name);
}
if (namespacedKey == null)
return null;
return registry.get(namespacedKey);
} catch (StreamCorruptedException e) {
return null;
protected R deserialize(Fields fields) throws StreamCorruptedException {
String name = fields.getAndRemoveObject("name", String.class);
assert name != null;
NamespacedKey namespacedKey;
if (!name.contains(":")) {
// Old variables
namespacedKey = NamespacedKey.minecraft(name);
} else {
namespacedKey = NamespacedKey.fromString(name);
}
if (namespacedKey == null)
throw new StreamCorruptedException("Invalid namespacedkey: " + name);
R object = registry.get(namespacedKey);
if (object == null)
throw new StreamCorruptedException("Invalid object from registry: " + namespacedKey);
return object;
}

@Override
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/ch/njol/skript/conditions/CondIsPreferredTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
public boolean check(Event event) {
return blocks.check(event, block ->
items.check(event, item -> {
if (block instanceof Block) {
return ((Block) block).isPreferredTool(item.getRandom());
} else if (block instanceof BlockData) {
return ((BlockData) block).isPreferredTool(item.getRandom());
} else {
return false;
ItemStack stack = item.getRandom();
if (stack != null) {
if (block instanceof Block)
return ((Block) block).isPreferredTool(stack);
if (block instanceof BlockData)
return ((BlockData) block).isPreferredTool(stack);
}
return false;
}), isNegated());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/effects/EffBroadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

/**
* This effect will call {@link BroadcastMessageEvent} as of INSERT_VERSION.
* This effect will call {@link BroadcastMessageEvent} as of 2.9.0.
*/
@Override
@SuppressWarnings("deprecation")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/effects/EffOpenBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void execute(final Event e) {
ItemType itemType = book.getSingle(e);
if (itemType != null) {
ItemStack itemStack = itemType.getRandom();
if (itemStack.getType() == Material.WRITTEN_BOOK) {
if (itemStack != null && itemStack.getType() == Material.WRITTEN_BOOK) {
for (Player player : players.getArray(e)) {
player.openBook(itemStack);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ch/njol/skript/effects/EffReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ private void replace(Event event, Object[] needles, Expression<?> haystackExpr)

if (new ItemType(itemStack).isSimilar(needle)) {
ItemStack newItemStack = ((ItemType) replacement).getRandom();
newItemStack.setAmount(itemStack.getAmount());

inv.setItem(slot, newItemStack);
if (newItemStack != null) {
newItemStack.setAmount(itemStack.getAmount());
inv.setItem(slot, newItemStack);
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ch/njol/skript/effects/EffReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand Down Expand Up @@ -110,7 +109,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
protected TriggerItem walk(Event event) {
debug(event, false);
//noinspection rawtypes,unchecked
((ReturnHandler) handler).returnValues(value.getArray(event));
((ReturnHandler) handler).returnValues(event, value);

TriggerSection parent = getParent();
while (parent != null && parent != handler) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ch/njol/skript/entity/BoatChestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public boolean isSupertypeOf(EntityData<?> e) {
public boolean isOfItemType(ItemType itemType) {
int ordinal = -1;

ItemStack stack = itemType.getRandom();
Material type = stack.getType();
Material type = itemType.getMaterial();
if (type == Material.OAK_CHEST_BOAT)
ordinal = 0;
else if (type == Material.SPRUCE_CHEST_BOAT)
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/ch/njol/skript/entity/BoatData.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ public boolean isSupertypeOf(EntityData<?> e) {

public boolean isOfItemType(ItemType i){
int ordinal = -1;

ItemStack stack = i.getRandom();
Material type = stack.getType();

Material type = i.getMaterial();
if (type == Material.OAK_BOAT)
ordinal = 0;
else if (type == Material.SPRUCE_BOAT)
Expand Down
Loading

0 comments on commit 46f0d25

Please sign in to comment.