From d0c1595da986bdac86cf76ebf1ca6ad7dc4e5e86 Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Sun, 17 Sep 2023 13:55:00 +0300 Subject: [PATCH] Fixed many errors when placing spawners with no entity inside them (#1881) --- .../api/hooks/SpawnersProvider.java | 3 +++ .../spawners/SpawnersProvider_RoseStacker.java | 16 ++++++---------- .../commands/player/CmdValue.java | 8 ++++++-- .../formatting/impl/CapitalizedFormatter.java | 9 ++++++--- .../superiorskyblock/core/key/Keys.java | 11 +++++++++-- .../external/ProvidersManagerImpl.java | 4 +++- .../SpawnersProviderItemMetaSpawnerType.java | 5 ++++- .../DefaultIslandCalculationAlgorithm.java | 14 ++++++-------- 8 files changed, 43 insertions(+), 27 deletions(-) diff --git a/API/src/main/java/com/bgsoftware/superiorskyblock/api/hooks/SpawnersProvider.java b/API/src/main/java/com/bgsoftware/superiorskyblock/api/hooks/SpawnersProvider.java index abc9bc1ac..2fab978ed 100644 --- a/API/src/main/java/com/bgsoftware/superiorskyblock/api/hooks/SpawnersProvider.java +++ b/API/src/main/java/com/bgsoftware/superiorskyblock/api/hooks/SpawnersProvider.java @@ -1,5 +1,6 @@ package com.bgsoftware.superiorskyblock.api.hooks; +import com.bgsoftware.common.annotations.Nullable; import com.bgsoftware.superiorskyblock.api.objects.Pair; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; @@ -17,9 +18,11 @@ public interface SpawnersProvider { /** * Get the spawner type from an item. + * May return null in-case the spawner has no entity inside it. * * @param itemStack The item to check. */ + @Nullable String getSpawnerType(ItemStack itemStack); } diff --git a/Hooks/RoseStacker/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProvider_RoseStacker.java b/Hooks/RoseStacker/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProvider_RoseStacker.java index a3b246af6..03cce73fa 100644 --- a/Hooks/RoseStacker/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProvider_RoseStacker.java +++ b/Hooks/RoseStacker/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProvider_RoseStacker.java @@ -3,8 +3,8 @@ import com.bgsoftware.common.reflection.ReflectMethod; import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; import com.bgsoftware.superiorskyblock.api.island.Island; -import com.bgsoftware.superiorskyblock.api.key.Key; import com.bgsoftware.superiorskyblock.api.objects.Pair; +import com.bgsoftware.superiorskyblock.core.key.Keys; import com.bgsoftware.superiorskyblock.core.logging.Log; import com.google.common.base.Preconditions; import dev.rosewood.rosestacker.api.RoseStackerAPI; @@ -55,9 +55,9 @@ public Pair getSpawner(Location location) { @Override public String getSpawnerType(ItemStack itemStack) { Preconditions.checkNotNull(itemStack, "itemStack parameter cannot be null."); - return GET_STACKED_ITEM_ENTITY_TYPE.isValid() ? - GET_STACKED_ITEM_ENTITY_TYPE.invoke(null, itemStack).name() : - ItemUtils.getStackedItemEntityType(itemStack).name(); + EntityType entityType = GET_STACKED_ITEM_ENTITY_TYPE.isValid() ? + GET_STACKED_ITEM_ENTITY_TYPE.invoke(null, itemStack) : ItemUtils.getStackedItemEntityType(itemStack); + return entityType == null ? null : entityType.name(); } @SuppressWarnings("unused") @@ -68,9 +68,7 @@ public void onSpawnerStack(SpawnerStackEvent e) { Location location = e.getStack().getLocation(); Island island = plugin.getGrid().getIslandAt(location); if (island != null) { - EntityType spawnerType = e.getStack().getSpawner().getSpawnedType(); - Key spawnerKey = Key.ofSpawner(spawnerType); - island.handleBlockPlace(spawnerKey, e.getIncreaseAmount()); + island.handleBlockPlace(Keys.of(e.getStack().getBlock()), e.getIncreaseAmount()); } } @@ -79,9 +77,7 @@ public void onSpawnerUnstack(SpawnerUnstackEvent e) { Location location = e.getStack().getLocation(); Island island = plugin.getGrid().getIslandAt(location); if (island != null) { - EntityType spawnerType = e.getStack().getSpawner().getSpawnedType(); - Key spawnerKey = Key.ofSpawner(spawnerType); - island.handleBlockBreak(spawnerKey, e.getDecreaseAmount()); + island.handleBlockBreak(Keys.of(e.getStack().getBlock()), e.getDecreaseAmount()); } } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/commands/player/CmdValue.java b/src/main/java/com/bgsoftware/superiorskyblock/commands/player/CmdValue.java index ee00d3670..896fc3362 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/commands/player/CmdValue.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/commands/player/CmdValue.java @@ -5,6 +5,7 @@ import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer; import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand; import com.bgsoftware.superiorskyblock.core.Materials; +import com.bgsoftware.superiorskyblock.core.Text; import com.bgsoftware.superiorskyblock.core.formatting.Formatters; import com.bgsoftware.superiorskyblock.core.key.Keys; import com.bgsoftware.superiorskyblock.core.messages.Message; @@ -70,8 +71,11 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, String[ toCheck = Keys.of(inHand); - if (inHand.getType() == Materials.SPAWNER.toBukkitType()) - keyName = Formatters.CAPITALIZED_FORMATTER.format(toCheck.getSubKey() + "_Spawner"); + if (inHand.getType() == Materials.SPAWNER.toBukkitType()) { + String subKey = toCheck.getSubKey(); + if (!Text.isBlank(subKey)) + keyName = Formatters.CAPITALIZED_FORMATTER.format(subKey + "_Spawner"); + } } else { toCheck = Keys.ofMaterialAndData(args[1]); } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/core/formatting/impl/CapitalizedFormatter.java b/src/main/java/com/bgsoftware/superiorskyblock/core/formatting/impl/CapitalizedFormatter.java index 61da55b37..4e52a38a1 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/core/formatting/impl/CapitalizedFormatter.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/core/formatting/impl/CapitalizedFormatter.java @@ -1,5 +1,6 @@ package com.bgsoftware.superiorskyblock.core.formatting.impl; +import com.bgsoftware.superiorskyblock.core.Text; import com.bgsoftware.superiorskyblock.core.formatting.IFormatter; import java.util.Locale; @@ -31,9 +32,11 @@ public String format(String value) { value = value.replace(":", "_-_"); for (String subKey : value.split("_")) { - formattedKey.append(" ") - .append(subKey.substring(0, 1).toUpperCase(Locale.ENGLISH)) - .append(subKey.substring(1).toLowerCase(Locale.ENGLISH)); + if (!Text.isBlank(subKey)) { + formattedKey.append(" ") + .append(subKey.substring(0, 1).toUpperCase(Locale.ENGLISH)) + .append(subKey.substring(1).toLowerCase(Locale.ENGLISH)); + } } return formattedKey.substring(1); diff --git a/src/main/java/com/bgsoftware/superiorskyblock/core/key/Keys.java b/src/main/java/com/bgsoftware/superiorskyblock/core/key/Keys.java index f7069e82e..cf3ab5c67 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/core/key/Keys.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/core/key/Keys.java @@ -23,6 +23,7 @@ import org.bukkit.inventory.ItemStack; import java.util.Locale; +import java.util.Optional; import java.util.regex.Pattern; public class Keys { @@ -64,7 +65,7 @@ public static Key of(Block block) { Key baseKey; if (blockType == Materials.SPAWNER.toBukkitType()) { CreatureSpawner creatureSpawner = (CreatureSpawner) block.getState(); - baseKey = SpawnerKey.of(EntityTypeKey.of(creatureSpawner.getSpawnedType())); + baseKey = getSpawnerKeyFromCreatureSpawner(creatureSpawner); } else { short durability = block.getData(); baseKey = MaterialKey.of(blockType, durability); @@ -76,7 +77,7 @@ public static Key of(Block block) { public static Key of(BlockState blockState) { Key baseKey; if (blockState instanceof CreatureSpawner) { - baseKey = SpawnerKey.of(EntityTypeKey.of(((CreatureSpawner) blockState).getSpawnedType())); + baseKey = getSpawnerKeyFromCreatureSpawner((CreatureSpawner) blockState); } else { baseKey = MaterialKey.of(blockState.getType(), blockState.getRawData()); } @@ -160,4 +161,10 @@ public static Key of(Class baseKeyClass, LazyReference key return new LazyKey<>(baseKeyClass, keyLoader); } + private static SpawnerKey getSpawnerKeyFromCreatureSpawner(CreatureSpawner creatureSpawner) { + EntityTypeKey entityTypeKey = Optional.ofNullable(creatureSpawner.getSpawnedType()) + .map(EntityTypeKey::of).orElse(null); + return SpawnerKey.of(entityTypeKey); + } + } diff --git a/src/main/java/com/bgsoftware/superiorskyblock/external/ProvidersManagerImpl.java b/src/main/java/com/bgsoftware/superiorskyblock/external/ProvidersManagerImpl.java index 41b9a4b45..c35d8b762 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/external/ProvidersManagerImpl.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/external/ProvidersManagerImpl.java @@ -25,6 +25,7 @@ import com.bgsoftware.superiorskyblock.core.ChunkPosition; import com.bgsoftware.superiorskyblock.core.Manager; import com.bgsoftware.superiorskyblock.core.key.Keys; +import com.bgsoftware.superiorskyblock.core.key.types.SpawnerKey; import com.bgsoftware.superiorskyblock.core.logging.Log; import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor; import com.bgsoftware.superiorskyblock.external.async.AsyncProvider; @@ -291,7 +292,8 @@ public void runWorldsListeners(String worldName) { } public Key getSpawnerKey(ItemStack itemStack) { - return Keys.ofSpawner(spawnersProvider.getSpawnerType(itemStack)); + String type = spawnersProvider.getSpawnerType(itemStack); + return type == null ? SpawnerKey.GLOBAL_KEY : Keys.ofSpawner(type); } public boolean hasSnapshotsSupport() { diff --git a/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProviderItemMetaSpawnerType.java b/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProviderItemMetaSpawnerType.java index 5f4db66ef..87b3427b6 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProviderItemMetaSpawnerType.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/external/spawners/SpawnersProviderItemMetaSpawnerType.java @@ -2,9 +2,12 @@ import com.google.common.base.Preconditions; import org.bukkit.block.CreatureSpawner; +import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BlockStateMeta; +import java.util.Optional; + public interface SpawnersProviderItemMetaSpawnerType extends SpawnersProvider_AutoDetect { default String getSpawnerType(ItemStack itemStack) { @@ -12,7 +15,7 @@ default String getSpawnerType(ItemStack itemStack) { if (itemStack.getItemMeta() instanceof BlockStateMeta) { CreatureSpawner creatureSpawner = (CreatureSpawner) ((BlockStateMeta) itemStack.getItemMeta()).getBlockState(); - return creatureSpawner.getSpawnedType().name(); + return Optional.ofNullable(creatureSpawner.getSpawnedType()).map(EntityType::name).orElse(null); } return "PIG"; diff --git a/src/main/java/com/bgsoftware/superiorskyblock/island/algorithm/DefaultIslandCalculationAlgorithm.java b/src/main/java/com/bgsoftware/superiorskyblock/island/algorithm/DefaultIslandCalculationAlgorithm.java index 9fdce794a..6a8423cd1 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/island/algorithm/DefaultIslandCalculationAlgorithm.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/island/algorithm/DefaultIslandCalculationAlgorithm.java @@ -24,7 +24,6 @@ import com.bgsoftware.superiorskyblock.island.IslandUtils; import com.bgsoftware.superiorskyblock.world.chunk.ChunkLoadReason; import org.bukkit.Location; -import org.bukkit.block.CreatureSpawner; import java.math.BigInteger; import java.util.Collection; @@ -127,20 +126,19 @@ public CompletableFuture calculateIsland(Island island) // Calculate spawner counts for (SpawnerInfo spawnerInfo : spawnersToCheck) { try { - CreatureSpawner creatureSpawner = (CreatureSpawner) spawnerInfo.location.getBlock().getState(); - blockKey = Keys.ofSpawner(creatureSpawner.getSpawnedType(), spawnerInfo.location); + blockKey = Keys.of(spawnerInfo.location.getBlock()); blockCount = spawnerInfo.spawnerCount; if (blockCount <= 0) { Pair spawnersProviderInfo = plugin.getProviders() .getSpawnersProvider().getSpawner(spawnerInfo.location); - String entityType = spawnersProviderInfo.getValue(); - if (entityType == null) - entityType = creatureSpawner.getSpawnedType().name(); - blockCount = spawnersProviderInfo.getKey(); - blockKey = Keys.ofSpawner(entityType, spawnerInfo.location); + + String entityType = spawnersProviderInfo.getValue(); + if (entityType != null) { + blockKey = Keys.ofSpawner(entityType, spawnerInfo.location); + } } blockCounts.addCounts(blockKey, blockCount);