Skip to content

Commit

Permalink
Merge pull request #317 from rockyhawk64/1.8.8_TO_1.20.4
Browse files Browse the repository at this point in the history
3.21.2.1
  • Loading branch information
rockyhawk64 authored May 8, 2024
2 parents 43dbaaf + 43d1b22 commit ad25df2
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 94 deletions.
2 changes: 1 addition & 1 deletion resource/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 3.21.2.0
version: 3.21.2.1
main: me.rockyhawk.commandpanels.CommandPanels
name: CommandPanels
author: RockyHawk
Expand Down
9 changes: 9 additions & 0 deletions src/me/rockyhawk/commandpanels/CommandPanels.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import me.rockyhawk.commandpanels.panelblocks.PanelBlockOnClick;
import me.rockyhawk.commandpanels.playerinventoryhandler.InventorySaver;
import me.rockyhawk.commandpanels.playerinventoryhandler.ItemStackSerializer;
import me.rockyhawk.commandpanels.playerinventoryhandler.pickupevent.EntityPickupEvent;
import me.rockyhawk.commandpanels.playerinventoryhandler.pickupevent.legacyPlayerEvent;
import me.rockyhawk.commandpanels.updater.Updater;
import net.milkbowl.vault.economy.Economy;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -179,7 +181,14 @@ public void onEnable() {
Objects.requireNonNull(this.getCommand("commandpanelversion")).setExecutor(new Commandpanelversion(this));
Objects.requireNonNull(this.getCommand("commandpanellist")).setExecutor(new Commandpanelslist(this));
this.getServer().getPluginManager().registerEvents(new Utils(this), this);

this.getServer().getPluginManager().registerEvents(inventorySaver, this);
if(this.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_12)){
this.getServer().getPluginManager().registerEvents(new EntityPickupEvent(this), this);
}else{
this.getServer().getPluginManager().registerEvents(new legacyPlayerEvent(this), this);
}

this.getServer().getPluginManager().registerEvents(inputUtils, this);
this.getServer().getPluginManager().registerEvents(new UtilsPanelsLoader(this), this);
this.getServer().getPluginManager().registerEvents(new GenUtils(this), this);
Expand Down
15 changes: 12 additions & 3 deletions src/me/rockyhawk/commandpanels/classresources/ItemCreation.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,13 @@ public boolean isIdentical(ItemStack one, ItemStack two, Boolean nbtCheck){
}
}
}catch(Exception ignore){}
//check for ID 1.12.2 and below
try {
if (plugin.legacy.MAJOR_VERSION.lessThanOrEqualTo(MinecraftVersions.v1_12) &&
(one.getDurability() != two.getDurability())) {
return false;
}
}catch(Exception ignore){}
//check for lore
try {
if (!one.getItemMeta().getLore().equals(two.getItemMeta().getLore())) {
Expand All @@ -512,9 +519,11 @@ public boolean isIdentical(ItemStack one, ItemStack two, Boolean nbtCheck){
}catch(Exception ignore){}
//check for custom model data
try {
if (one.getItemMeta().getCustomModelData() != (two.getItemMeta().getCustomModelData())) {
if(one.getItemMeta().hasCustomModelData()) {
return false;
if (plugin.legacy.MAJOR_VERSION.greaterThanOrEqualTo(MinecraftVersions.v1_14)){
if (one.getItemMeta().getCustomModelData() != (two.getItemMeta().getCustomModelData())) {
if(one.getItemMeta().hasCustomModelData()) {
return false;
}
}
}
}catch(Exception ignore){}
Expand Down
137 changes: 63 additions & 74 deletions src/me/rockyhawk/commandpanels/commandtags/paywalls/ItemPaywall.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.rockyhawk.commandpanels.commandtags.PaywallOutput;
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
Expand All @@ -22,19 +23,13 @@ public void commandTag(PaywallEvent e){
if(e.name.equalsIgnoreCase("item-paywall=")){
//if player uses item-paywall= [Material] [Amount] <id:#> <IGNORENBT> WILL NOT TAKE CUSTOM ITEMS. IGNORENBT lets nbt items through. Useful for spawner edge cases.
//player can use item-paywall= [custom-item] [Amount]
List<ItemStack> cont = new ArrayList<>(Arrays.asList(plugin.inventorySaver.getNormalInventory(e.p)));
HashMap<Integer, ItemStack> remCont = new HashMap<>();
try {
int id = -1;
boolean ignoreNBT = false;
for (String val : e.args) {
//item ID for legacy minecraft versions
if (val.startsWith("id:")) {
id = Integer.parseInt(val.substring(3));
}
//This is here for when people want to take nbt items like spawners with types in a check for spawners.
if(val.equals("IGNORENBT")){
if (val.equals("IGNORENBT")) {
ignoreNBT = true;
break;
}
}

Expand All @@ -48,79 +43,19 @@ public void commandTag(PaywallEvent e){
//If normal just set material.
sellItem = new ItemStack(Objects.requireNonNull(Material.matchMaterial(e.args[0])), Integer.parseInt(e.args[1]));
}
//this is not a boolean because it needs to return an int
PaywallOutput removedItem = PaywallOutput.Blocked;

int remainingAmount = sellItem.getAmount();
//loop through items in the inventory
for (int f = 0; f < 36; f++) {

if (cont.get(f) == null) {
//skip slot if empty
continue;
}

ItemStack itm = cont.get(f); //Get item/slot

//Check if the item matches the id set. If not continue to next in loop.
if (id != -1 && itm.getDurability() != id) {
continue;
}

if(plugin.itemCreate.isIdentical(sellItem,itm, !ignoreNBT)){ //Check if both sell item and item in slot are identical
//Adding item to the remove list then checking if we have reached the required amount.
ItemStack add = new ItemStack(itm.getType(), itm.getAmount());
remainingAmount -= add.getAmount();
if (e.doDelete) remCont.put(f,add);
if (remainingAmount <= 0) {
removedItem = PaywallOutput.Passed;
break;
}
}
}

if (remainingAmount <= 0) {
//If we have reached the end of the paywall.
for(Map.Entry<Integer, ItemStack> entry : remCont.entrySet()) {
ItemStack remItem = entry.getValue();

//Check if it's the last item in the loop and only subtract the remaining amount.
if (sellItem.getAmount() < remItem.getAmount()) {
if (plugin.inventorySaver.hasNormalInventory(e.p)) {
if (e.doDelete)
//Normal inventory
e.p.getInventory().getItem(entry.getKey()).setAmount(remItem.getAmount() - sellItem.getAmount());
e.p.updateInventory();
} else {
if (e.doDelete)
//Saved inventory
cont.get(entry.getKey()).setAmount(remItem.getAmount() - sellItem.getAmount());
plugin.inventorySaver.inventoryConfig.set(e.p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(cont.toArray(new ItemStack[0])));
}
} else { //If its anywhere but the last in loop just get rid of the items.
if (plugin.inventorySaver.hasNormalInventory(e.p)) {
if (e.doDelete)
//Normal inventory
e.p.getInventory().setItem(entry.getKey(), null);
e.p.updateInventory();
} else {
if (e.doDelete)
//Saved inventory
cont.remove(entry.getValue());
plugin.inventorySaver.inventoryConfig.set(e.p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(cont.toArray(new ItemStack[0])));
}
}

if (e.doDelete) sellItem.setAmount(sellItem.getAmount() - remItem.getAmount());
//try to remove the item and determine outcome
PaywallOutput removedItem = PaywallOutput.Blocked;
if (e.doDelete){
if(removeItem(e.p, sellItem, ignoreNBT)){
removedItem = PaywallOutput.Passed;
}

removedItem = PaywallOutput.Passed;
}

//send message and return
if (removedItem == PaywallOutput.Blocked) {
if (plugin.config.getBoolean("purchase.item.enable")) {
//no item was found
//no item found to remove
plugin.tex.sendString(e.panel, PanelPosition.Top, e.p, Objects.requireNonNull(plugin.config.getString("purchase.item.failure")));
}
} else {
Expand All @@ -137,4 +72,58 @@ public void commandTag(PaywallEvent e){
}
}
}

public boolean removeItem(Player p, ItemStack itemToRemove, boolean ignoreNBT) {
boolean result;

if (plugin.inventorySaver.hasNormalInventory(p)) {
result = removeItemFromInventory(p.getInventory().getContents(), itemToRemove, ignoreNBT);
} else {
// Load the saved inventory from config, manipulate it, and save it back
ItemStack[] savedInventory = plugin.inventorySaver.getNormalInventory(p);
result = removeItemFromInventory(savedInventory, itemToRemove, ignoreNBT);
plugin.inventorySaver.inventoryConfig.set(p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(savedInventory));
}

return result; // Return true if the items were successfully removed, otherwise false
}

private boolean removeItemFromInventory(ItemStack[] inventory, ItemStack itemToRemove, boolean ignoreNBT) {
int amountToRemove = itemToRemove.getAmount();
int count = 0; // To count how many of the required items are present

// First pass: count the items to ensure there are enough
for (ItemStack item : inventory) {
if (item != null && plugin.itemCreate.isIdentical(item, itemToRemove, !ignoreNBT)) {
count += item.getAmount();
}
}

// If not enough items, return false and do not modify the inventory
if (count < amountToRemove) {
return false;
}

// Second pass: remove the items if there are enough
for (int i = 0; i < inventory.length; i++) {
ItemStack currentItem = inventory[i];
if (currentItem != null && plugin.itemCreate.isIdentical(currentItem, itemToRemove, !ignoreNBT)) {
int removeAmount = Math.min(currentItem.getAmount(), amountToRemove);
currentItem.setAmount(currentItem.getAmount() - removeAmount);
amountToRemove -= removeAmount;

// Remove the item stack if it becomes empty
if (currentItem.getAmount() == 0) {
inventory[i] = null;
}

// If removed all needed, break out of the loop
if (amountToRemove == 0) {
break;
}
}
}

return true; // Return true as items were successfully removed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import me.rockyhawk.commandpanels.api.PanelOpenedEvent;
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
Expand Down Expand Up @@ -75,19 +73,6 @@ public void onDeath(PlayerDeathEvent e){
}
}

@EventHandler
public void onPickup(EntityPickupItemEvent e){
if(e.getEntity() instanceof HumanEntity) {
Player p = (Player)e.getEntity();
//move the item into the players inventory instead of the panel
if (plugin.openPanels.hasPanelOpen(p.getName(), PanelPosition.Middle) || plugin.openPanels.hasPanelOpen(p.getName(), PanelPosition.Bottom)) {
plugin.inventorySaver.addItem(p,e.getItem().getItemStack());
e.getItem().remove();
e.setCancelled(true);
}
}
}

@EventHandler
public void playerJoined(PlayerJoinEvent e){
restoreInventory(e.getPlayer(), PanelPosition.Top);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.rockyhawk.commandpanels.playerinventoryhandler.pickupevent;

import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;

public class EntityPickupEvent implements Listener {

CommandPanels plugin;
public EntityPickupEvent(CommandPanels pl) {
this.plugin = pl;
}
@EventHandler
public void onPickup(EntityPickupItemEvent e){
if(e.getEntity() instanceof HumanEntity) {
Player p = (Player)e.getEntity();
//move the item into the players inventory instead of the panel
if (plugin.openPanels.hasPanelOpen(p.getName(), PanelPosition.Middle) || plugin.openPanels.hasPanelOpen(p.getName(), PanelPosition.Bottom)) {
plugin.inventorySaver.addItem(p,e.getItem().getItemStack());
e.getItem().remove();
e.setCancelled(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.rockyhawk.commandpanels.playerinventoryhandler.pickupevent;

import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.openpanelsmanager.PanelPosition;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPickupItemEvent;

public class legacyPlayerEvent implements Listener {

CommandPanels plugin;
public legacyPlayerEvent(CommandPanels pl) {
this.plugin = pl;
}

@EventHandler
public void onPickup(PlayerPickupItemEvent e){
Player p = e.getPlayer();
//move the item into the players inventory instead of the panel
if (plugin.openPanels.hasPanelOpen(p.getName(), PanelPosition.Middle) || plugin.openPanels.hasPanelOpen(p.getName(), PanelPosition.Bottom)) {
plugin.inventorySaver.addItem(p,e.getItem().getItemStack());
e.getItem().remove();
e.setCancelled(true);
}
}
}
2 changes: 1 addition & 1 deletion src/me/rockyhawk/commandpanels/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public String getLatestVersion(boolean sendMessages){
public void run(){
HttpURLConnection connection;
try {
connection = (HttpURLConnection) new URL("https://raw.githubusercontent.com/rockyhawk64/CommandPanels/master/resource/plugin.yml").openConnection();
connection = (HttpURLConnection) new URL("https://raw.githubusercontent.com/rockyhawk64/CommandPanels/latest/resource/plugin.yml").openConnection();
connection.setConnectTimeout(5000); // 5 seconds
connection.setReadTimeout(5000); // 5 seconds
connection.connect();
Expand Down

0 comments on commit ad25df2

Please sign in to comment.