Skip to content

Commit e27a57c

Browse files
committed
Inventory exploit prevention
1 parent 4213582 commit e27a57c

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

src/main/java/io/github/thatsmusic99/headsplus/inventories/BaseInventory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.thatsmusic99.headsplus.inventories.icons.list.Air;
88
import io.github.thatsmusic99.headsplus.inventories.icons.list.Glass;
99
import io.github.thatsmusic99.headsplus.inventories.icons.list.Stats;
10+
import io.github.thatsmusic99.headsplus.reflection.NBTManager;
1011
import io.github.thatsmusic99.headsplus.util.CachedValues;
1112
import io.github.thatsmusic99.headsplus.util.HPUtils;
1213
import io.github.thatsmusic99.headsplus.util.PagedLists;
@@ -179,6 +180,14 @@ public void onInventoryClick(InventoryClickEvent event) {
179180
Player player = (Player) event.getWhoClicked();
180181
if (slot > -1 && slot < event.getInventory().getSize()) {
181182
event.setCancelled(true);
183+
for (int i = 0; i < 46; i++) {
184+
ItemStack item = player.getInventory().getItem(i);
185+
if (item != null) {
186+
if (NBTManager.isIcon(item)) {
187+
player.getInventory().setItem(i, new ItemStack(Material.AIR));
188+
}
189+
}
190+
}
182191
IconClickEvent iconEvent = new IconClickEvent(player, icons[slot]);
183192
Bukkit.getPluginManager().callEvent(iconEvent);
184193
if (!iconEvent.isCancelled()) {

src/main/java/io/github/thatsmusic99/headsplus/inventories/Icon.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.thatsmusic99.headsplus.HeadsPlus;
44
import io.github.thatsmusic99.headsplus.config.HeadsPlusMessagesManager;
5+
import io.github.thatsmusic99.headsplus.reflection.NBTManager;
56
import org.bukkit.Material;
67
import org.bukkit.configuration.file.FileConfiguration;
78
import org.bukkit.entity.Player;
@@ -21,7 +22,7 @@ public abstract class Icon {
2122
private String id;
2223

2324
public Icon(ItemStack itemStack) {
24-
item = itemStack;
25+
item = NBTManager.addIconNBT(itemStack);
2526
hpi = hp.getItems().getConfig();
2627
}
2728

@@ -34,11 +35,13 @@ public Icon(String id) {
3435
hpi = hp.getItems().getConfig();
3536
this.id = id;
3637
initItem(id);
38+
item = NBTManager.addIconNBT(item);
3739
}
3840
public Icon(Player player) {
3941
hpi = hp.getItems().getConfig();
4042
initItem(getId());
4143
initNameAndLore(getId(), player);
44+
item = NBTManager.addIconNBT(item);
4245
}
4346

4447
public Icon() {

src/main/java/io/github/thatsmusic99/headsplus/inventories/icons/content/CustomHead.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.thatsmusic99.headsplus.api.events.HeadPurchaseEvent;
55
import io.github.thatsmusic99.headsplus.inventories.InventoryManager;
66
import io.github.thatsmusic99.headsplus.inventories.icons.Content;
7+
import io.github.thatsmusic99.headsplus.reflection.NBTManager;
78
import net.milkbowl.vault.economy.Economy;
89
import net.milkbowl.vault.economy.EconomyResponse;
910
import org.bukkit.Bukkit;
@@ -64,6 +65,7 @@ public boolean onClick(Player player, InventoryClickEvent event) {
6465
ItemMeta meta = item.getItemMeta();
6566
meta.setLore(new ArrayList<>());
6667
item.setItemMeta(meta);
68+
item = NBTManager.removeIconNBT(item);
6769
player.getInventory().addItem(item);
6870
}
6971
} else {

src/main/java/io/github/thatsmusic99/headsplus/listeners/HPMaskEvents.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,19 @@ public void onEvent(PlayerQuitEvent event) {
7272
@EventHandler
7373
public void onEvent(InventoryClickEvent e) {
7474
HeadsPlus hp = HeadsPlus.getInstance();
75+
Player player = (Player) e.getWhoClicked();
76+
for (int i = 0; i < 46; i++) {
77+
ItemStack item = player.getInventory().getItem(i);
78+
if (item != null) {
79+
if (NBTManager.isIcon(item)) {
80+
player.getInventory().setItem(i, new ItemStack(Material.AIR));
81+
}
82+
}
83+
}
7584
ItemStack item;
7685
boolean shift = e.isShiftClick();
7786
// If we're shift clicking
87+
7888
if (shift) {
7989
// We need to get the current item
8090
item = e.getCurrentItem();
@@ -84,6 +94,7 @@ public void onEvent(InventoryClickEvent e) {
8494
if (e.getAction().equals(InventoryAction.PICKUP_ALL)) return;
8595
item = e.getCursor();
8696
}
97+
8798
if (hp.getConfiguration().getPerks().mask_powerups) {
8899
if (e.getRawSlot() == getSlot() || (shift && e.getRawSlot() != getSlot())) {
89100
checkMask((Player) e.getWhoClicked(), item);

src/main/java/io/github/thatsmusic99/headsplus/reflection/NBTManager.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -464,25 +464,16 @@ public static ItemStack setType(ItemStack i, String type) {
464464
return setString(i, "headsplus-type", type);
465465
}
466466

467-
// This is a funny one, ignore this example
468-
public static ItemStack addDatabaseHead(ItemStack i, String id, double price) {
469-
try {
470-
Object nmsItem = getNMSCopy(i);
471-
Object nbtTag = getNBTTag(nmsItem);
472-
if (nbtTag == null) {
473-
nbtTag = newNBTTag();
474-
}
475-
Block.class.getMethod("setData", byte.class);
476-
Method method = nbtTag.getClass().getMethod("setString", String.class, String.class);
477-
method.invoke(nbtTag, "head-id", id);
478-
Method method1 = nbtTag.getClass().getMethod("setDouble", String.class, double.class);
479-
method1.invoke(nbtTag, "head-price", price);
480-
nmsItem = setNBTTag(nmsItem, nbtTag);
481-
return asBukkitCopy(nmsItem);
482-
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
483-
e.printStackTrace();
484-
}
485-
return i;
467+
public static ItemStack addIconNBT(ItemStack i) {
468+
return setBoolean(i, "hp-gui", true);
469+
}
470+
471+
public static boolean isIcon(ItemStack i) {
472+
return getBoolean(i, "hp-gui");
473+
}
474+
475+
public static ItemStack removeIconNBT(ItemStack i) {
476+
return setBoolean(i, "hp-gui", false);
486477
}
487478

488479
public static String getType(ItemStack i) {

0 commit comments

Comments
 (0)