Skip to content

Commit

Permalink
Fixed issues with detection of similar stacked blocks and the place i…
Browse files Browse the repository at this point in the history
…tem (#1068)
  • Loading branch information
OmerBenGera committed Apr 22, 2022
1 parent 458be27 commit cc172d1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
Expand Up @@ -329,7 +329,7 @@ public void onBlockStack(PlayerInteractEvent e) {
StackedBlocksLogic.tryUnstack(e.getPlayer(), clickedBlock, plugin);
}

if (inHand != null && StackedBlocksLogic.canStackBlocks(e.getPlayer(), inHand, clickedBlock, null) &&
if (inHand != null && StackedBlocksLogic.canStackBlocks(e.getPlayer(), inHand, clickedBlock) &&
StackedBlocksLogic.tryStack(e.getPlayer(), inHand, clickedBlock.getLocation(), e)) {
e.setCancelled(true);
}
Expand All @@ -344,7 +344,7 @@ public void onBlockStack(BlockPlaceEvent e) {
if (plugin.getStackedBlocks().getStackedBlockAmount(e.getBlock()) > 1)
plugin.getStackedBlocks().setStackedBlock(e.getBlock(), 1);

if (!StackedBlocksLogic.canStackBlocks(e.getPlayer(), e.getItemInHand(), e.getBlockAgainst(), e.getBlockReplacedState()))
if (!StackedBlocksLogic.canStackBlocks(e.getPlayer(), e.getItemInHand(), e.getBlockAgainst()))
return;

if (StackedBlocksLogic.tryStack(e.getPlayer(), e.getItemInHand(), e.getBlockAgainst().getLocation(), e))
Expand Down
Expand Up @@ -50,7 +50,7 @@ public void onInteract(InventoryClickEvent e) {
if (itemToDeposit == null || itemToDeposit.getType() == Material.AIR)
return;

if (!StackedBlocksLogic.canStackBlocks((Player) e.getWhoClicked(), itemToDeposit, stackedBlock.getBlock(), null))
if (!StackedBlocksLogic.canStackBlocks((Player) e.getWhoClicked(), itemToDeposit, stackedBlock.getBlock()))
e.setCancelled(true);
}

Expand All @@ -60,7 +60,7 @@ public void onClose(InventoryCloseEvent e) {

for (ItemStack itemStack : e.getInventory().getContents()) {
if (itemStack != null && itemStack.getType() != Material.AIR) {
if (StackedBlocksLogic.canStackBlocks((Player) e.getPlayer(), itemStack, stackedBlock.getBlock(), null)) {
if (StackedBlocksLogic.canStackBlocks((Player) e.getPlayer(), itemStack, stackedBlock.getBlock())) {
depositAmount += itemStack.getAmount();
blockItem = itemStack;
} else {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.bgsoftware.superiorskyblock.api.hooks.listener.IStackedBlocksListener;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.key.KeySet;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.key.KeyImpl;
Expand All @@ -18,14 +19,14 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nullable;
import java.math.BigInteger;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

Expand All @@ -38,18 +39,13 @@ public final class StackedBlocksLogic {
private static final Map<Material, Material> AGAINST_BLOCK_CHANGE_MATERIAL = buildImmutableMap(
new Pair<>(Materials.getMaterialSafe("GLOWING_REDSTONE_ORE"), Material.REDSTONE_ORE)
);
private static final Set<Material> DATA_REMOVAL_MATERIALS = buildImmutableSet(
Materials.END_PORTAL_FRAME.toBukkitType(),
Materials.getMaterialSafe("PRISMARINE_BRICKS"),
Materials.getMaterialSafe("DARK_PRISMARINE")
);

private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

private StackedBlocksLogic() {
}

public static boolean canStackBlocks(Player player, ItemStack placeItem, Block againstBlock, BlockState replaceState) {
public static boolean canStackBlocks(Player player, ItemStack placeItem, Block againstBlock) {
if (!plugin.getSettings().getStackedBlocks().isEnabled())
return false;

Expand All @@ -68,23 +64,16 @@ public static boolean canStackBlocks(Player player, ItemStack placeItem, Block a
if (newAgainstBlockType != null)
againstBlock.setType(newAgainstBlockType);

//noinspection deprecation
byte blockData = againstBlock.getData();
Material blockType = againstBlock.getType();
KeySet whitelist = (KeySet) plugin.getSettings().getStackedBlocks().getWhitelisted();

if (CAULDRON_ITEM != null && blockType == Material.CAULDRON && CAULDRON_ITEM == placeItem.getType()) {
blockType = CAULDRON_ITEM;
}
Key againstBlockKey = whitelist.getKey(KeyImpl.of(againstBlock));

if (DATA_REMOVAL_MATERIALS.contains(blockType)) {
blockData = 0;
}

if (blockType != placeItem.getType() || blockData != placeItem.getDurability() ||
(replaceState != null && replaceState.getType() != Material.AIR))
if (!whitelist.contains(againstBlockKey))
return false;

if (!plugin.getSettings().getStackedBlocks().getWhitelisted().contains(KeyImpl.of(againstBlock)))
Key placeItemBlockKey = whitelist.getKey(KeyImpl.of(placeItem));

if (!Objects.equals(againstBlockKey, placeItemBlockKey))
return false;

return superiorPlayer.hasPermission("superior.island.stacker.*") ||
Expand Down

0 comments on commit cc172d1

Please sign in to comment.