Remove all usage of old MaterialData and other outdated code

This should make it more future-proof and possibly even work with 1.14 already.
This commit is contained in:
Phoenix616 2019-04-30 21:31:24 +01:00
parent 6773f89221
commit c538f4a3dc
3 changed files with 17 additions and 73 deletions

View File

@ -1,12 +1,10 @@
package com.Acrobot.Breeze.Utils;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.block.data.type.Sign;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.entity.Player;
@ -24,8 +22,8 @@ public class BlockUtil {
* @return Is this block a sign?
*/
public static boolean isSign(Block block) {
return block.getType() == Material.SIGN
|| block.getType() == Material.WALL_SIGN;
BlockData data = block.getBlockData();
return data instanceof Sign || data instanceof WallSign;
}
/**

View File

@ -4,7 +4,6 @@ import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
@ -20,8 +19,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.material.Directional;
import org.bukkit.material.PistonBaseMaterial;
import org.bukkit.metadata.FixedMetadataValue;
import java.util.ArrayList;
@ -78,7 +75,7 @@ public class SignBreak implements Listener {
@EventHandler(ignoreCancelled = true)
public static void onBlockPistonExtend(BlockPistonExtendEvent event) {
for (Block block : getExtendBlocks(event)) {
for (Block block : event.getBlocks()) {
if (!canBlockBeBroken(block, null)) {
event.setCancelled(true);
return;
@ -88,7 +85,7 @@ public class SignBreak implements Listener {
@EventHandler(ignoreCancelled = true)
public static void onBlockPistonRetract(BlockPistonRetractEvent event) {
for (Block block : getRetractBlocks(event)) {
for (Block block : event.getBlocks()) {
if (!canBlockBeBroken(block, null)) {
event.setCancelled(true);
return;
@ -189,55 +186,4 @@ public class SignBreak implements Listener {
return attachedSigns;
}
}
private static List<Block> getRetractBlocks(BlockPistonRetractEvent event) {
try {
return event.getBlocks();
} catch (NoSuchMethodError outdated) { // backwards compatiblity
List<Block> blocks = new ArrayList<>();
Block block = getRetractLocationBlock(event);
if (block != null && !BlockUtil.isSign(block)) {
blocks.add(block);
}
return blocks;
}
}
//Those are fixes for a very old CraftBukkit's piston bug, where piston appears not to be a piston.
private static BlockFace getPistonDirection(Block block) {
return block.getState().getData() instanceof PistonBaseMaterial ? ((Directional) block.getState().getData()).getFacing() : null;
}
private static Block getRetractLocationBlock(BlockPistonRetractEvent event) {
BlockFace pistonDirection = getPistonDirection(event.getBlock());
return pistonDirection != null ? event.getBlock().getRelative((pistonDirection), 2).getLocation().getBlock() : null;
}
private static List<Block> getExtendBlocks(BlockPistonExtendEvent event) {
try {
return event.getBlocks();
} catch (NoSuchMethodError outdated) { // backwards compatiblity
BlockFace pistonDirection = getPistonDirection(event.getBlock());
if (pistonDirection == null) {
return new ArrayList<>();
}
Block piston = event.getBlock();
List<Block> pushedBlocks = new ArrayList<>();
for (int currentBlock = 1; currentBlock < event.getLength() + 1; currentBlock++) {
Block block = piston.getRelative(pistonDirection, currentBlock);
Material blockType = block.getType();
if (blockType == Material.AIR) {
break;
}
pushedBlocks.add(block);
}
return pushedBlocks;
}
}
}

View File

@ -11,8 +11,8 @@ import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Chest;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.material.Attachable;
/**
* @author Acrobot
@ -46,8 +46,9 @@ public class uBlock {
@Deprecated
public static org.bukkit.block.Chest findConnectedChest(Sign sign) {
BlockFace signFace = null;
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
BlockData data = sign.getBlockData();
if (data instanceof WallSign) {
signFace = ((WallSign) data).getFacing().getOppositeFace();
}
return findConnectedChest(sign.getBlock(), signFace);
}
@ -59,9 +60,9 @@ public class uBlock {
public static org.bukkit.block.Chest findConnectedChest(Block block) {
BlockFace signFace = null;
if (BlockUtil.isSign(block)) {
Sign sign = (Sign) block.getState();
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
BlockData data = block.getBlockData();
if (data instanceof WallSign) {
signFace = ((WallSign) data).getFacing().getOppositeFace();
}
}
return findConnectedChest(block, signFace);
@ -92,19 +93,18 @@ public class uBlock {
public static Container findConnectedContainer(Sign sign) {
BlockFace signFace = null;
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
BlockData data = sign.getBlockData();
if (data instanceof WallSign) {
signFace = ((WallSign) data).getFacing().getOppositeFace();
}
return findConnectedContainer(sign.getLocation(), signFace);
}
public static Container findConnectedContainer(Block block) {
BlockFace signFace = null;
if (BlockUtil.isSign(block)) {
Sign sign = (Sign) block.getState();
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
}
BlockData data = block.getBlockData();
if (data instanceof WallSign) {
signFace = ((WallSign) data).getFacing().getOppositeFace();
}
return findConnectedContainer(block.getLocation(), signFace);
}