Move on power source remove/add

This commit is contained in:
Ilya 2022-02-11 18:11:30 +03:00
parent b3e0cf3949
commit 48e79a2d51
3 changed files with 199 additions and 22 deletions

View File

@ -1,4 +1,4 @@
package ru.redguy.extendedpistons; //TODO: slime sides chests move, power supply remove, refactor
package ru.redguy.extendedpistons; //TODO: slime sides chests move, refactor
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;

View File

@ -46,7 +46,6 @@ public class PistonExtendsChecker {
if (!BlockPiston.a(var1, this.a, this.c, this.d, false, this.d)) {
if (var1.o() == EnumPistonReaction.DESTROY) {
this.f.add(this.c);
System.out.println("A");
return true;
} else {
return false;
@ -60,7 +59,6 @@ public class PistonExtendsChecker {
return false;
}
}
System.out.println("B");
return true;
}
}

View File

@ -1,6 +1,5 @@
package ru.redguy.extendedpistons;
import com.google.common.collect.Lists;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -9,11 +8,8 @@ import org.bukkit.entity.Player;
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.event.block.*;
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.material.PistonBaseMaterial;
import java.util.Comparator;
@ -35,6 +31,183 @@ public class WorldListener implements Listener {
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent e) {
Material blockType = e.getBlock().getType();
if (!(blockType.equals(Material.REDSTONE_BLOCK) || blockType.equals(Material.REDSTONE_TORCH_ON))) return;
HashMap<BlockFace, BlockState> states = new HashMap<>();
HashMap<BlockFace, Integer> powers = 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());
powers.put(face, e.getBlock().getRelative(face).getBlockPower());
}
Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> {
if (ExtendedPistons.INSTANCE.isStickyPistonPulls()) {
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 && powers.get(blockFace) != 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();
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks);
boolean isMovable = checker.a();
List<Block> blocks = checker.getMovedBlocksObjects();
List<Block> toBreak = checker.getBrokenBlocksObjects();
if (isMovable) {
BlockPistonRetractEvent ev = new BlockPistonRetractEvent(block, blocks, piston.getFacing());
Bukkit.getPluginManager().callEvent(ev);
if (ev.isCancelled()) continue;
for (Block block1 : toBreak) {
block1.breakNaturally();
}
for (Block b : blocks) {
Block b2 = b.getRelative(piston.getFacing().getOppositeFace());
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);
}
}
}
}
}
});
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent e) {
Material blockType = e.getBlock().getType();
if (!(blockType.equals(Material.REDSTONE_BLOCK) || blockType.equals(Material.REDSTONE_TORCH_ON))) return;
HashMap<BlockFace, BlockState> states = new HashMap<>();
HashMap<BlockFace, Integer> powers = 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());
powers.put(face, e.getBlock().getRelative(face).getBlockPower());
}
Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> {
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 && powers.get(blockFace) == 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);
if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) {
BlockState state = states.get(face);
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks);
boolean isMovable = checker.a();
List<Block> blocks = checker.getMovedBlocksObjects();
List<Block> toBreak = checker.getBrokenBlocksObjects();
upper:
for (Block b : blocks) {
Block b2 = b.getRelative(piston.getFacing());
ArrayList<BlockFace> faces = new ArrayList<BlockFace>() {{
add(BlockFace.NORTH);
add(BlockFace.SOUTH);
add(BlockFace.WEST);
add(BlockFace.EAST);
}};
faces.remove(piston.getFacing().getOppositeFace());
int chests = 0;
for (BlockFace blockFace : faces) {
Block b3 = b2.getRelative(blockFace);
if (b3.getType() == Material.CHEST) {
chests++;
Chest chest = (Chest) b3.getState();
if (chest.getInventory() instanceof DoubleChestInventory) {
isMovable = false;
break upper;
}
}
}
if (chests > 1) {
isMovable = false;
break;
} else if (chests == 1) {
if (b.getType() == Material.CHEST) {
Chest chest = (Chest) b.getState();
if (chest.getInventory() instanceof DoubleChestInventory) {
isMovable = false;
break;
}
}
}
}
if (isMovable) {
BlockPistonExtendedExtendEvent event = new BlockPistonExtendedExtendEvent(block, blocks, piston.getFacing());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return;
Collections.reverse(blocks);
for (Block block1 : toBreak) {
block1.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)) {
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);
}
}
}
}
});
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onRedstone(BlockRedstoneEvent e) {
HashMap<BlockFace, BlockState> states = new HashMap<>();
@ -129,9 +302,15 @@ public class WorldListener implements Listener {
List<Block> blocks = checker.getMovedBlocksObjects();
List<Block> toBreak = checker.getBrokenBlocksObjects();
upper: for (Block b : blocks) {
upper:
for (Block b : blocks) {
Block b2 = b.getRelative(piston.getFacing());
ArrayList<BlockFace> faces = new ArrayList<BlockFace>() {{add(BlockFace.NORTH);add(BlockFace.SOUTH);add(BlockFace.WEST);add(BlockFace.EAST);}};
ArrayList<BlockFace> faces = new ArrayList<BlockFace>() {{
add(BlockFace.NORTH);
add(BlockFace.SOUTH);
add(BlockFace.WEST);
add(BlockFace.EAST);
}};
faces.remove(piston.getFacing().getOppositeFace());
int chests = 0;
for (BlockFace blockFace : faces) {