Move on power source remove/add
This commit is contained in:
parent
b3e0cf3949
commit
48e79a2d51
@ -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 net.milkbowl.vault.permission.Permission;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -46,7 +46,6 @@ public class PistonExtendsChecker {
|
|||||||
if (!BlockPiston.a(var1, this.a, this.c, this.d, false, this.d)) {
|
if (!BlockPiston.a(var1, this.a, this.c, this.d, false, this.d)) {
|
||||||
if (var1.o() == EnumPistonReaction.DESTROY) {
|
if (var1.o() == EnumPistonReaction.DESTROY) {
|
||||||
this.f.add(this.c);
|
this.f.add(this.c);
|
||||||
System.out.println("A");
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
@ -60,7 +59,6 @@ public class PistonExtendsChecker {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("B");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package ru.redguy.extendedpistons;
|
package ru.redguy.extendedpistons;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -9,11 +8,8 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.BlockPistonExtendEvent;
|
import org.bukkit.event.block.*;
|
||||||
import org.bukkit.event.block.BlockPistonRetractEvent;
|
|
||||||
import org.bukkit.event.block.BlockRedstoneEvent;
|
|
||||||
import org.bukkit.inventory.DoubleChestInventory;
|
import org.bukkit.inventory.DoubleChestInventory;
|
||||||
import org.bukkit.inventory.InventoryHolder;
|
|
||||||
import org.bukkit.material.PistonBaseMaterial;
|
import org.bukkit.material.PistonBaseMaterial;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@ -28,13 +24,190 @@ public class WorldListener implements Listener {
|
|||||||
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
||||||
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
||||||
|
|
||||||
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(),e.getBlock().getLocation(),piston.getFacing(),true,maxBlocks);
|
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), e.getBlock().getLocation(), piston.getFacing(), true, maxBlocks);
|
||||||
|
|
||||||
if (!checker.a()) {
|
if (!checker.a()) {
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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)
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||||
public void onRedstone(BlockRedstoneEvent e) {
|
public void onRedstone(BlockRedstoneEvent e) {
|
||||||
HashMap<BlockFace, BlockState> states = new HashMap<>();
|
HashMap<BlockFace, BlockState> states = new HashMap<>();
|
||||||
@ -67,12 +240,12 @@ public class WorldListener implements Listener {
|
|||||||
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
||||||
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
||||||
|
|
||||||
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(),block.getLocation(),piston.getFacing(),false,maxBlocks);
|
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks);
|
||||||
boolean isMovable = checker.a();
|
boolean isMovable = checker.a();
|
||||||
List<Block> blocks = checker.getMovedBlocksObjects();
|
List<Block> blocks = checker.getMovedBlocksObjects();
|
||||||
List<Block> toBreak = checker.getBrokenBlocksObjects();
|
List<Block> toBreak = checker.getBrokenBlocksObjects();
|
||||||
|
|
||||||
if(isMovable) {
|
if (isMovable) {
|
||||||
BlockPistonRetractEvent ev = new BlockPistonRetractEvent(block, blocks, piston.getFacing());
|
BlockPistonRetractEvent ev = new BlockPistonRetractEvent(block, blocks, piston.getFacing());
|
||||||
Bukkit.getPluginManager().callEvent(ev);
|
Bukkit.getPluginManager().callEvent(ev);
|
||||||
if (ev.isCancelled()) continue;
|
if (ev.isCancelled()) continue;
|
||||||
@ -124,34 +297,40 @@ public class WorldListener implements Listener {
|
|||||||
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
PistonBaseMaterial piston = (PistonBaseMaterial) state.getData();
|
||||||
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation());
|
||||||
|
|
||||||
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(),block.getLocation(),piston.getFacing(),true,maxBlocks);
|
PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks);
|
||||||
boolean isMovable = checker.a();
|
boolean isMovable = checker.a();
|
||||||
List<Block> blocks = checker.getMovedBlocksObjects();
|
List<Block> blocks = checker.getMovedBlocksObjects();
|
||||||
List<Block> toBreak = checker.getBrokenBlocksObjects();
|
List<Block> toBreak = checker.getBrokenBlocksObjects();
|
||||||
|
|
||||||
upper: for (Block b : blocks) {
|
upper:
|
||||||
|
for (Block b : blocks) {
|
||||||
Block b2 = b.getRelative(piston.getFacing());
|
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());
|
faces.remove(piston.getFacing().getOppositeFace());
|
||||||
int chests = 0;
|
int chests = 0;
|
||||||
for (BlockFace blockFace : faces) {
|
for (BlockFace blockFace : faces) {
|
||||||
Block b3 = b2.getRelative(blockFace);
|
Block b3 = b2.getRelative(blockFace);
|
||||||
if(b3.getType() == Material.CHEST) {
|
if (b3.getType() == Material.CHEST) {
|
||||||
chests++;
|
chests++;
|
||||||
Chest chest = (Chest)b3.getState();
|
Chest chest = (Chest) b3.getState();
|
||||||
if(chest.getInventory() instanceof DoubleChestInventory) {
|
if (chest.getInventory() instanceof DoubleChestInventory) {
|
||||||
isMovable = false;
|
isMovable = false;
|
||||||
break upper;
|
break upper;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(chests > 1) {
|
if (chests > 1) {
|
||||||
isMovable = false;
|
isMovable = false;
|
||||||
break;
|
break;
|
||||||
} else if(chests == 1) {
|
} else if (chests == 1) {
|
||||||
if(b.getType() == Material.CHEST) {
|
if (b.getType() == Material.CHEST) {
|
||||||
Chest chest = (Chest)b.getState();
|
Chest chest = (Chest) b.getState();
|
||||||
if(chest.getInventory() instanceof DoubleChestInventory) {
|
if (chest.getInventory() instanceof DoubleChestInventory) {
|
||||||
isMovable = false;
|
isMovable = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user