Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equippable components #7194

Open
wants to merge 9 commits into
base: dev/feature
Choose a base branch
from
44 changes: 44 additions & 0 deletions src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public ItemStack get(final EntityEquipment e) {
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
e.setItemInMainHand(item);
}

@Override
public org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot() {
return org.bukkit.inventory.EquipmentSlot.HAND;
}
},
OFF_HAND(40) {

Expand All @@ -61,6 +66,11 @@ public ItemStack get(EntityEquipment e) {
public void set(EntityEquipment e, @Nullable ItemStack item) {
e.setItemInOffHand(item);
}

@Override
public org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot() {
return org.bukkit.inventory.EquipmentSlot.OFF_HAND;
}

},
HELMET(39) {
Expand All @@ -74,6 +84,11 @@ public ItemStack get(final EntityEquipment e) {
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
e.setHelmet(item);
}

@Override
public org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot() {
return org.bukkit.inventory.EquipmentSlot.HEAD;
}
},
CHESTPLATE(38) {
@Override
Expand All @@ -86,6 +101,11 @@ public ItemStack get(final EntityEquipment e) {
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
e.setChestplate(item);
}

@Override
public org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot() {
return org.bukkit.inventory.EquipmentSlot.CHEST;
}
},
LEGGINGS(37) {
@Override
Expand All @@ -98,6 +118,11 @@ public ItemStack get(final EntityEquipment e) {
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
e.setLeggings(item);
}

@Override
public org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot() {
return org.bukkit.inventory.EquipmentSlot.LEGS;
}
},
BOOTS(36) {
@Override
Expand All @@ -110,6 +135,11 @@ public ItemStack get(final EntityEquipment e) {
public void set(final EntityEquipment e, final @Nullable ItemStack item) {
e.setBoots(item);
}

@Override
public org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot() {
return org.bukkit.inventory.EquipmentSlot.FEET;
}
};

public final int slotNumber;
Expand All @@ -126,6 +156,8 @@ public void set(final EntityEquipment e, final @Nullable ItemStack item) {
public abstract ItemStack get(EntityEquipment e);

public abstract void set(EntityEquipment e, @Nullable ItemStack item);

public abstract org.bukkit.inventory.EquipmentSlot getBukkitEquipSlot();

}

Expand Down Expand Up @@ -204,6 +236,18 @@ public int getIndex() {
return slotIndex != -1 ? slotIndex : slot.slotNumber;
}

public static EquipSlot convertToSkriptEquipSlot(org.bukkit.inventory.EquipmentSlot bukkitSlot) {
return switch (bukkitSlot) {
case HEAD -> EquipSlot.HELMET;
case CHEST -> EquipSlot.CHESTPLATE;
case LEGS -> EquipSlot.LEGGINGS;
case FEET -> EquipSlot.BOOTS;
case HAND -> EquipSlot.TOOL;
case OFF_HAND -> EquipSlot.OFF_HAND;
default -> null;
};
}

@Override
public String toString(@Nullable Event event, boolean debug) {
if (slotToString) // Slot to string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.skriptlang.skript.bukkit.equippablecomponents;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.EnumClassInfo;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.slot.EquipmentSlot.EquipSlot;
import org.bukkit.inventory.meta.components.EquippableComponent;

import java.io.IOException;

public class EquippableComponentModule {

/*
TODO:
Add changing data of equippable components to Exprs + Effs
Add equippable components to Conds
Tests (AHHHHHH)
Docs
*/


public static void load() throws IOException {
if (!Skript.classExists("org.bukkit.inventory.meta.components.EquippableComponent"))
return;

Skript.getAddonInstance().loadClasses("org.skriptlang.skript.bukkit.equippablecomponents", "elements");

Classes.registerClass(new EnumClassInfo<>(EquipSlot.class, "equipmentslot", "equipment slot")
.user("equipment ?slots?")
.name("Equipment Slot")
.description("Represents an equipment slot")
.since("INSERT VERSION")
);

Classes.registerClass(new ClassInfo<>(EquippableComponent.class, "equippablecomponent")
.user("equippable ?components?")
.name("Equippable Components")
.description("Represents an equippable component used for items.")
.requiredPlugins("Minecraft 1.21.2+")
.since("INSERT VERSION")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.skriptlang.skript.bukkit.equippablecomponents.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

@Name("Equippable Component Can Take Damage")
@Description("Checks if the items can take damage when the entity wearing them gets hurt.")
@Examples({
"if the equippable component of diamond chestplate can take damage:",
"\t"
})
public class CondEquipCompDamage extends Condition {

static {
Skript.registerCondition(CondEquipCompDamage.class, ConditionType.PROPERTY,
"[the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% (can take|allow) damage",
"[the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% (can't take|can not take|disallow) damage");
}

private Expression<?> objects;
private boolean damage;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
objects = exprs[0];
damage = matchedPattern == 0;
setNegated(!damage);
return true;
}

@Override
public boolean check(Event event) {
boolean finalProvocation = false;
for (Object object : objects.getArray(event)) {
ItemStack itemStack = ItemUtils.asItemStack(object);
if (itemStack == null)
continue;
if (itemStack.getItemMeta().getEquippable().isDamageOnHurt() == damage) {
finalProvocation = true;
} else {
finalProvocation = false;
break;
}
}
return finalProvocation;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the equippable components of " + objects.toString(event, debug)
+ (isNegated() ? "can not" : "can") + " take damage";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.skriptlang.skript.bukkit.equippablecomponents.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

public class CondEquipCompDispensable extends Condition {

static {
Skript.registerCondition(CondEquipCompDispensable.class, ConditionType.PROPERTY,
"[the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% (is|are) [:un]dispensable",
"[the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% (isn't|is not|aren't|are not) [:un]dispensable");
}

private Expression<?> objects;
private boolean dispensable;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
objects = exprs[0];
dispensable = !parseResult.hasTag("un");
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(Event event) {
boolean finalProvocation = false;
for (Object object : objects.getArray(event)) {
ItemStack itemStack = ItemUtils.asItemStack(object);
if (itemStack == null)
continue;
if (itemStack.getItemMeta().getEquippable().isDispensable() == dispensable) {
finalProvocation = true;
} else {
finalProvocation = false;
break;
}
}
return finalProvocation;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the equippable components of " + objects.toString(event, debug)
+ (isNegated() ? "are not" : "are") + (dispensable ? "dispensable" : "undispensable");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.skriptlang.skript.bukkit.equippablecomponents.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

public class CondEquipCompSwappable extends Condition {

static {
Skript.registerCondition(CondEquipCompSwappable.class, ConditionType.PROPERTY,
"[the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% (is|are) [:un]swappable",
"[the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% (isn't|is not|aren't|are not) [:un]swappable");
}

private Expression<?> objects;
private boolean swappable;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
objects = exprs[0];
swappable = !parseResult.hasTag("un");
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(Event event) {
boolean finalProvocation = false;
for (Object object : objects.getArray(event)) {
ItemStack itemStack = ItemUtils.asItemStack(object);
if (itemStack == null)
continue;
if (itemStack.getItemMeta().getEquippable().isSwappable() == swappable) {
finalProvocation = true;
} else {
finalProvocation = false;
break;
}
}
return finalProvocation;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the equippable components of " + objects.toString(event, debug)
+ (isNegated() ? "are not" : "are") + (swappable ? "swappable" : "unswappable");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.skriptlang.skript.bukkit.equippablecomponents.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.slot.Slot;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Nullable;

public class EffEquipCompDamage extends Effect {

static {
Skript.registerEffect(EffEquipCompDamage.class,
"(set|make|force) [the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% [to] (allow|take) damage",
"(set|make|force) [the] [equip[pable] component[s] of] %itemstacks/itemtypes/slots% [to] (disallow|not take) damage");
}

private Expression<?> objects;
private boolean damage;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
objects = exprs[0];
damage = matchedPattern == 0;
return false;
}

@Override
protected void execute(Event event) {
for (Object object : objects.getArray(event)) {
ItemStack itemStack = ItemUtils.asItemStack(object);
if (itemStack == null)
continue;
ItemMeta meta = itemStack.getItemMeta();
meta.getEquippable().setDamageOnHurt(damage);
itemStack.setItemMeta(meta);
if (object instanceof Slot slot) {
slot.setItem(itemStack);
} else if (object instanceof ItemType itemType) {
itemType.setItemMeta(meta);
} else if (object instanceof ItemStack itemStack1) {
itemStack1.setItemMeta(meta);
}
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "set the equippable components of " + objects.toString(event, debug) + " to "
+ (damage ? "take damage" : "not take damage");
}

}
Loading