Skip to content

Commit

Permalink
Fixed many errors when placing spawners with no entity inside them (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Sep 17, 2023
1 parent 375fa68 commit d0c1595
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 27 deletions.
@@ -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;
Expand All @@ -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);

}
Expand Up @@ -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;
Expand Down Expand Up @@ -55,9 +55,9 @@ public Pair<Integer, String> 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")
Expand All @@ -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());
}
}

Expand All @@ -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());
}
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
Expand Down
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/bgsoftware/superiorskyblock/core/key/Keys.java
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
Expand Down Expand Up @@ -160,4 +161,10 @@ public static <T extends Key> Key of(Class<T> baseKeyClass, LazyReference<T> 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);
}

}
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Expand Up @@ -2,17 +2,20 @@

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) {
Preconditions.checkNotNull(itemStack, "itemStack parameter cannot be null.");

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";
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -127,20 +126,19 @@ public CompletableFuture<IslandCalculationResult> 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<Integer, String> 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);
Expand Down

0 comments on commit d0c1595

Please sign in to comment.