Check slimefun block storage during player interacts.

This should solve protection issues when a player interacts with a block
that has no corresponding TileEntity.

* Improve SlimefunProvider lookups.
* Fix slimefun item_name context.
This commit is contained in:
bloodshot 2020-10-30 11:51:01 -04:00
parent 0ac8ad89d8
commit 569dd608df
4 changed files with 49 additions and 27 deletions

View File

@ -117,6 +117,8 @@ dependencies {
compileOnly "net.milkbowl.vault:VaultAPI:1.7"
compileOnly "us.dynmap:dynmap-api:3.0-SNAPSHOT"
compileOnly "com.github.lucko:luckperms:master-SNAPSHOT"
compileOnly "com.github.slimefun:slimefun4:master-SNAPSHOT"
compileOnly "com.github.thebusybiscuit:cs-corelib:master-SNAPSHOT"
// Libs
compileOnly "aopalliance:aopalliance:1.0"
compileOnly "co.aikar:acf-core:0.5.0-SNAPSHOT"

View File

@ -865,7 +865,7 @@ public class PlayerEventHandler implements Listener {
final BlockState state = clickedBlock.getState();
final ItemStack itemInHand = event.getItem();
final boolean hasInventory = NMSUtil.getInstance().isTileInventory(location) || clickedBlock.getType() == Material.ENDER_CHEST;
if (hasInventory) {
if (hasInventory || (GriefDefenderPlugin.getInstance().getSlimefunProvider() != null && GriefDefenderPlugin.getInstance().getSlimefunProvider().isInventory(clickedBlock))) {
onInventoryOpen(event, state.getLocation(), state, player);
return;
}

View File

@ -987,6 +987,12 @@ public class GDPermissionManager implements PermissionManager {
contexts.add(new Context(ContextKeys.USED_ITEM, stackId));
if (stack.getItemMeta() != null && stack.getItemMeta().getDisplayName() != null) {
String itemName = stack.getItemMeta().getDisplayName().replaceAll("[^A-Za-z0-9]", "").toLowerCase();
if (GriefDefenderPlugin.getInstance().getSlimefunProvider() != null) {
final String slimefunId = GriefDefenderPlugin.getInstance().getSlimefunProvider().getSlimeItemDisplayName(stack);
if (slimefunId != null && !slimefunId.isEmpty()) {
itemName = slimefunId;
}
}
if (itemName != null && !itemName.isEmpty()) {
if (!itemName.contains(":")) {
itemName = "minecraft:" + itemName;

View File

@ -30,7 +30,10 @@ import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import com.griefdefender.api.permission.Context;
import com.griefdefender.internal.util.NMSUtil;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
public class SlimefunProvider {
@ -39,6 +42,7 @@ public class SlimefunProvider {
private static final String SLIME_ITEM_KEY = "slimefun:slimefun_item";
public SlimefunProvider() {
}
public String getSlimeItemId(ItemStack stack) {
@ -46,29 +50,19 @@ public class SlimefunProvider {
}
public String getSlimeItemId(ItemStack stack, Set<Context> contexts) {
// check item
String customItemId = NMSUtil.getInstance().getItemStackNBTString(stack, SLIME_COMPOUND_KEY, SLIME_ITEM_KEY);
if (customItemId != null && !customItemId.isEmpty()) {
final SlimefunItem slimefunItem = SlimefunItem.getByItem(stack);
if (slimefunItem != null) {
String level = null;
if (contexts != null && customItemId.length() > 2 && customItemId.matches("^.*\\_\\d$")) {
level = customItemId.substring(customItemId.length() - 1, customItemId.length());
customItemId = customItemId.substring(0, customItemId.length() - 2);
String id = slimefunItem.getId();
if (id.length() > 2 && id.matches("^.*\\_\\d$")) {
System.out.println("match!");
level = id.substring(id.length() - 1, id.length());
id = id.substring(0, id.length() - 2);
if (contexts != null) {
contexts.add(new Context("slimefun_level", level));
}
}
return "slimefun:" + customItemId.toLowerCase();
}
// check block
String customItemBlockId = NMSUtil.getInstance().getItemStackNBTString(stack, SLIME_COMPOUND_KEY, SLIME_BLOCK_KEY);
if (customItemBlockId != null && !customItemBlockId.isEmpty()) {
String level = null;
if (contexts != null && customItemBlockId.length() > 2 && customItemBlockId.matches("^.*\\_\\d$")) {
level = customItemBlockId.substring(customItemBlockId.length() - 1, customItemBlockId.length());
customItemBlockId = customItemBlockId.substring(0, customItemBlockId.length() - 2);
contexts.add(new Context("slimefun_level", level));
}
return "slimefun:" + customItemBlockId.toLowerCase();
return "slimefun:" + id.toLowerCase();
}
return "";
@ -79,17 +73,37 @@ public class SlimefunProvider {
}
public String getSlimeBlockId(Block block, Set<Context> contexts) {
String customItemBlockId = NMSUtil.getInstance().getTileEntityNBTString(block, "PublicBukkitValues", "slimefun:slimefun_block");
if (customItemBlockId != null && !customItemBlockId.isEmpty()) {
final SlimefunItem slimefunBlock = BlockStorage.check(block);
if (slimefunBlock != null) {
String level = null;
if (contexts != null && customItemBlockId.length() > 2 && customItemBlockId.matches("^.*\\_\\d$")) {
level = customItemBlockId.substring(customItemBlockId.length() - 1, customItemBlockId.length());
customItemBlockId = customItemBlockId.substring(0, customItemBlockId.length() - 2);
contexts.add(new Context("slimefun_level", level));
String id = slimefunBlock.getId();
if (id.length() > 2 && id.matches("^.*\\_\\d$")) {
level = id.substring(id.length() - 1, id.length());
id = id.substring(0, id.length() - 2);
if (contexts != null) {
contexts.add(new Context("slimefun_level", level));
}
}
return "slimefun:" + customItemBlockId.toLowerCase();
return "slimefun:" + id.toLowerCase();
}
return "";
}
public String getSlimeItemDisplayName(ItemStack stack) {
final SlimefunItem slimefunItem = SlimefunItem.getByItem(stack);
if (slimefunItem != null) {
return "slimefun:" + slimefunItem.getItemName();
}
return "";
}
public boolean isInventory(Block block) {
final SlimefunItem slimefunItem = BlockStorage.check(block);
if (slimefunItem != null) {
return BlockMenuPreset.isInventory(slimefunItem.getId());
}
return false;
}
}