Remove NMS
Break blocks Pulling chests
This commit is contained in:
parent
956129f97d
commit
7829f4b145
@ -1,6 +1,6 @@
|
||||
package ru.redguy.extendedpistons;
|
||||
|
||||
import net.minecraft.server.v1_12_R1.BlockPosition;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
@ -10,12 +10,12 @@ import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockPistonExtendEvent;
|
||||
import org.bukkit.event.block.BlockPistonRetractEvent;
|
||||
import org.bukkit.event.block.BlockRedstoneEvent;
|
||||
import org.bukkit.material.PistonBaseMaterial;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.*;
|
||||
|
||||
public class WorldListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.LOW,ignoreCancelled = true)
|
||||
@ -24,10 +24,10 @@ public class WorldListener implements Listener {
|
||||
Block block = e.getBlock();
|
||||
BlockState state = block.getState();
|
||||
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
||||
Player player = findNearbyPlayer(block.getLocation());
|
||||
int maxBlocks = 20;//TODO: get max length
|
||||
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
||||
|
||||
ArrayList<Block> blocks = new ArrayList<>();
|
||||
Block toBreak = null;
|
||||
boolean isMovable = true;
|
||||
Block firstBlock = block.getRelative(piston.getFacing());
|
||||
if (firstBlock.isLiquid() || firstBlock.isEmpty()) {
|
||||
@ -36,11 +36,14 @@ public class WorldListener implements Listener {
|
||||
Block b = block.getRelative(piston.getFacing(), i);
|
||||
if (b.isLiquid() || b.isEmpty()) break;
|
||||
if (b.getPistonMoveReaction().equals(PistonMoveReaction.IGNORE) || b.getPistonMoveReaction().equals(PistonMoveReaction.BLOCK)) {
|
||||
if (!(b.getType() == Material.CHEST || b.getType() == Material.ENDER_CHEST || b.getType() == Material.FURNACE || b.getType() == Material.BURNING_FURNACE)) {
|
||||
if (!(b.getType() == Material.CHEST || b.getType() == Material.FURNACE || b.getType() == Material.BURNING_FURNACE)) {
|
||||
isMovable = false;
|
||||
} else {
|
||||
blocks.add(b);//TODO: проверять на возух
|
||||
blocks.add(b);
|
||||
}
|
||||
} else if(b.getPistonMoveReaction().equals(PistonMoveReaction.BREAK)) {
|
||||
toBreak = b;
|
||||
break;
|
||||
} else {
|
||||
blocks.add(b);
|
||||
}
|
||||
@ -57,19 +60,67 @@ public class WorldListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onRedstone(BlockRedstoneEvent e) {
|
||||
Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> {
|
||||
if (e.getOldCurrent() != 0 && e.getNewCurrent() == 0) return;
|
||||
HashMap<BlockFace,BlockState> states = new HashMap<>();
|
||||
for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) {
|
||||
states.put(face,e.getBlock().getRelative(face).getState());
|
||||
}
|
||||
Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE,() -> {
|
||||
if (e.getOldCurrent() != 0 && e.getNewCurrent() == 0) {
|
||||
ArrayList<BlockFace> checkedBlockFaces = new ArrayList<BlockFace>() {{
|
||||
add(BlockFace.SELF);
|
||||
add(BlockFace.UP);
|
||||
add(BlockFace.DOWN);
|
||||
}};
|
||||
if(e.getBlock().getType().equals(Material.REDSTONE_WIRE)) {
|
||||
if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) {
|
||||
for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) {
|
||||
Block block = e.getBlock().getRelative(blockFace);
|
||||
net.minecraft.server.v1_12_R1.World nmsWorld = ((org.bukkit.craftbukkit.v1_12_R1.CraftWorld) block.getWorld()).getHandle();
|
||||
BlockPosition bp = new BlockPosition(block.getX(), block.getY(), block.getZ());
|
||||
if (nmsWorld.getBlockPower(bp) != 0) checkedBlockFaces.add(blockFace);
|
||||
if (block.getBlockPower() == 0) checkedBlockFaces.add(blockFace);
|
||||
}
|
||||
} else {
|
||||
checkedBlockFaces.add(BlockFace.NORTH);
|
||||
checkedBlockFaces.add(BlockFace.SOUTH);
|
||||
checkedBlockFaces.add(BlockFace.EAST);
|
||||
checkedBlockFaces.add(BlockFace.WEST);
|
||||
}
|
||||
for (BlockFace face : checkedBlockFaces) {
|
||||
Block block = e.getBlock().getRelative(face);
|
||||
BlockState state = states.get(face);
|
||||
if (state.getType().equals(Material.PISTON_STICKY_BASE)) {
|
||||
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
||||
|
||||
Block b = block.getRelative(piston.getFacing(),2);
|
||||
if(b.getType().equals(Material.CHEST)||b.getType().equals(Material.FURNACE)||b.getType().equals(Material.BURNING_FURNACE)) {
|
||||
Block b2 = block.getRelative(piston.getFacing());
|
||||
BlockPistonRetractEvent ev = new BlockPistonRetractEvent(block, Lists.newArrayList(b2),piston.getFacing());
|
||||
Bukkit.getPluginManager().callEvent(ev);
|
||||
if(ev.isCancelled()) continue;
|
||||
b2.setType(b.getType());
|
||||
b2.setData(b.getData());
|
||||
if (b.getType().equals(Material.CHEST)) {
|
||||
Chest oldChest = (Chest) b.getState();
|
||||
Chest newChest = (Chest) b2.getState();
|
||||
newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents());
|
||||
oldChest.getBlockInventory().clear();
|
||||
} else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) {
|
||||
Furnace oldFurnace = (Furnace) b.getState();
|
||||
Furnace newFurnace = (Furnace) b2.getState();
|
||||
newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents());
|
||||
oldFurnace.getInventory().clear();
|
||||
}
|
||||
b.setType(Material.AIR);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ArrayList<BlockFace> checkedBlockFaces = new ArrayList<BlockFace>() {{
|
||||
add(BlockFace.SELF);
|
||||
add(BlockFace.UP);
|
||||
add(BlockFace.DOWN);
|
||||
}};
|
||||
if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) {
|
||||
for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) {
|
||||
Block block = e.getBlock().getRelative(blockFace);
|
||||
if (block.getBlockPower() != 0) checkedBlockFaces.add(blockFace);
|
||||
}
|
||||
} else {
|
||||
checkedBlockFaces.add(BlockFace.NORTH);
|
||||
@ -80,10 +131,9 @@ public class WorldListener implements Listener {
|
||||
for (BlockFace face : checkedBlockFaces) {
|
||||
Block block = e.getBlock().getRelative(face);
|
||||
if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) {
|
||||
BlockState state = block.getState();
|
||||
BlockState state = states.get(face);
|
||||
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
||||
Player player = findNearbyPlayer(block.getLocation());
|
||||
int maxBlocks = 20;//TODO: get max length
|
||||
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
||||
|
||||
ArrayList<Block> blocks = new ArrayList<>();
|
||||
Block toBreak = null;
|
||||
@ -95,12 +145,12 @@ public class WorldListener implements Listener {
|
||||
Block b = block.getRelative(piston.getFacing(), i);
|
||||
if (b.isLiquid() || b.isEmpty()) break;
|
||||
if (b.getPistonMoveReaction().equals(PistonMoveReaction.IGNORE) || b.getPistonMoveReaction().equals(PistonMoveReaction.BLOCK)) {
|
||||
if (!(b.getType() == Material.CHEST || b.getType() == Material.ENDER_CHEST || b.getType() == Material.FURNACE || b.getType() == Material.BURNING_FURNACE)) {
|
||||
if (!(b.getType() == Material.CHEST || b.getType() == Material.FURNACE || b.getType() == Material.BURNING_FURNACE)) {
|
||||
isMovable = false;
|
||||
} else {
|
||||
blocks.add(b);//TODO: проверять на возух
|
||||
blocks.add(b);
|
||||
}
|
||||
} else if(b.getPistonMoveReaction().equals(PistonMoveReaction.BREAK)) {
|
||||
} else if (b.getPistonMoveReaction().equals(PistonMoveReaction.BREAK)) {
|
||||
toBreak = b;
|
||||
break;
|
||||
} else {
|
||||
@ -118,17 +168,17 @@ public class WorldListener implements Listener {
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if (event.isCancelled()) return;
|
||||
Collections.reverse(blocks);
|
||||
if(toBreak!=null) toBreak.breakNaturally();
|
||||
if (toBreak != null) toBreak.breakNaturally();
|
||||
for (Block b : blocks) {
|
||||
Block b2 = b.getRelative(piston.getFacing());
|
||||
b2.setType(b.getType());
|
||||
b2.setData(b.getData());
|
||||
if(b.getType().equals(Material.CHEST)) {
|
||||
if (b.getType().equals(Material.CHEST)) {
|
||||
Chest oldChest = (Chest) b.getState();
|
||||
Chest newChest = (Chest) b2.getState();
|
||||
newChest.getBlockInventory().setContents(oldChest.getBlockInventory().getContents());
|
||||
oldChest.getBlockInventory().clear();
|
||||
} else if(b.getType().equals(Material.FURNACE)||b.getType().equals(Material.BURNING_FURNACE)) {
|
||||
} else if (b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE)) {
|
||||
Furnace oldFurnace = (Furnace) b.getState();
|
||||
Furnace newFurnace = (Furnace) b2.getState();
|
||||
newFurnace.getInventory().setContents(oldFurnace.getInventory().getContents());
|
||||
@ -139,10 +189,16 @@ public class WorldListener implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Player findNearbyPlayer(Location location) {
|
||||
return Bukkit.getOnlinePlayers().stream().min(Comparator.comparingDouble(p -> p.getLocation().distance(location))).get();
|
||||
public int findNearbyPlayerMaxBlocks(Location location) {
|
||||
Optional<Player> op = (Optional<Player>) Bukkit.getOnlinePlayers().stream().min(Comparator.comparingDouble(p -> p.getLocation().distance(location)));
|
||||
if(op.isPresent()) {
|
||||
return 20; //TODO
|
||||
} else {
|
||||
return 12;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user