Skip to content

Commit

Permalink
Adds custom actions on InventoryButtons and childs.
Browse files Browse the repository at this point in the history
InventoryButtons do now process actions (forgot to do it when writing InventoryTrackers).
  • Loading branch information
anjoismysign committed Mar 14, 2024
1 parent bbf7dba commit 5da9ed3
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.09</version>
<version>1.698.10</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.09</version>
<version>1.698.10</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.09</version>
<version>1.698.10</version>
<packaging>pom</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.BlobLib;
import us.mytheria.bloblib.action.ActionType;
import us.mytheria.bloblib.api.BlobLibTranslatableAPI;
import us.mytheria.bloblib.entities.translatable.TranslatableItem;
import us.mytheria.bloblib.itemstack.ItemStackReader;
Expand Down Expand Up @@ -59,11 +61,25 @@ public static BlobMultiSlotable read(ConfigurationSection section, String key,
String action = null;
if (section.isString("Action"))
action = section.getString("Action");
ActionType actionType = null;
ConfigurationSection actionSection = section.getConfigurationSection("Action");
if (actionSection != null) {
if (!actionSection.isString("Action"))
Bukkit.getLogger().info("'Action' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action)");
if (!actionSection.isString("Action-Type"))
Bukkit.getLogger().info("'Action-Type' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action-Type)");
action = actionSection.getString("Action");
try {
actionType = ActionType.valueOf(actionSection.getString("Action-Type"));
} catch (IllegalArgumentException e) {
BlobLib.getAnjoLogger().singleError("Invalid 'ActionType' for " + key + ".Action.Action-Type");
}
}
ItemStack clone = translatableItem.getClone();
int amount = section.getInt("Amount", 1);
clone.setAmount(amount);
return new BlobMultiSlotable(list, clone, key, permission, price,
priceCurrency, action);
priceCurrency, action, actionType);
}
ConfigurationSection itemStackSection = section.getConfigurationSection("ItemStack");
if (itemStackSection == null) {
Expand All @@ -86,8 +102,22 @@ public static BlobMultiSlotable read(ConfigurationSection section, String key,
String action = null;
if (section.isString("Action"))
action = section.getString("Action");
ActionType actionType = null;
ConfigurationSection actionSection = section.getConfigurationSection("Action");
if (actionSection != null) {
if (!actionSection.isString("Action"))
Bukkit.getLogger().info("'Action' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action)");
if (!actionSection.isString("Action-Type"))
Bukkit.getLogger().info("'Action-Type' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action-Type)");
action = actionSection.getString("Action");
try {
actionType = ActionType.valueOf(actionSection.getString("Action-Type"));
} catch (IllegalArgumentException e) {
BlobLib.getAnjoLogger().singleError("Invalid 'ActionType' for " + key + ".Action.Action-Type");
}
}
return new BlobMultiSlotable(list, itemStack, key, permission, price,
priceCurrency, action);
priceCurrency, action, actionType);
}

/**
Expand All @@ -106,8 +136,9 @@ public BlobMultiSlotable(Set<Integer> slots, ItemStack itemStack,
@Nullable String permission,
double price,
@Nullable String priceCurrency,
@Nullable String action) {
super(slots, itemStack, permission, price, priceCurrency, action);
@Nullable String action,
@Nullable ActionType actionType) {
super(slots, itemStack, permission, price, priceCurrency, action, actionType);
this.key = key;
}

Expand All @@ -126,7 +157,7 @@ public void setInInventory(Inventory inventory) {

public InventoryButton toInventoryButton() {
return new InventoryButton(key, getSlots(), getPermission(), getPrice(),
getPriceCurrency(), getAction());
getPriceCurrency(), getAction(), getActionType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permissible;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.action.Action;
import us.mytheria.bloblib.action.ActionType;
import us.mytheria.bloblib.action.CommandAction;
import us.mytheria.bloblib.action.ConsoleCommandAction;
import us.mytheria.bloblib.api.BlobLibActionAPI;
import us.mytheria.bloblib.api.BlobLibEconomyAPI;
import us.mytheria.bloblib.vault.multieconomy.ElasticEconomy;
Expand All @@ -21,18 +25,25 @@ public class InventoryButton {
private final String permission;
private final double price;
private final String priceCurrency;
@Nullable
private final String action;
@Nullable
private final ActionType actionType;

public InventoryButton(String key, Set<Integer> slots,
public InventoryButton(String key,
@NotNull Set<Integer> slots,
@Nullable String permission,
double price, @Nullable String priceCurrency,
@Nullable String action) {
double price,
@Nullable String priceCurrency,
@Nullable String action,
@Nullable ActionType actionType) {
this.key = key;
this.slots = slots;
this.permission = permission;
this.price = price;
this.priceCurrency = priceCurrency;
this.action = action;
this.actionType = actionType;
}

public String getKey() {
Expand Down Expand Up @@ -142,6 +153,13 @@ public String getAction() {
return action;
}

/**
* @return The action type of the button. Null if there is no action type.
*/
@Nullable ActionType getActionType() {
return actionType;
}

/**
* Will handle the action of the button.
*
Expand All @@ -150,6 +168,20 @@ public String getAction() {
public void handleAction(Entity entity) {
if (action == null)
return;
if (actionType != null) {
Action<Entity> build;
switch (actionType) {
case ACTOR_COMMAND -> {
build = CommandAction.build(action);
}
case CONSOLE_COMMAND -> {
build = ConsoleCommandAction.build(action);
}
default -> throw new IllegalStateException("Unexpected value: " + actionType);
}
build.perform(entity);
return;
}
Action<Entity> fetch = BlobLibActionAPI.getInstance().getAction(action);
fetch.perform(entity);
}
Expand All @@ -165,6 +197,6 @@ public void accept(ButtonVisitor visitor) {
*/
public InventoryButton copy() {
return new InventoryButton(key, new HashSet<>(slots),
permission, price, priceCurrency, action);
permission, price, priceCurrency, action, actionType);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.mytheria.bloblib.entities.inventory;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -138,6 +139,8 @@ public void processClickEvent(InventoryClickEvent event, T button) {
clickEvents.values().forEach(clickEvent -> {
if (clickEvent == null)
return;
if (!button.handleAll((Player) event.getWhoClicked()))
return;
clickEvent.accept(event, button);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.BlobLib;
import us.mytheria.bloblib.action.ActionType;
import us.mytheria.bloblib.api.BlobLibTranslatableAPI;
import us.mytheria.bloblib.entities.translatable.TranslatableItem;
import us.mytheria.bloblib.itemstack.ItemStackReader;
Expand Down Expand Up @@ -68,11 +70,25 @@ public static MetaBlobMultiSlotable read(ConfigurationSection section, String ke
String action = null;
if (section.isString("Action"))
action = section.getString("Action");
ActionType actionType = null;
ConfigurationSection actionSection = section.getConfigurationSection("Action");
if (actionSection != null) {
if (!actionSection.isString("Action"))
Bukkit.getLogger().info("'Action' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action)");
if (!actionSection.isString("Action-Type"))
Bukkit.getLogger().info("'Action-Type' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action-Type)");
action = actionSection.getString("Action");
try {
actionType = ActionType.valueOf(actionSection.getString("Action-Type"));
} catch (IllegalArgumentException e) {
BlobLib.getAnjoLogger().singleError("Invalid 'ActionType' for " + key + ".Action.Action-Type");
}
}
ItemStack clone = translatableItem.getClone();
int amount = section.getInt("Amount", 1);
clone.setAmount(amount);
return new MetaBlobMultiSlotable(set, clone, key, meta, subMeta,
permission, price, priceCurrency, action);
permission, price, priceCurrency, action, actionType);
}
ConfigurationSection itemStackSection = section.getConfigurationSection("ItemStack");
if (itemStackSection == null) {
Expand Down Expand Up @@ -102,8 +118,22 @@ public static MetaBlobMultiSlotable read(ConfigurationSection section, String ke
String action = null;
if (section.isString("Action"))
action = section.getString("Action");
ActionType actionType = null;
ConfigurationSection actionSection = section.getConfigurationSection("Action");
if (actionSection != null) {
if (!actionSection.isString("Action"))
Bukkit.getLogger().info("'Action' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action)");
if (!actionSection.isString("Action-Type"))
Bukkit.getLogger().info("'Action-Type' field is missing in 'Action' ConfigurationSection (" + key + ".Action.Action-Type)");
action = actionSection.getString("Action");
try {
actionType = ActionType.valueOf(actionSection.getString("Action-Type"));
} catch (IllegalArgumentException e) {
BlobLib.getAnjoLogger().singleError("Invalid 'ActionType' for " + key + ".Action.Action-Type");
}
}
return new MetaBlobMultiSlotable(set, itemStack, key, meta, subMeta,
permission, price, priceCurrency, action);
permission, price, priceCurrency, action, actionType);
}

/**
Expand All @@ -118,8 +148,9 @@ public MetaBlobMultiSlotable(Set<Integer> slots, ItemStack itemStack, String key
@Nullable String permission,
double price,
@Nullable String priceCurrency,
@Nullable String action) {
super(slots, itemStack, permission, price, priceCurrency, action);
@Nullable String action,
@Nullable ActionType actionType) {
super(slots, itemStack, permission, price, priceCurrency, action, actionType);
this.key = key;
this.meta = meta;
this.subMeta = subMeta;
Expand All @@ -140,7 +171,8 @@ public void setInInventory(Inventory inventory) {

public MetaInventoryButton toMetaInventoryButton() {
return new MetaInventoryButton(key, getSlots(), meta, subMeta,
getPermission(), getPrice(), getPriceCurrency(), getAction());
getPermission(), getPrice(), getPriceCurrency(),
getAction(), getActionType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.action.ActionType;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -14,14 +15,14 @@ public static MetaInventoryButton fromInventoryButton(InventoryButton button, St
String subMeta) {
return new MetaInventoryButton(button.getKey(), button.getSlots(), meta,
subMeta, button.getPermission(), button.getPrice(),
button.getPriceCurrency(), button.getAction());
button.getPriceCurrency(), button.getAction(), button.getActionType());
}

public MetaInventoryButton(String key, Set<Integer> slots, String meta,
@Nullable String subMeta, @Nullable String permission,
double price, @Nullable String priceCurrency,
@Nullable String action) {
super(key, slots, permission, price, priceCurrency, action);
@Nullable String action, @Nullable ActionType actionType) {
super(key, slots, permission, price, priceCurrency, action, actionType);
this.meta = meta;
this.subMeta = subMeta;
}
Expand Down Expand Up @@ -62,6 +63,6 @@ public void accept(ButtonVisitor visitor) {
@Override
public MetaInventoryButton copy() {
return new MetaInventoryButton(getKey(), new HashSet<>(getSlots()), getMeta(), getSubMeta(),
getPermission(), getPrice(), getPriceCurrency(), getAction());
getPermission(), getPrice(), getPriceCurrency(), getAction(), getActionType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.action.ActionType;

import java.util.Collection;

Expand All @@ -18,7 +19,10 @@ public abstract class MultiSlotable {
private final String permission;
private final double price;
private final String priceCurrency;
@Nullable
private final String action;
@Nullable
private final ActionType actionType;

/**
* @param slots The slots to be used.
Expand All @@ -27,18 +31,21 @@ public abstract class MultiSlotable {
* @param price The price to be used.
* @param priceCurrency The price currency to be used.
* @param action The action to be used.
* @param actionType The action type to be used.
*/
public MultiSlotable(Collection<Integer> slots, ItemStack itemStack,
@Nullable String permission,
double price,
@Nullable String priceCurrency,
@Nullable String action) {
@Nullable String action,
@Nullable ActionType actionType) {
this.slots = slots;
this.itemStack = itemStack;
this.permission = permission;
this.price = price;
this.priceCurrency = priceCurrency;
this.action = action;
this.actionType = actionType;
}

/**
Expand Down Expand Up @@ -75,4 +82,9 @@ public String getPriceCurrency() {
public String getAction() {
return action;
}

@Nullable
public ActionType getActionType() {
return actionType;
}
}

0 comments on commit 5da9ed3

Please sign in to comment.