Remove NMS

Break blocks
Pulling chests
This commit is contained in:
Ilya 2022-02-10 18:32:11 +03:00
parent 956129f97d
commit 7829f4b145

View File

@ -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,8 +60,12 @@ public class WorldListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onRedstone(BlockRedstoneEvent e) {
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) return;
if (e.getOldCurrent() != 0 && e.getNewCurrent() == 0) {
ArrayList<BlockFace> checkedBlockFaces = new ArrayList<BlockFace>() {{
add(BlockFace.SELF);
add(BlockFace.UP);
@ -67,9 +74,53 @@ public class WorldListener implements Listener {
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,10 +145,10 @@ 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;
@ -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;
}
}
}