commit 26018d34e50a71baf09e0c6a8264f9b29d7e0a26
parent 0efc6b454790ff3c0bf24d5d8de7e9a2e93aaaf7
Author: typable <typable.dev@gmail.com>
Date: Thu, 26 Nov 2020 20:13:15 +0100
Added "Double Door" and more
Diffstat:
3 files changed, 183 insertions(+), 0 deletions(-)
diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java
@@ -12,6 +12,8 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import de.typable.minecrafthub.constant.DefaultConstants;
+import de.typable.minecrafthub.event.DoubleDoorListener;
+import de.typable.minecrafthub.event.EventListener;
import de.typable.minecrafthub.event.StandbyListener;
@@ -19,6 +21,8 @@ public class Main extends JavaPlugin
{
private PluginManager pluginManager;
private StandbyListener standbyListener;
+ private DoubleDoorListener doubleDoorListener;
+ private EventListener eventListener;
@Override
public void onEnable()
@@ -27,6 +31,12 @@ public class Main extends JavaPlugin
standbyListener = new StandbyListener(this);
pluginManager.registerEvents(standbyListener, this);
+
+ doubleDoorListener = new DoubleDoorListener();
+ pluginManager.registerEvents(doubleDoorListener, this);
+
+ eventListener = new EventListener();
+ pluginManager.registerEvents(eventListener, this);
}
@Override
diff --git a/src/de/typable/minecrafthub/event/DoubleDoorListener.java b/src/de/typable/minecrafthub/event/DoubleDoorListener.java
@@ -0,0 +1,144 @@
+package de.typable.minecrafthub.event;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.bukkit.Location;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.block.BlockState;
+import org.bukkit.block.data.type.Door;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.Action;
+import org.bukkit.event.player.PlayerInteractEvent;
+
+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
+ };
+
+ @EventHandler
+ public void onPlayerInteract(PlayerInteractEvent event)
+ {
+ 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(blockList.isEmpty())
+ {
+ return;
+ }
+
+ for(Block blockItem : blockList)
+ {
+ if(block.getType() != blockItem.getType())
+ {
+ continue;
+ }
+
+ 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() && doorItem.isOpen())
+ {
+ doorItem.setOpen(false);
+
+ stateItem.setBlockData(doorItem);
+ stateItem.update();
+ }
+
+ if(!door.isOpen() && !doorItem.isOpen())
+ {
+ doorItem.setOpen(true);
+
+ stateItem.setBlockData(doorItem);
+ stateItem.update();
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private boolean isDoor(Material material)
+ {
+ for(Material door : DOOR_TYPE)
+ {
+ if(material == door)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ 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
@@ -0,0 +1,29 @@
+package de.typable.minecrafthub.event;
+
+import org.bukkit.Material;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.Action;
+import org.bukkit.event.entity.EntityExplodeEvent;
+import org.bukkit.event.player.PlayerInteractEvent;
+
+public class EventListener implements Listener
+{
+ @EventHandler
+ public void onPlayerInteract(PlayerInteractEvent event)
+ {
+ if(event.getAction() == Action.PHYSICAL)
+ {
+ if(event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.FARMLAND)
+ {
+ event.setCancelled(true);
+ }
+ }
+ }
+
+ @EventHandler
+ public void onEntityExplode(EntityExplodeEvent event)
+ {
+ event.blockList().clear();
+ }
+}