QUOTE: Life is tough, but so are you.

Fixed double doors - poppy - A feature-rich Minecraft plugin which enhances gaming experience

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git://192.168.2.2/poppy
Log | Files | Refs | README

commit 68a8793325f2028e6f012c89d26d1776baec1c1e
parent 930944cc6a3b2f394e925947fad09a1ee824f4c8
Author: typable <typable.dev@gmail.com>
Date:   Sat,  5 Dec 2020 22:10:53 +0100

Fixed double doors
Diffstat:
Msrc/de/typable/minecrafthub/Main.java | 19+++++++++----------
Msrc/de/typable/minecrafthub/constant/DefaultConstants.java | 3+--
Msrc/de/typable/minecrafthub/event/ChairListener.java | 44++++++++++++++++++--------------------------
Msrc/de/typable/minecrafthub/event/DoubleDoorListener.java | 179+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/de/typable/minecrafthub/event/EventListener.java | 11+++++++----
Msrc/de/typable/minecrafthub/event/StandbyListener.java | 4++--
6 files changed, 131 insertions(+), 129 deletions(-)

diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java @@ -34,13 +34,13 @@ public class Main extends JavaPlugin standbyListener = new StandbyListener(this); pluginManager.registerEvents(standbyListener, this); - + doubleDoorListener = new DoubleDoorListener(); pluginManager.registerEvents(doubleDoorListener, this); - + chairListener = new ChairListener(); pluginManager.registerEvents(chairListener, this); - + eventListener = new EventListener(); pluginManager.registerEvents(eventListener, this); } @@ -57,7 +57,7 @@ public class Main extends JavaPlugin { ItemStack handitem = player.getInventory().getItemInMainHand(); ItemStack headitem = player.getInventory().getHelmet(); - + player.getInventory().setHelmet(handitem); player.getInventory().setItemInMainHand((new ItemStack(Material.AIR))); @@ -69,7 +69,7 @@ public class Main extends JavaPlugin } } } - + if(label.equals("skull")) { if(!player.isOp()) @@ -77,17 +77,17 @@ public class Main extends JavaPlugin player.sendMessage(DefaultConstants.Messages.NOT_ENOUGH_PERMISSION); return true; } - + if(args.length != 1) { return false; } - + ItemStack skull = new ItemStack(Material.PLAYER_HEAD); SkullMeta meta = (SkullMeta) skull.getItemMeta(); meta.setOwner(args[0]); skull.setItemMeta(meta); - + player.getInventory().addItem(skull); } @@ -114,8 +114,7 @@ public class Main extends JavaPlugin boolean enabled = argument.equals("true"); standbyListener.setEnabled(enabled); - player.sendMessage(ChatColor.GRAY + "Standby mode is now " - + (enabled ? "enabled" : "disabled")); + player.sendMessage(ChatColor.GRAY + "Standby mode is now " + (enabled ? "enabled" : "disabled")); } } diff --git a/src/de/typable/minecrafthub/constant/DefaultConstants.java b/src/de/typable/minecrafthub/constant/DefaultConstants.java @@ -9,7 +9,6 @@ public class DefaultConstants public static final class Messages { - public static final String NOT_ENOUGH_PERMISSION = ChatColor.RED - + "You don't have enough Permission to perform this command!"; + public static final String NOT_ENOUGH_PERMISSION = ChatColor.RED + "You don't have enough Permission to perform this command!"; } } diff --git a/src/de/typable/minecrafthub/event/ChairListener.java b/src/de/typable/minecrafthub/event/ChairListener.java @@ -19,18 +19,8 @@ import org.spigotmc.event.entity.EntityDismountEvent; public class ChairListener implements Listener { - private static final Material[] CHAIR_TYPE = new Material[] { - Material.OAK_STAIRS, - Material.SPRUCE_STAIRS, - Material.BIRCH_STAIRS, - Material.JUNGLE_STAIRS, - Material.ACACIA_STAIRS, - Material.DARK_OAK_STAIRS, - Material.ACACIA_STAIRS, - Material.CRIMSON_STAIRS, - Material.WARPED_STAIRS - }; - + private static final Material[] CHAIR_TYPE = new Material[] { Material.OAK_STAIRS, Material.SPRUCE_STAIRS, Material.BIRCH_STAIRS, Material.JUNGLE_STAIRS, Material.ACACIA_STAIRS, Material.DARK_OAK_STAIRS, Material.ACACIA_STAIRS, Material.CRIMSON_STAIRS, Material.WARPED_STAIRS }; + @SuppressWarnings("deprecation") @EventHandler public void onPlayerInteract(PlayerInteractEvent event) @@ -40,24 +30,25 @@ public class ChairListener implements Listener if(event.getClickedBlock() != null && !event.getPlayer().isSneaking()) { Block block = event.getClickedBlock(); - + if(isChair(block.getType())) { Block upperBlock = block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0)); - + if(upperBlock != null && upperBlock.getType() == Material.AIR) { Stairs stairs = (Stairs) block.getState().getBlockData(); - + if(isCompatible(stairs)) { Location location = block.getLocation().add(0.5, 0, 0.5); - Arrow arrow = (Arrow) block.getWorld().spawnArrow(location, new Vector(), 0F, 0F); + Arrow arrow = (Arrow) block.getWorld() + .spawnArrow(location, new Vector(), 0F, 0F); arrow.setGravity(false); arrow.setInvulnerable(true); arrow.setPickupStatus(PickupStatus.DISALLOWED); arrow.setPassenger((Entity) event.getPlayer()); - + event.setCancelled(true); } } @@ -65,21 +56,21 @@ public class ChairListener implements Listener } } } - + @EventHandler public void onEntityDismount(EntityDismountEvent event) { if(event.getEntity() instanceof Player) { Entity entity = event.getDismounted(); - + if(entity != null && entity instanceof Arrow) { entity.remove(); } } } - + private boolean isChair(Material material) { for(Material chair : CHAIR_TYPE) @@ -89,27 +80,28 @@ public class ChairListener implements Listener return true; } } - + return false; } - + private boolean isCompatible(Stairs stairs) { if(stairs.getHalf() != Half.BOTTOM) { return false; } - + if(stairs.isWaterlogged()) { return false; } - - if(stairs.getShape() == Stairs.Shape.INNER_RIGHT || stairs.getShape() == Stairs.Shape.INNER_LEFT) + + if(stairs.getShape() == Stairs.Shape.INNER_RIGHT || stairs + .getShape() == Stairs.Shape.INNER_LEFT) { return false; } - + return true; } } diff --git a/src/de/typable/minecrafthub/event/DoubleDoorListener.java b/src/de/typable/minecrafthub/event/DoubleDoorListener.java @@ -1,8 +1,5 @@ package de.typable.minecrafthub.event; -import java.util.ArrayList; -import java.util.List; - import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; @@ -14,87 +11,101 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + public class DoubleDoorListener implements Listener { - private static final Material[] DOOR_TYPE = new Material[] { - Material.OAK_DOOR, - Material.SPRUCE_DOOR, - Material.BIRCH_DOOR, - Material.JUNGLE_DOOR, - Material.ACACIA_DOOR, - Material.DARK_OAK_DOOR, - Material.ACACIA_DOOR, - Material.CRIMSON_DOOR, - Material.WARPED_DOOR - }; - + private static final Material[] DOOR_TYPE = new Material[] { Material.OAK_DOOR, Material.SPRUCE_DOOR, Material.BIRCH_DOOR, Material.JUNGLE_DOOR, Material.ACACIA_DOOR, Material.DARK_OAK_DOOR, Material.ACACIA_DOOR, Material.CRIMSON_DOOR, Material.WARPED_DOOR }; + @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); - + if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { if(event.getClickedBlock() != null) { Block block = event.getClickedBlock(); - + if(isDoor(block.getType())) { - List<Block> blockList = getNearbyDoors(block.getLocation()); - if(player.isSneaking()) { + if(isHoldingItem(player.getInventory().getItemInMainHand())) + { + return; + } + + if(isHoldingItem(player.getInventory().getItemInOffHand())) + { + return; + } + } + + BlockState state = block.getState(); + Door door = (Door) state.getBlockData(); + + Location location = getOppositeDoor(door, block.getLocation()); + + if(location == null) + { return; } - - if(blockList.isEmpty()) + + Block opposite = block.getWorld().getBlockAt(location); + + if(block.getType() != opposite.getType()) { return; } - - for(Block blockItem : blockList) + + BlockState stateOpposite = opposite.getState(); + Door doorOpposite = (Door) stateOpposite.getBlockData(); + + if(isCompatible(door, doorOpposite)) { - if(block.getType() != blockItem.getType()) + if(door.isOpen() && doorOpposite.isOpen()) { - continue; + doorOpposite.setOpen(false); + + stateOpposite.setBlockData(doorOpposite); + stateOpposite.update(); + + player.playSound(player.getLocation(), Sound.BLOCK_WOODEN_DOOR_CLOSE, 1F, 1F); } - - BlockState state = block.getState(); - BlockState stateItem = blockItem.getState(); - - Door door = (Door) state.getBlockData(); - Door doorItem = (Door) stateItem.getBlockData(); - - if(isCompatible(door, doorItem)) + + if(!door.isOpen() && !doorOpposite.isOpen()) { - if(door.isOpen() && doorItem.isOpen()) - { - doorItem.setOpen(false); - - stateItem.setBlockData(doorItem); - stateItem.update(); - - player.playSound(player.getLocation(), Sound.BLOCK_WOODEN_DOOR_CLOSE, 1F, 1F); - } - - if(!door.isOpen() && !doorItem.isOpen()) - { - doorItem.setOpen(true); - - stateItem.setBlockData(doorItem); - stateItem.update(); - - player.playSound(player.getLocation(), Sound.BLOCK_WOODEN_DOOR_OPEN, 1F, 1F); - } + doorOpposite.setOpen(true); + + stateOpposite.setBlockData(doorOpposite); + stateOpposite.update(); + + player.playSound(player.getLocation(), Sound.BLOCK_WOODEN_DOOR_OPEN, 1F, 1F); } } } } } } - + + private boolean isHoldingItem(ItemStack item) + { + if(item == null) + { + return false; + } + + if(item.getType() == Material.AIR) + { + return false; + } + + return true; + } + private boolean isDoor(Material material) { for(Material door : DOOR_TYPE) @@ -104,54 +115,52 @@ public class DoubleDoorListener implements Listener return true; } } - + return false; } - + + private Location getOppositeDoor(Door door, Location location) + { + int invert = door.getHinge() == Door.Hinge.LEFT ? 1 : -1; + + switch(door.getFacing()) + { + case NORTH: + location.add(1 * invert, 0, 0); + break; + case EAST: + location.add(0, 0, 1 * invert); + break; + case SOUTH: + location.add(-1 * invert, 0, 0); + break; + case WEST: + location.add(0, 0, -1 * invert); + break; + default: + return null; + } + + return location; + } + private boolean isCompatible(Door door, Door doorItem) { if(door.getFacing() != doorItem.getFacing()) { return false; } - + if(door.getHalf() != doorItem.getHalf()) { return false; } - + if(door.getHinge() == doorItem.getHinge()) { return false; } - + return true; } - - private List<Block> getNearbyDoors(Location location) - { - List<Block> blockList = new ArrayList<>(); - - int blockX = location.getBlockX(); - int blockY = location.getBlockY(); - int blockZ = location.getBlockZ(); - - for(int x = blockX - 1; x <= blockX + 1; x++) - { - for(int z = blockZ - 1; z <= blockZ + 1; z++) - { - if(blockX != x || blockZ != z) - { - Block block = location.getWorld().getBlockAt(x, blockY, z); - - if(block != null && isDoor(block.getType())) - { - blockList.add(block); - } - } - } - } - - return blockList; - } } diff --git a/src/de/typable/minecrafthub/event/EventListener.java b/src/de/typable/minecrafthub/event/EventListener.java @@ -9,6 +9,7 @@ import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerInteractEvent; + public class EventListener implements Listener { @EventHandler @@ -16,23 +17,25 @@ public class EventListener implements Listener { if(event.getAction() == Action.PHYSICAL) { - if(event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.FARMLAND) + if(event.getClickedBlock() != null && event.getClickedBlock() + .getType() == Material.FARMLAND) { event.setCancelled(true); } } } - + @EventHandler public void onEntityExplode(EntityExplodeEvent event) { event.blockList().clear(); } - + @EventHandler public void onChat(AsyncPlayerChatEvent event) { - String format = ChatColor.WHITE + event.getPlayer().getName() + ": " + ChatColor.GRAY + event.getMessage(); + String format = ChatColor.WHITE + event.getPlayer().getName() + ": " + ChatColor.GRAY + event + .getMessage(); event.setFormat(format); } } diff --git a/src/de/typable/minecrafthub/event/StandbyListener.java b/src/de/typable/minecrafthub/event/StandbyListener.java @@ -15,7 +15,7 @@ import de.typable.minecrafthub.constant.DefaultConstants; public class StandbyListener implements Listener, Runnable -{ +{ private Plugin plugin; private BukkitTask task; private boolean enabled; @@ -43,7 +43,7 @@ public class StandbyListener implements Listener, Runnable if(enabled) { Collection<? extends Player> online = Bukkit.getOnlinePlayers(); - + if(online.size() == 1 && online.contains(event.getPlayer())) { task = Bukkit.getScheduler().runTaskLater(plugin, this, delay * DefaultConstants.TICK);