Skip to content

Commit

Permalink
Fixed detection of block types for stacked blocks (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Mar 10, 2022
1 parent 2726026 commit 2efc7cc
Showing 1 changed file with 42 additions and 5 deletions.
Expand Up @@ -3,12 +3,16 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.hooks.listener.IStackedBlocksListener;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.key.Key;
import com.bgsoftware.superiorskyblock.utils.ServerVersion;
import com.bgsoftware.superiorskyblock.utils.events.EventsCaller;
import com.bgsoftware.superiorskyblock.utils.islands.IslandUtils;
import com.bgsoftware.superiorskyblock.utils.items.ItemUtils;
import com.bgsoftware.superiorskyblock.utils.legacy.Materials;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -19,10 +23,22 @@
import org.bukkit.inventory.ItemStack;

import java.math.BigInteger;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

public final class StackedBlocksLogic {

@SuppressWarnings("unchecked")
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() {
Expand All @@ -43,19 +59,22 @@ public static boolean canStackBlocks(Player player, ItemStack placeItem, Block a
if (placeItem.hasItemMeta() && (placeItem.getItemMeta().hasDisplayName() || placeItem.getItemMeta().hasLore()))
return false;

if (againstBlock.getType().name().equals("GLOWING_REDSTONE_ORE"))
againstBlock.setType(Material.REDSTONE_ORE);
Material newAgainstBlockType = AGAINST_BLOCK_CHANGE_MATERIAL.get(againstBlock.getType());
if (newAgainstBlockType != null)
againstBlock.setType(newAgainstBlockType);

//noinspection deprecation
byte blockData = againstBlock.getData();
Material blockType = againstBlock.getType();

if (blockType.name().contains("PORTAL_FRAME")) {
blockData = 0;
} else if (blockType == Material.CAULDRON && placeItem.getType().name().equals("CAULDRON_ITEM")) {
if (blockType == Material.CAULDRON && placeItem.getType().name().equals("CAULDRON_ITEM")) {
blockType = Material.valueOf("CAULDRON_ITEM");
}

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

if (blockType != placeItem.getType() || blockData != placeItem.getDurability() ||
(replaceState != null && replaceState.getType() != Material.AIR))
return false;
Expand Down Expand Up @@ -201,4 +220,22 @@ public static boolean tryUnstack(Player player, Block block, SuperiorSkyblockPlu
return true;
}

private static Map<Material, Material> buildImmutableMap(Pair<Material, Material>... materials) {
ImmutableMap.Builder<Material, Material> builder = new ImmutableMap.Builder<>();
for (Pair<Material, Material> material : materials) {
if (material.getKey() != null && material.getValue() != null)
builder.put(material.getKey(), material.getValue());
}
return builder.build();
}

private static Set<Material> buildImmutableSet(Material... materials) {
ImmutableSet.Builder<Material> builder = new ImmutableSet.Builder<>();
for (Material material : materials) {
if (material != null)
builder.add(material);
}
return builder.build();
}

}

0 comments on commit 2efc7cc

Please sign in to comment.