Skip to content

Commit

Permalink
Feat: Using NBT to detect items
Browse files Browse the repository at this point in the history
Refactor: Deleted the debug mode
  • Loading branch information
GuangChen2333 committed Dec 26, 2021
1 parent 0ac1ff7 commit 1eeb8cd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 72 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ A spigot plugin for limiting 32k weapons

Supported minecraft version: 1.12.2

> Warning: In 2.0.0 and later versions, the plugin need the dependency `NBT API`
> You can download it [Here](https://www.spigotmc.org/resources/nbt-api.7939/)
---

## Commands
Expand All @@ -24,13 +27,8 @@ Disable plugin (temporarily)
```yaml
# Plugin switch
enabled: true
# Debug Mode (print more logs)
# Actually I used it myself, not practical
debug_mode: false
```
## Recent roadmap
- [x] Debug Mode
- [x] ~~Fix InventoryClickEvent~~ Listen **InventoryCloseEvent**
- [x] Fix the problem that Shulker Box will be detected
- [ ] Add the list of controlled items
- [x] Use NBT to determine whether the item should be tested
- [x] Delete debug mode
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'cn.guangchen233'
version '1.1.0'
version '2.0.0'

repositories {
maven {
Expand All @@ -26,11 +26,16 @@ repositories {
url = 'https://oss.sonatype.org/content/repositories/central'
}

maven {
url = 'https://repo.codemc.org/repository/maven-public/'
}

mavenCentral()
}

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
compileOnly 'de.tr7zw:item-nbt-api-plugin:2.9.0'
}

test {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/cn/guangchen233/limiter32k/LimiterMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ public void onEnable() {
saveDefaultConfig();
FileConfiguration config = getConfig();
boolean isEnabled = config.getBoolean("enabled");
boolean isDebugMode = config.getBoolean("debug_mode");
this.getLogger().info("Thanks for using this plugin");
this.getLogger().info("Current version: " + this.getDescription().getVersion());
this.getLogger().info("Plugin enabled: " + isEnabled);

EventListener eventListener = new EventListener(isEnabled, isDebugMode, this.getLogger());
EventListener eventListener = new EventListener(isEnabled, this.getLogger());
Bukkit.getPluginManager().registerEvents(eventListener, this);
Bukkit.getPluginCommand("32klimiter").setExecutor(new LimiterCommand(eventListener));
}
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/cn/guangchen233/limiter32k/events/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

public class EventListener implements Listener {
private boolean enabled;
private final boolean debug_mode;
private final Logger logger;
private final Utils utils;
private final ItemStack AIR = new ItemStack(Material.AIR);

public EventListener(boolean enabled, boolean debug_mode, Logger logger) {
public EventListener(boolean enabled, Logger logger) {
this.enabled = enabled;
this.debug_mode = debug_mode;
this.logger = logger;
this.utils = new Utils(logger);
}

@EventHandler
Expand All @@ -34,12 +34,12 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
// is player
Player player = (Player) event.getDamager();
// main hand
if (Utils.checkItem(player.getInventory().getItemInMainHand())) {
if (utils.checkItem(player.getInventory().getItemInMainHand())) {
event.setDamage(40D);
player.getInventory().setItemInMainHand(AIR);
}
// off hand
else if (Utils.checkItem(player.getInventory().getItemInOffHand())) {
else if (utils.checkItem(player.getInventory().getItemInOffHand())) {
event.setDamage(40D);
player.getInventory().setItemInOffHand(AIR);
}
Expand All @@ -51,8 +51,8 @@ else if (Utils.checkItem(player.getInventory().getItemInOffHand())) {
@EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
if (enabled) {
boolean mainHandResult = Utils.checkItem(event.getMainHandItem());
boolean offHandResult = Utils.checkItem(event.getOffHandItem());
boolean mainHandResult = utils.checkItem(event.getMainHandItem());
boolean offHandResult = utils.checkItem(event.getOffHandItem());
if (mainHandResult) {
event.setMainHandItem(AIR);
}
Expand All @@ -66,7 +66,7 @@ public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
public void EntityPickupItem(EntityPickupItemEvent event) {
if (enabled) {
ItemStack item = event.getItem().getItemStack();
if (Utils.checkItem(item)) {
if (utils.checkItem(item)) {
event.setCancelled(true);
event.getItem().remove();
}
Expand All @@ -76,10 +76,7 @@ public void EntityPickupItem(EntityPickupItemEvent event) {
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (enabled) {
if (debug_mode) {
logger.info(event.getAction().name() + "|" + event.getInventory().getType().name());
}
if (Utils.checkItem(event.getCurrentItem())) {
if (utils.checkItem(event.getCurrentItem())) {
if (event.getInventory().getType() != InventoryType.HOPPER) {
event.setCurrentItem(AIR);
} else if (event.getAction() != InventoryAction.PICKUP_ALL) {
Expand All @@ -96,7 +93,7 @@ public void onInventoryOpen(InventoryOpenEvent event) {
if (items.length > 0) {
ArrayList<ItemStack> abnormalItems = new ArrayList<>();
for (ItemStack item : items) {
if (Utils.checkItem(item)) {
if (utils.checkItem(item)) {
if (!abnormalItems.contains(item)) {
abnormalItems.add(item);
}
Expand All @@ -119,7 +116,7 @@ public void onInventoryCloseEvent(InventoryCloseEvent event) {
if (items.length > 0) {
ArrayList<ItemStack> abnormalItems = new ArrayList<>();
for (ItemStack item : items) {
if (Utils.checkItem(item)) {
if (utils.checkItem(item)) {
if (!abnormalItems.contains(item)) {
abnormalItems.add(item);
}
Expand Down
62 changes: 19 additions & 43 deletions src/main/java/cn/guangchen233/limiter32k/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,39 @@
package cn.guangchen233.limiter32k.utils;

import de.tr7zw.nbtapi.NBTCompoundList;
import de.tr7zw.nbtapi.NBTItem;
import de.tr7zw.nbtapi.NBTListCompound;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class Utils {
public static List<Material> getShulkerBoxes() {
return Arrays.asList(
Material.BLUE_SHULKER_BOX,
Material.BLACK_SHULKER_BOX,
Material.BROWN_SHULKER_BOX,
Material.CYAN_SHULKER_BOX,
Material.GRAY_SHULKER_BOX,
Material.GREEN_SHULKER_BOX,
Material.LIGHT_BLUE_SHULKER_BOX,
Material.LIME_SHULKER_BOX,
Material.MAGENTA_SHULKER_BOX,
Material.ORANGE_SHULKER_BOX,
Material.PINK_SHULKER_BOX,
Material.PURPLE_SHULKER_BOX,
Material.RED_SHULKER_BOX,
Material.SILVER_SHULKER_BOX,
Material.WHITE_SHULKER_BOX,
Material.YELLOW_SHULKER_BOX
);
}
private final Logger logger;

public static Map<String, Object> stringToMap(String str){
str = str.replace("{", "");
str = str.replace("}", "");
String[] str1 = str.split(", ");
Map<String,Object> map = new HashMap<>();
for (String s : str1) {
String[] str2 = s.split("=");
map.put(str2[0], str2[1]);
}
return map;
public Utils(Logger logger) {
this.logger = logger;
}

public static boolean checkAbnormalInternal(ItemStack item) {
// I don't know what I got from it, but it did take effect
if (item != null && !getShulkerBoxes().contains(item.getType())) {
Map<String, Object> serialize = item.serialize();
if (serialize.containsKey("meta")) {
String meta_string = serialize.get("meta").toString().replace("UNSPECIFIC_META:", "");
Map<String, Object> metas = stringToMap(meta_string);
return metas.containsKey("internal");
public boolean checkAbnormalInternal(ItemStack item) {
if (item != null && item.getType() != Material.AIR) {
NBTItem nbtItem = new NBTItem(item);
NBTCompoundList attrs = nbtItem.getCompoundList("AttributeModifiers");
if (attrs.size() > 0) {
for (NBTListCompound attr : attrs) {
if (attr.getInteger("Amount") != 0) {
return true;
}
}
}
return false;
}
return false;
}

public static boolean checkAbnormalEnchantment(ItemStack item) {
public boolean checkAbnormalEnchantment(ItemStack item) {
if (item != null) {
Map<Enchantment, Integer> enchantments = item.getEnchantments();
for (Integer level : enchantments.values()) {
Expand All @@ -70,7 +46,7 @@ public static boolean checkAbnormalEnchantment(ItemStack item) {
return false;
}

public static boolean checkItem(ItemStack itemStack) {
public boolean checkItem(ItemStack itemStack) {
if (itemStack != null) {
return checkAbnormalInternal(itemStack)
|| checkAbnormalEnchantment(itemStack);
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# Plugin switch
enabled: true
# Debug Mode (print more logs)
# Actually I used it myself, not practical
debug_mode: false
7 changes: 5 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 32kLimiter
main: cn.guangchen233.limiter32k.LimiterMain
version: 1.1.0
version: 2.0.0
author: GuangChen233
website: https://github.com/GuangChen2333/32kLimiter
api-version: 1.12
Expand All @@ -14,4 +14,7 @@ commands:

permissions:
32klimiter.admin:
default: op
default: op

depend:
- NBTAPI

0 comments on commit 1eeb8cd

Please sign in to comment.