1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Some stuff + fixes #2560

This commit is contained in:
TheBusyBiscuit 2020-12-05 13:54:46 +01:00
parent 282367d6ff
commit c84200d2c4
13 changed files with 221 additions and 58 deletions

View File

@ -29,6 +29,12 @@
#### Changes
* Performance optimizations for Cargo networks
* Removed an old version of bStats
* General performance improvements
* Improvements to the BlockBreakHandler (API)
#### Fixes
* Fixed "block-explosions" (e.g. beds in Nether) not properly respecting explosion-resistant blocks
* Fixed #2560
## Release Candidate 18 (03 Dec 2020)

View File

@ -2,18 +2,48 @@ package io.github.thebusybiscuit.slimefun4.core.handlers;
import java.util.List;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
@FunctionalInterface
public interface BlockBreakHandler extends ItemHandler {
public abstract class BlockBreakHandler implements ItemHandler {
boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops);
private final boolean allowAndroids;
private final boolean allowExplosions;
public BlockBreakHandler(boolean allowAndroids, boolean allowExplosions) {
this.allowAndroids = allowAndroids;
this.allowExplosions = allowExplosions;
}
@ParametersAreNonnullByDefault
public abstract void onPlayerBreak(BlockBreakEvent e, ItemStack item, List<ItemStack> drops);
@ParametersAreNonnullByDefault
public void onExplode(Block b, List<ItemStack> drops) {
// This can be overridden, if necessary
}
@ParametersAreNonnullByDefault
public void onAndroidBreak(AndroidMineEvent e) {
// This can be overridden, if necessary
}
public boolean isExplosionAllowed() {
return allowExplosions;
}
public boolean isAndroidAllowed() {
return allowAndroids;
}
@Override
default Class<? extends ItemHandler> getIdentifier() {
public Class<? extends ItemHandler> getIdentifier() {
return BlockBreakHandler.class;
}
}

View File

@ -1,5 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.handlers;
import javax.annotation.Nonnull;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
@ -31,7 +33,7 @@ public abstract class BlockPlaceHandler implements ItemHandler {
* @param e
* The corresponding {@link BlockPlaceEvent}
*/
public abstract void onPlayerPlace(BlockPlaceEvent e);
public abstract void onPlayerPlace(@Nonnull BlockPlaceEvent e);
/**
* This method is called whenever a {@link BlockPlacer} places this {@link Block}.
@ -42,7 +44,7 @@ public abstract class BlockPlaceHandler implements ItemHandler {
* @param e
* The corresponding {@link BlockPlacerPlaceEvent}
*/
public void onBlockPlacerPlace(BlockPlacerPlaceEvent e) {
public void onBlockPlacerPlace(@Nonnull BlockPlacerPlaceEvent e) {
// This can be overridden, if necessary
}

View File

@ -30,7 +30,7 @@ final class CargoUtils {
/**
* These are the slots where our filter items sit.
*/
static final int[] FILTER_SLOTS = { 19, 20, 21, 28, 29, 30, 37, 38, 39 };
private static final int[] FILTER_SLOTS = { 19, 20, 21, 28, 29, 30, 37, 38, 39 };
private CargoUtils() {}
@ -222,11 +222,11 @@ final class CargoUtils {
int maxSlot = range[1];
for (int slot = minSlot; slot < maxSlot; slot++) {
ItemStack is = contents[slot];
ItemStack item = contents[slot];
if (matchesFilter(network, node, is)) {
if (matchesFilter(network, node, item)) {
inv.setItem(slot, null);
return new ItemStackAndInteger(is, slot);
return new ItemStackAndInteger(item, slot);
}
}
@ -330,9 +330,9 @@ final class CargoUtils {
static DirtyChestMenu getChestMenu(@Nonnull Block block) {
if (BlockStorage.hasInventory(block)) {
return BlockStorage.getInventory(block);
} else {
return BlockStorage.getUniversalInventory(block);
}
return BlockStorage.getUniversalInventory(block);
}
static boolean matchesFilter(@Nonnull AbstractItemNetwork network, @Nonnull Block node, @Nullable ItemStack item) {

View File

@ -92,7 +92,7 @@ class ItemFilter implements Predicate<ItemStack> {
this.checkLore = Objects.equals(blockData.getString("filter-lore"), "true");
this.rejectOnMatch = !Objects.equals(blockData.getString("filter-type"), "whitelist");
for (int slot : CargoUtils.FILTER_SLOTS) {
for (int slot : CargoUtils.getWhitelistBlacklistSlots()) {
ItemStack stack = menu.getItemInSlot(slot);
if (stack != null && stack.getType() != Material.AIR) {

View File

@ -1,17 +1,24 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.blocks;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.math.DoubleHandler;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
@ -29,12 +36,14 @@ public class HologramProjector extends SlimefunItem {
private static final String OFFSET_PARAMETER = "offset";
@ParametersAreNonnullByDefault
public HologramProjector(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
super(category, item, recipeType, recipe, recipeOutput);
addItemHandler(onPlace(), onRightClick(), onBreak());
}
@Nonnull
private BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) {
@ -51,13 +60,28 @@ public class HologramProjector extends SlimefunItem {
};
}
@Nonnull
private BlockBreakHandler onBreak() {
return (e, item, fortune, drops) -> {
remove(e.getBlock());
return true;
return new BlockBreakHandler(true, true) {
@Override
public void onPlayerBreak(BlockBreakEvent e, ItemStack item, List<ItemStack> drops) {
remove(e.getBlock());
}
@Override
public void onExplode(Block b, List<ItemStack> drops) {
remove(b);
}
@Override
public void onAndroidBreak(AndroidMineEvent e) {
remove(e.getBlock());
}
};
}
@Nonnull
public BlockUseHandler onRightClick() {
return e -> {
e.cancel();
@ -71,7 +95,7 @@ public class HologramProjector extends SlimefunItem {
};
}
private static void openEditor(Player p, Block projector) {
private static void openEditor(@Nonnull Player p, @Nonnull Block projector) {
ChestMenu menu = new ChestMenu(SlimefunPlugin.getLocalization().getMessage(p, "machines.HOLOGRAM_PROJECTOR.inventory-title"));
menu.addItem(0, new CustomItem(Material.NAME_TAG, "&7Text &e(Click to edit)", "", "&r" + ChatColors.color(BlockStorage.getLocationInfo(projector.getLocation(), "text"))));
@ -104,7 +128,7 @@ public class HologramProjector extends SlimefunItem {
menu.open(p);
}
private static ArmorStand getArmorStand(Block projector, boolean createIfNoneExists) {
private static ArmorStand getArmorStand(@Nonnull Block projector, boolean createIfNoneExists) {
String nametag = BlockStorage.getLocationInfo(projector.getLocation(), "text");
double offset = Double.parseDouble(BlockStorage.getLocationInfo(projector.getLocation(), OFFSET_PARAMETER));
Location l = new Location(projector.getWorld(), projector.getX() + 0.5, projector.getY() + offset, projector.getZ() + 0.5);
@ -128,7 +152,7 @@ public class HologramProjector extends SlimefunItem {
return hologram;
}
private static void remove(Block b) {
private static void remove(@Nonnull Block b) {
ArmorStand hologram = getArmorStand(b, false);
if (hologram != null) {

View File

@ -55,9 +55,11 @@ public class BlockListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onBlockPlaceExisting(BlockPlaceEvent e) {
// This prevents Players from placing a block where another block already exists
// While this can cause ghost blocks it also prevents them from replacing grass
// or saplings etc...
/*
* This prevents Players from placing a block where another block already exists.
* While this can cause ghost blocks it also prevents them from replacing grass
* or saplings etc...
*/
if (BlockStorage.hasBlockInfo(e.getBlock())) {
e.setCancelled(true);
}
@ -89,18 +91,18 @@ public class BlockListener implements Listener {
return;
}
checkForSensitiveBlockAbove(e.getPlayer(), e.getBlock());
ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
checkForSensitiveBlockAbove(e, item);
int fortune = getBonusDropsWithFortune(item, e.getBlock());
List<ItemStack> drops = new ArrayList<>();
if (item.getType() != Material.AIR) {
if (!e.isCancelled() && item.getType() != Material.AIR) {
callToolHandler(e, item, fortune, drops);
}
if (!e.isCancelled()) {
callBlockHandler(e, item, fortune, drops);
callBlockHandler(e, item, drops);
}
dropItems(e, drops);
@ -120,7 +122,7 @@ public class BlockListener implements Listener {
}
@ParametersAreNonnullByDefault
private void callBlockHandler(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
private void callBlockHandler(BlockBreakEvent e, ItemStack item, List<ItemStack> drops) {
SlimefunItem sfItem = BlockStorage.check(e.getBlock());
if (sfItem == null && SlimefunPlugin.getBlockDataService().isTileEntity(e.getBlock().getType())) {
@ -140,7 +142,7 @@ public class BlockListener implements Listener {
return;
}
} else {
sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onBlockBreak(e, item, fortune, drops));
sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(e, item, drops));
}
drops.addAll(sfItem.getDrops());
@ -150,13 +152,15 @@ public class BlockListener implements Listener {
@ParametersAreNonnullByDefault
private void dropItems(BlockBreakEvent e, List<ItemStack> drops) {
if (!drops.isEmpty()) {
e.getBlock().setType(Material.AIR);
if (!drops.isEmpty() && !e.isCancelled()) {
// Notify plugins like CoreProtect
SlimefunPlugin.getProtectionManager().logAction(e.getPlayer(), e.getBlock(), ProtectableAction.BREAK_BLOCK);
// Fixes #2560
if (e.isDropItems()) {
// Disable normal block drops
e.setDropItems(false);
for (ItemStack drop : drops) {
if (drop != null && drop.getType() != Material.AIR) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop);
@ -177,8 +181,8 @@ public class BlockListener implements Listener {
* The {@link Block} that was broken
*/
@ParametersAreNonnullByDefault
private void checkForSensitiveBlockAbove(Player p, Block b) {
Block blockAbove = b.getRelative(BlockFace.UP);
private void checkForSensitiveBlockAbove(BlockBreakEvent e, ItemStack item) {
Block blockAbove = e.getBlock().getRelative(BlockFace.UP);
if (SlimefunTag.SENSITIVE_MATERIALS.isTagged(blockAbove.getType())) {
SlimefunItem sfItem = BlockStorage.check(blockAbove);
@ -187,13 +191,29 @@ public class BlockListener implements Listener {
SlimefunBlockHandler blockHandler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getId());
if (blockHandler != null) {
if (blockHandler.onBreak(p, blockAbove, sfItem, UnregisterReason.PLAYER_BREAK)) {
if (blockHandler.onBreak(e.getPlayer(), blockAbove, sfItem, UnregisterReason.PLAYER_BREAK)) {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), BlockStorage.retrieve(blockAbove));
blockAbove.setType(Material.AIR);
}
} else {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), BlockStorage.retrieve(blockAbove));
/*
* We create a dummy here to pass onto the BlockBreakHandler.
* This will set the correct block context.
*/
BlockBreakEvent dummyEvent = new BlockBreakEvent(blockAbove, e.getPlayer());
List<ItemStack> drops = new ArrayList<>();
drops.addAll(sfItem.getDrops(e.getPlayer()));
sfItem.callItemHandler(BlockBreakHandler.class, handler -> handler.onPlayerBreak(dummyEvent, item, drops));
blockAbove.setType(Material.AIR);
if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) {
for (ItemStack drop : drops) {
if (drop != null && drop.getType() != Material.AIR) {
blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop);
}
}
}
}
}
}

View File

@ -1,6 +1,8 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull;
@ -9,15 +11,29 @@ import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.core.attributes.WitherProof;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
/**
* The {@link ExplosionsListener} is a {@link Listener} which listens to any explosion events.
* Any {@link WitherProof} block is excluded from these explosions and this {@link Listener} also
* calls the explosive part of the {@link BlockBreakHandler}.
*
* @author TheBusyBiscuit
*
* @see BlockBreakHandler
* @see WitherProof
*
*/
public class ExplosionsListener implements Listener {
public ExplosionsListener(@Nonnull SlimefunPlugin plugin) {
@ -26,12 +42,19 @@ public class ExplosionsListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent e) {
Iterator<Block> blocks = e.blockList().iterator();
removeResistantBlocks(e.blockList().iterator());
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockExplode(BlockExplodeEvent e) {
removeResistantBlocks(e.blockList().iterator());
}
private void removeResistantBlocks(@Nonnull Iterator<Block> blocks) {
while (blocks.hasNext()) {
Block block = blocks.next();
SlimefunItem item = BlockStorage.check(block);
if (item != null) {
blocks.remove();
@ -41,6 +64,24 @@ public class ExplosionsListener implements Listener {
if (blockHandler != null) {
success = blockHandler.onBreak(null, block, item, UnregisterReason.EXPLODE);
} else {
item.callItemHandler(BlockBreakHandler.class, handler -> {
if (handler.isExplosionAllowed()) {
BlockStorage.clearBlockInfo(block);
block.setType(Material.AIR);
List<ItemStack> drops = new ArrayList<>();
handler.onExplode(block, drops);
for (ItemStack drop : drops) {
if (drop != null && drop.getType() != Material.AIR) {
block.getWorld().dropItemNaturally(block.getLocation(), drop);
}
}
}
});
return;
}
if (success) {

View File

@ -0,0 +1,32 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
import javax.annotation.Nonnull;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
public class MiningAndroidListener implements Listener {
public MiningAndroidListener(@Nonnull SlimefunPlugin plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler(ignoreCancelled = true)
public void onEntityExplode(AndroidMineEvent e) {
SlimefunItem item = BlockStorage.check(e.getBlock());
item.callItemHandler(BlockBreakHandler.class, handler -> {
if (handler.isAndroidAllowed()) {
handler.onAndroidBreak(e);
} else {
e.setCancelled(true);
}
});
}
}

View File

@ -4,6 +4,7 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
@ -16,8 +17,10 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
*
* @author TheBusyBiscuit
*
* @deprecated Please use the {@link BlockBreakHandler} instead.
*
*/
@Deprecated
@FunctionalInterface
public interface SlimefunBlockHandler {

View File

@ -36,6 +36,7 @@ import io.github.thebusybiscuit.slimefun4.core.attributes.Placeable;
import io.github.thebusybiscuit.slimefun4.core.attributes.Radioactive;
import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.researching.Research;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
@ -1039,6 +1040,17 @@ public class SlimefunItem implements Placeable {
return SlimefunPlugin.getRegistry().getPublicItemHandlers().computeIfAbsent(identifier, c -> new HashSet<>());
}
/**
* This has been deprecated.
*
* @deprecated Please use {@link #addItemHandler(ItemHandler...)} and {@link BlockBreakHandler} instead
*
* @param id
* The id
* @param handler
* The handler
*/
@Deprecated
public static void registerBlockHandler(String id, SlimefunBlockHandler handler) {
SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler);
}

View File

@ -19,6 +19,7 @@ import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow;
* @deprecated This interface is not designed to be used by addons.
*
*/
@Deprecated
public interface InventoryBlock {
/**

View File

@ -380,19 +380,14 @@ public class BlockStorage {
* @return the SlimefunItem's ItemStack corresponding to the block if it has one, otherwise null
*/
@Nullable
public static ItemStack retrieve(Block block) {
if (!hasBlockInfo(block)) {
public static ItemStack retrieve(@Nonnull Block block) {
SlimefunItem item = check(block);
if (item == null) {
return null;
} else {
String id = getLocationInfo(block.getLocation(), "id");
SlimefunItem item = SlimefunItem.getByID(id);
clearBlockInfo(block);
if (item == null) {
return null;
} else {
return item.getItem();
}
return item.getItem();
}
}
@ -668,6 +663,11 @@ public class BlockStorage {
return id == null ? null : SlimefunItem.getByID(id);
}
public static boolean check(Block block, String slimefunItem) {
String id = checkID(block);
return id != null && id.equals(slimefunItem);
}
@Nullable
public static String checkID(@Nonnull Block b) {
if (SlimefunPlugin.getBlockDataService().isTileEntity(b.getType())) {
@ -681,16 +681,8 @@ public class BlockStorage {
return checkID(b.getLocation());
}
public static boolean check(Block block, String slimefunItem) {
String id = checkID(block);
return id != null && id.equals(slimefunItem);
}
public static String checkID(Location l) {
if (!hasBlockInfo(l)) {
return null;
}
@Nullable
public static String checkID(@Nonnull Location l) {
return getLocationInfo(l, "id");
}
@ -703,7 +695,7 @@ public class BlockStorage {
return id != null && id.equals(slimefunItem);
}
public static boolean isWorldLoaded(World world) {
public static boolean isWorldLoaded(@Nonnull World world) {
return SlimefunPlugin.getRegistry().getWorlds().containsKey(world.getName());
}