Make actions functional

This commit is contained in:
zontreck 2025-04-06 11:34:28 -07:00
parent 4acb0baa7a
commit 84a8c1bc58
5 changed files with 188 additions and 0 deletions

View file

@ -1,5 +1,7 @@
package dev.zontreck.ase;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -8,6 +10,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import dev.zontreck.ase.guis.ChestGUI;
import dev.zontreck.ase.guis.PrimitiveAction;
import dev.zontreck.ase.guis.PrimitiveItem;
public class EventsHandler implements Listener {
@ -38,6 +41,12 @@ public class EventsHandler implements Listener {
// Check if clicked item is a GUI button
if (PrimitiveItem.isGUIButton(ice.getCurrentItem())) {
ice.setCancelled(true);
List<PrimitiveAction> actions = PrimitiveAction.getActions(ice.getCurrentItem());
for (PrimitiveAction primitiveAction : actions) {
primitiveAction.handleAction(player);
}
}
}
}

View file

@ -11,6 +11,7 @@ import dev.zontreck.ase.homes.Home;
public class SavedData {
public Map<String, Home> homes;
// TODO: Add warps
public SavedData() {
homes = new HashMap<>();

View file

@ -18,6 +18,8 @@ public class CreditsGui {
Component.text("Username: zontreck", NamedTextColor.DARK_PURPLE),
Component.text("Creator of ASE", NamedTextColor.DARK_PURPLE));
zontreckHead.skull("zontreck");
zontreckHead.addAction(new PrimitiveAction(PrimitiveAction.SEND_MESSAGE, PrimitiveAction
.serializeComponent(Component.text("I tried to steal zontreck's head!", NamedTextColor.DARK_RED))));
gui.fill(9, StainedGlass);
gui.add(StainedGlass);

View file

@ -0,0 +1,169 @@
package dev.zontreck.ase.guis;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import dev.zontreck.ase.AriasServerEssentials;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.title.Title;
import net.kyori.adventure.title.Title.Times;
public class PrimitiveAction {
public static final int NONE = 0;
public static final int OPEN_GUI = 1;
public static final int CLOSE_GUI = 2;
public static final int SEND_MESSAGE = 3;
public static final int SEND_COMMAND = 4;
public static final int SEND_ACTIONBAR = 5;
public static final int SEND_TITLE = 6;
public static final int SEND_SOUND = 7;
public static final int SEND_PARTICLE = 8;
public static final int SEND_EFFECT = 9;
public static final int SEND_SYSTEM_COMMAND = 10;
public static final String ACTION_TYPE = "action_type";
public static final String ACTION_DATA = "action_data";
public static final String ACTIONS = "actions";
public int actionType;
public String actionData;
public PrimitiveAction(int actionType, String actionData) {
this.actionType = actionType;
this.actionData = actionData;
}
public PrimitiveAction(int actionType) {
this.actionType = actionType;
this.actionData = "";
}
// Serialize a Component to a string using LegacyComponentSerializer
public static String serializeComponent(Component component) {
return LegacyComponentSerializer.legacySection().serialize(component);
}
// Deserialize a string back to a Component
public static Component deserializeComponent(String componentString) {
return LegacyComponentSerializer.legacySection().deserialize(componentString);
}
// Handle the action based on the action type and action data
public void handleAction(Player player) {
switch (actionType) {
case NONE:
return;
case OPEN_GUI:
// Handle opening a GUI (implement your logic here)
break;
case CLOSE_GUI:
player.closeInventory();
break;
case SEND_MESSAGE:
// Send a message to the player
player.sendMessage(deserializeComponent(actionData));
break;
case SEND_COMMAND:
// Execute a command (you can execute the command here)
player.performCommand(actionData);
break;
case SEND_SYSTEM_COMMAND:
// Executes a system command, ignores player's permissions.
player.getServer().dispatchCommand(null, actionData);
break;
case SEND_ACTIONBAR:
// Send an action bar message (You can use a plugin like Adventure API for this)
player.sendActionBar(deserializeComponent(actionData));
break;
case SEND_TITLE:
// Send a title message
player.showTitle(Title.title(deserializeComponent(actionData), Component.empty(),
Times.times(Duration.ofSeconds(1), Duration.ofSeconds(5), Duration.ofSeconds(1))));
break;
case SEND_SOUND:
// Play a sound to the player
// Example: player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP,
// 1, 1);
break;
case SEND_PARTICLE:
// Send a particle effect to the player
// Example: player.spawnParticle(Particle.EXPLOSION_LARGE, player.getLocation(),
// 1);
break;
case SEND_EFFECT:
// Apply a potion effect (can be customized)
// Example: player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200,
// 1));
break;
}
}
// Attach a list of actions to an item
public static ItemStack attachActions(List<PrimitiveAction> actions, ItemStack item) {
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return item; // No meta available
}
// Serialize the list of actions into a string
StringBuilder serializedActions = new StringBuilder();
for (PrimitiveAction action : actions) {
String actionString = action.actionType + ":" + action.actionData;
serializedActions.append(actionString).append(";");
}
// Store the serialized actions as lore or in a custom tag (like NBT)
PersistentDataContainer data = meta.getPersistentDataContainer();
data.set(NamespacedKey.fromString(ACTIONS, AriasServerEssentials.getSelf()), PersistentDataType.STRING,
serializedActions.toString());
item.setItemMeta(meta);
return item;
}
// Get the actions stored in the item's PersistentDataContainer
public static List<PrimitiveAction> getActions(ItemStack item) {
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return new ArrayList<>(); // No meta available
}
PersistentDataContainer data = meta.getPersistentDataContainer();
String actionsData = data.get(NamespacedKey.fromString(ACTIONS, AriasServerEssentials.getSelf()),
PersistentDataType.STRING);
if (actionsData == null || actionsData.isEmpty()) {
return new ArrayList<>(); // No actions stored
}
List<PrimitiveAction> actions = new ArrayList<>();
String[] actionsArray = actionsData.split(";");
for (String actionString : actionsArray) {
if (!actionString.isEmpty()) {
String[] actionParts = actionString.split(":");
if (actionParts.length == 2) {
try {
int actionType = Integer.parseInt(actionParts[0]);
String actionData = actionParts[1];
actions.add(new PrimitiveAction(actionType, actionData));
} catch (NumberFormatException e) {
// Handle invalid action type (logging, skipping, etc.)
continue;
}
}
}
}
return actions;
}
}

View file

@ -26,6 +26,7 @@ public class PrimitiveItem {
public boolean isGUIButton;
public boolean isSkull = false;
public String skullOwner = "";
public List<PrimitiveAction> actions = new ArrayList<>();
public PrimitiveItem(Material type, Component name, boolean isGUIButton, Component... lore) {
itemType = type;
@ -41,6 +42,10 @@ public class PrimitiveItem {
this.isGUIButton = isGUIButton;
}
public void addAction(PrimitiveAction action) {
actions.add(action);
}
public ItemStack generate() {
ItemStack item = new ItemStack(itemType, 1);
@ -68,6 +73,8 @@ public class PrimitiveItem {
}
item.setItemMeta(meta);
item = PrimitiveAction.attachActions(actions, item);
return item;
}