Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Asintotoo authored Jul 20, 2024
1 parent 2271ff8 commit 2d0c773
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.asintoto</groupId>
<artifactId>Basic</artifactId>
<version>1.2.7</version>
<version>1.2.8</version>
<packaging>jar</packaging>

<name>Basic</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,26 @@ public void onClick(InventoryClickEvent e) {

PersistentDataContainer itemData = item.getItemMeta().getPersistentDataContainer();

boolean isUnocked = itemData.has(BasicKeys.BUTTON_TYPE_UNLOCKED, PersistentDataType.BOOLEAN);
Menu menu = MenuManager.getPlayerMenu(p);

if(menu == null) return;

int rawSlot = e.getRawSlot();
int slot = e.getSlot();

if(menu.hasPlayerInventoryProtection()) {
if(isInventoryClick(slot, rawSlot, menu)) {
return;
}
}

if(!itemData.has(BasicKeys.BUTTON_IS_BUTTON, PersistentDataType.BOOLEAN)) {
return;
}

boolean isUlnocked = itemData.has(BasicKeys.BUTTON_TYPE_UNLOCKED, PersistentDataType.BOOLEAN);

if(!isUnocked) {
if(!isUlnocked) {
e.setCancelled(true);
}

Expand All @@ -44,12 +61,19 @@ public void onClick(InventoryClickEvent e) {
return;
}

int slot = e.getRawSlot();
if(isValidSlot(rawSlot, menu)) {
menu.onClick(p, rawSlot, e.getAction());
}
}

Menu menu = MenuManager.getPlayerMenu(p);
private boolean isValidSlot(int rawSlot, Menu menu) {
return rawSlot >= 0 && rawSlot <= menu.getSize() - 1;
}

if(slot >= 0 && slot <= menu.getSize() - 1) {
menu.onClick(p, slot, e.getAction());
}
private boolean isInventoryClick(int slot, int rawSlot, Menu menu) {
if(!isValidSlot(rawSlot, menu)) return false;
if(slot < 0 || slot > 35) return false;
if(rawSlot == slot) return false;
return true;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/asintoto/basic/menu/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public Button(ItemStack item, ButtonType buttonType) {

this.item.setItemMeta(meta);
}

ItemMeta meta = this.item.getItemMeta();

meta.getPersistentDataContainer().set(BasicKeys.BUTTON_IS_BUTTON,
PersistentDataType.BOOLEAN, true);

this.item.setItemMeta(meta);
}

public Button(ItemCreator item, ButtonType buttonType) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/asintoto/basic/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

Expand All @@ -23,6 +24,7 @@ public class Menu {
private String title;
private Menu prevMenu;
private Map<Integer, Button> buttonSlotMap;
private boolean playerInventoryProtection;

public Menu(int size, String title) {
this.title = ColorLib.setColors(title);
Expand All @@ -36,6 +38,16 @@ public Menu(int size, String title) {

this.prevMenu = null;

this.playerInventoryProtection = true;

}

public boolean hasPlayerInventoryProtection() {
return playerInventoryProtection;
}

public void setPlayerInventoryProtection(boolean playerInventoryProtection) {
this.playerInventoryProtection = playerInventoryProtection;
}

public void setInventory(Inventory inventory) {
Expand Down Expand Up @@ -88,6 +100,12 @@ public Button getButtonAtSlot(int slot) {
return null;
}

public ItemStack getRawItem(int slot) {
ItemStack temp = inventory.getItem(slot);
if(temp != null) return new ItemStack(temp.getType());
return null;
}

public void onClick(Player p, int slot, InventoryAction action) {

}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/asintoto/basic/utils/BasicKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class BasicKeys {
public static final NamespacedKey BUTTON_TYPE_CLOSE = new NamespacedKey(Basic.getPlugin(), "button_type_close");
public static final NamespacedKey BUTTON_TYPE_UNLOCKED = new NamespacedKey(Basic.getPlugin(), "button_type_unlocked");
public static final NamespacedKey BUTTON_TYPE_PREV = new NamespacedKey(Basic.getPlugin(), "button_type_prev");
public static final NamespacedKey BUTTON_IS_BUTTON = new NamespacedKey(Basic.getPlugin(), "button_is_button");
}

0 comments on commit 2d0c773

Please sign in to comment.