Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple bug fixes #288

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
blockProtVersion=1.2.1
nbtApiVersion=2.13.1-SNAPSHOT
anvilGuiVersion=1.9.5-SNAPSHOT
anvilGuiVersion=1.9.6-SNAPSHOT
townyVersion=0.98.1.6
papiVersion=2.11.0
worldGuardVersion=7.0.7
2 changes: 1 addition & 1 deletion spigot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
implementation(project(":common"))

// Spigot
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly("org.apache.commons:commons-lang3:3.13.0")

// bStats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@ protected ItemStack toggleEnchants(@NotNull ItemStack stack, final @Nullable Boo
}
if (meta != null) {
if (meta.hasEnchants() && (toggle == null || !toggle)) {
meta.removeEnchant(Enchantment.ARROW_INFINITE);
meta.removeEnchant(Enchantment.INFINITY);
} else if (!meta.hasEnchants() && (toggle == null || toggle)) {
meta.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
meta.addEnchant(Enchantment.INFINITY, 1, true);
}
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
stack.setItemMeta(meta);
Expand Down Expand Up @@ -644,10 +644,10 @@ protected ItemStack toggleOption(@NotNull ItemStack stack, final @Nullable Boole
}

if (meta.hasEnchants() && (toggle == null || !toggle)) {
meta.removeEnchant(Enchantment.ARROW_INFINITE);
meta.removeEnchant(Enchantment.INFINITY);
meta.setDisplayName(name + ": " + Translator.get(TranslationKey.DISABLED));
} else if (!meta.hasEnchants() && (toggle == null || toggle)) {
meta.addEnchant(Enchantment.ARROW_INFINITE, 1, true);
meta.addEnchant(Enchantment.INFINITY, 1, true);
meta.setDisplayName(name + ": " + Translator.get(TranslationKey.ENABLED));
}
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import de.sean.blockprot.bukkit.TranslationKey;
import de.sean.blockprot.bukkit.Translator;
import de.sean.blockprot.bukkit.nbt.BlockAccessFlag;
import de.sean.blockprot.bukkit.nbt.BlockNBTHandler;
import de.sean.blockprot.bukkit.nbt.FriendHandler;
import de.sean.blockprot.bukkit.nbt.FriendSupportingHandler;
import de.sean.blockprot.nbt.FriendModifyAction;
import de.tr7zw.changeme.nbtapi.NBTCompound;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
Expand Down Expand Up @@ -115,8 +117,17 @@ public void onClick(@NotNull InventoryClickEvent event, @NotNull InventoryState

@Override
public void onClose(@NotNull InventoryCloseEvent event, @NotNull InventoryState state) {
if (this.playerHandler != null && curFlags != null)
if (this.playerHandler != null && curFlags != null) {
this.playerHandler.setAccessFlags(curFlags);

final Block block = state.getBlock();
if (block == null) return;

final BlockNBTHandler nbtHandler = getNbtHandlerOrNull(block);
if (nbtHandler == null) return;

nbtHandler.applyToOtherContainer();
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -227,10 +228,19 @@ public void onBlockPhysics(@NotNull final BlockPhysicsEvent event) {

@EventHandler
public void onSignChanged(@NotNull final SignChangeEvent event) {
if (BlockProt.getDefaultConfig().isLockableBlock(event.getBlock().getType())) {
final var handler = new BlockNBTHandler(event.getBlock());
if (handler.isProtected() && !handler.isOwner(event.getPlayer().getUniqueId()))
event.setCancelled(true);
}
final Block block = event.getBlock();
if (!BlockProt.getDefaultConfig().isLockableBlock(block.getType())) return;

final BlockNBTHandler handler = new BlockNBTHandler(block);
if (!handler.isProtected()) return;

final Player player = event.getPlayer();
final String playerUuid = player.getUniqueId().toString();
if (handler.isOwner(playerUuid)) return;

final var friend = handler.getFriend(playerUuid);
if (friend.isPresent() && friend.get().canWrite()) return;

event.setCancelled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.type.Door;
import org.bukkit.block.data.type.Gate;
import org.bukkit.block.data.type.Lectern;
import org.bukkit.block.data.type.TrapDoor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -64,14 +67,15 @@ public void playerInteract(PlayerInteractEvent event) {
sendMessage(player, Translator.get(TranslationKey.MESSAGES__NO_PERMISSION));
} else {
BlockNBTHandler handler = new BlockNBTHandler(event.getClickedBlock());
final var blockData = event.getClickedBlock().getBlockData();
if (!(handler.canAccess(player.getUniqueId().toString()) || player.hasPermission(Permissions.BYPASS.key()))) {
event.setCancelled(true);
sendMessage(player, Translator.get(TranslationKey.MESSAGES__NO_PERMISSION));
} else if (event.getClickedBlock().getType() == Material.LECTERN && !handler.isOwner(player.getUniqueId())) {
} else if (event.getClickedBlock().getType() == Material.LECTERN && handler.isProtected() && !handler.isOwner(player.getUniqueId())) {
// With Lecterns you place the books by interacting with the block. canAccess will return true because the
// player has the READ permission, but this should not be allowed in this case. In the case that the player
// wants to take the book from the lectern (hasBook returns true) we already listen for PlayerTakeLecternBookEvent.
final var lectern = (Lectern)event.getClickedBlock().getBlockData();
final var lectern = (Lectern)blockData;
if (!lectern.hasBook()) {
final var friend = handler.getFriend(player.getUniqueId().toString());
if (friend.isEmpty() || !friend.get().canWrite()) {
Expand All @@ -80,6 +84,13 @@ public void playerInteract(PlayerInteractEvent event) {
sendMessage(player, Translator.get(TranslationKey.MESSAGES__NO_PERMISSION));
}
}
} else if ((blockData instanceof Door || blockData instanceof TrapDoor || blockData instanceof Gate) && handler.isProtected() && !handler.isOwner(player.getUniqueId())) {
final var friend = handler.getFriend(player.getUniqueId().toString());
if (friend.isEmpty() || !friend.get().canWrite()) {
// The player cannot write and therefore is not allowed to change the door open state.
event.setCancelled(true);
sendMessage(player, Translator.get(TranslationKey.MESSAGES__NO_PERMISSION));
}
} else if (!(new PlayerSettingsHandler(player).hasPlayerInteractedWithMenu())) {
Long timestamp = LockHintMessageCooldown.getTimestamp(player);
if (timestamp == null || timestamp < System.currentTimeMillis() - (BlockProt.getDefaultConfig().getLockHintCooldown() * 1000)) { // 10 seconds in milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,55 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) {
} else {
event.setCancelled(true); // Don't allow interaction in a menu.
}

return;
}
} else {
// No state, let's check if they're in some block inventory.
try {
// Casting null does not trigger a ClassCastException.
if (event.getInventory().getHolder() == null) return;
BlockInventoryHolder blockHolder = (BlockInventoryHolder) event.getInventory().getHolder();
if (BlockProt.getDefaultConfig().isLockable(blockHolder.getBlock().getType())) {
// Ok, we have a lockable block, check if they can write anything to this.
// TODO: Implement a Cache for this lookup, it seems to be quite expensive.
// We should probably use a MultiMap, or implement our own Key that
// can use multiple key objects, a Block and Player in this case.
BlockNBTHandler handler = new BlockNBTHandler(blockHolder.getBlock());
String playerUuid = player.getUniqueId().toString();

if (handler.isProtected() && !handler.isOwner(playerUuid)) {
final var friend = handler.getFriend(playerUuid);
if (friend.isPresent()) {
if (!friend.get().canWrite()) {
event.setCancelled(true);
} else if (!friend.get().canRead()) {
event.setCancelled(true);
player.closeInventory();
}
} else {
// The player is not a friend and not the owner; they shouldn't have
// access anyway.
player.closeInventory();
}

// No state, let's check if they're in some block inventory.
try {
// Casting null does not trigger a ClassCastException.
if (event.getInventory().getHolder() == null) return;
final InventoryHolder holder = event.getInventory().getHolder();

Block block;
if (holder instanceof BlockInventoryHolder blockHolder) {
block = blockHolder.getBlock();
} else if (holder instanceof DoubleChest doubleChestHolder) {
block = doubleChestHolder.getLocation().getBlock();
} else {
return;
}

if (BlockProt.getDefaultConfig().isLockable(block.getType())) {
// Ok, we have a lockable block, check if they can write anything to this.
// TODO: Implement a Cache for this lookup, it seems to be quite expensive.
// We should probably use a MultiMap, or implement our own Key that
// can use multiple key objects, a Block and Player in this case.
BlockNBTHandler handler = new BlockNBTHandler(block);
String playerUuid = player.getUniqueId().toString();

if (handler.isProtected() && !handler.isOwner(playerUuid)) {
final var friend = handler.getFriend(playerUuid);
if (friend.isPresent()) {
if (!friend.get().canWrite()) {
event.setCancelled(true);
} else if (!friend.get().canRead()) {
event.setCancelled(true);
Bukkit.getScheduler().runTask(BlockProt.getInstance(), player::closeInventory);
}
} else {
// The player is not a friend and not the owner; they shouldn't have
// access anyway.
event.setCancelled(true);
Bukkit.getScheduler().runTask(BlockProt.getInstance(), player::closeInventory);
}
}
} catch (ClassCastException e) {
// It's not a block, and it's therefore also not lockable.
// This is probably some other custom inventory from another
// plugin, or possibly some entity inventory, e.g. villagers.
}
} catch (ClassCastException e) {
// It's not a block, and it's therefore also not lockable.
// This is probably some other custom inventory from another
// plugin, or possibly some entity inventory, e.g. villagers.
}
}

Expand Down