diff --git a/CHANGELOG.md b/CHANGELOG.md index b4fc62cf8..d57b12736 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ * Fixed #1992 * Possibly fixed #1951 * Fixed tab completion for /sf give showing players instead of amounts +* Fixed #1993 ## Release Candidate 12 (27 May 2020) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#12 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/BlockPlacer.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/BlockPlacer.java index 1e8b2ee8b..b2ee48af0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/BlockPlacer.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/BlockPlacer.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.blocks; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.bukkit.Effect; @@ -8,13 +9,16 @@ import org.bukkit.Material; import org.bukkit.Nameable; import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.bukkit.block.CreatureSpawner; import org.bukkit.block.Dispenser; +import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections; import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; +import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; @@ -47,11 +51,12 @@ public class BlockPlacer extends SimpleSlimefunItem { e.setCancelled(true); if ((facedBlock.getType() == null || facedBlock.getType() == Material.AIR) && e.getItem().getType().isBlock() && !isBlacklisted(e.getItem().getType())) { - SlimefunItem sfItem = SlimefunItem.getByItem(e.getItem()); + SlimefunItem item = SlimefunItem.getByItem(e.getItem()); - if (sfItem != null) { - if (!SlimefunPlugin.getRegistry().getBlockHandlers().containsKey(sfItem.getID())) { - placeSlimefunBlock(sfItem, e.getItem(), facedBlock, dispenser); + if (item != null) { + // Check if this Item can even be placed down + if (!(item instanceof NotPlaceable) && !SlimefunPlugin.getRegistry().getBlockHandlers().containsKey(item.getID())) { + placeSlimefunBlock(item, e.getItem(), facedBlock, dispenser); } } else { @@ -75,10 +80,20 @@ public class BlockPlacer extends SimpleSlimefunItem { return false; } - private void placeSlimefunBlock(SlimefunItem sfItem, ItemStack item, Block facedBlock, Dispenser dispenser) { - facedBlock.setType(item.getType()); - BlockStorage.store(facedBlock, sfItem.getID()); - facedBlock.getWorld().playEffect(facedBlock.getLocation(), Effect.STEP_SOUND, item.getType()); + private void placeSlimefunBlock(SlimefunItem sfItem, ItemStack item, Block block, Dispenser dispenser) { + block.setType(item.getType()); + BlockStorage.store(block, sfItem.getID()); + block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, item.getType()); + + if (item.getType() == Material.SPAWNER && sfItem instanceof RepairedSpawner) { + Optional entity = ((RepairedSpawner) sfItem).getEntityType(item); + + if (entity.isPresent()) { + CreatureSpawner spawner = (CreatureSpawner) block.getState(); + spawner.setSpawnedType(entity.get()); + spawner.update(true, false); + } + } if (dispenser.getInventory().containsAtLeast(item, 2)) { dispenser.getInventory().removeItem(new CustomItem(item, 1)); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/RepairedSpawner.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/RepairedSpawner.java index fb18ca592..a7442fd07 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/RepairedSpawner.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/RepairedSpawner.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.blocks; import java.util.Locale; +import java.util.Optional; import org.bukkit.ChatColor; import org.bukkit.block.CreatureSpawner; @@ -25,18 +26,12 @@ public class RepairedSpawner extends SimpleSlimefunItem { public BlockPlaceHandler getItemHandler() { return (e, item) -> { // We need to explicitly ignore the lore here - if (SlimefunUtils.isItemSimilar(item, SlimefunItems.REPAIRED_SPAWNER, false)) { - EntityType type = null; + if (SlimefunUtils.isItemSimilar(item, SlimefunItems.REPAIRED_SPAWNER, false, false)) { + Optional entity = getEntityType(item); - for (String line : item.getItemMeta().getLore()) { - if (ChatColor.stripColor(line).startsWith("Type: ") && !line.contains("")) { - type = EntityType.valueOf(ChatColor.stripColor(line).replace("Type: ", "").replace(' ', '_').toUpperCase(Locale.ROOT)); - } - } - - if (type != null) { + if (entity.isPresent()) { CreatureSpawner spawner = (CreatureSpawner) e.getBlock().getState(); - spawner.setSpawnedType(type); + spawner.setSpawnedType(entity.get()); spawner.update(true, false); } @@ -48,4 +43,22 @@ public class RepairedSpawner extends SimpleSlimefunItem { }; } + /** + * This method tries to obtain an {@link EntityType} from a given {@link ItemStack}. + * The provided {@link ItemStack} must be a {@link RepairedSpawner} item. + * + * @param item + * @return + */ + public Optional getEntityType(ItemStack item) { + for (String line : item.getItemMeta().getLore()) { + if (ChatColor.stripColor(line).startsWith("Type: ") && !line.contains("")) { + EntityType type = EntityType.valueOf(ChatColor.stripColor(line).replace("Type: ", "").replace(' ', '_').toUpperCase(Locale.ROOT)); + return Optional.of(type); + } + } + + return Optional.empty(); + } + }