diff --git a/src/main/java/de/craftinc/replicator/BlockPlaceListener.java b/src/main/java/de/craftinc/replicator/BlockPlaceListener.java index 70aa8d9..60d1442 100644 --- a/src/main/java/de/craftinc/replicator/BlockPlaceListener.java +++ b/src/main/java/de/craftinc/replicator/BlockPlaceListener.java @@ -14,10 +14,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ - package de.craftinc.replicator; import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; @@ -26,6 +27,7 @@ import java.util.ArrayList; @SuppressWarnings("UnusedDeclaration") public class BlockPlaceListener implements Listener { + @EventHandler(priority = EventPriority.MONITOR) public void onBlockPlaced( BlockPlaceEvent event ) { ArrayList replicators = Replicator.getReplicators(event.getBlockPlaced().getLocation()); diff --git a/src/main/java/de/craftinc/replicator/PlayerInteractEntityListener.java b/src/main/java/de/craftinc/replicator/PlayerInteractEntityListener.java new file mode 100644 index 0000000..3fe912a --- /dev/null +++ b/src/main/java/de/craftinc/replicator/PlayerInteractEntityListener.java @@ -0,0 +1,97 @@ +/* Craft Inc. Replicator + Copyright (C) 2013 Paul Schulze, Maximilian Häckel, Moritz Kaltofen + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +package de.craftinc.replicator; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.ItemFrame; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import java.util.ArrayList; + +@SuppressWarnings("UnusedDeclaration") +public class PlayerInteractEntityListener implements Listener +{ + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerInteractEntity( PlayerInteractEntityEvent event ) + { + // do nothing if entity type is not an item frame + if ( event.getRightClicked().getType() != EntityType.ITEM_FRAME ) + { + return; + } + + // get the actual entity + ItemFrame itemFrame = null; + Integer entityId = event.getRightClicked().getEntityId(); + for ( Entity e : event.getPlayer().getWorld().getEntitiesByClass(ItemFrame.class) ) + { + if ( e.getEntityId() == entityId ) + { + itemFrame = (ItemFrame) e; + break; + } + } + + // if no item frame was found: return! (strange and should NEVER happen as long as the world is not upside down!) + if ( itemFrame == null ) + { + return; + } + + // do nothing if item frame is empty + if (itemFrame.getItem().getType().equals(Material.AIR)) + { + return; + } + + // find replicator centers which are suitable for this item frame + ArrayList replicatorCenters = Replicator.getReplicators(event.getRightClicked().getLocation()); + + // do nothing if no replicator centers have been found + if ( replicatorCenters.isEmpty() ) + { + return; + } + + // get the replicators from database for these centers + for ( Location replicatorCenter : replicatorCenters ) + { + Replicator replicator = Replicator.getOrCreate(replicatorCenter, event.getPlayer().getName()); + + // replicator can be null if the player is not user and not owner + if ( replicator == null ) + { + continue; + } + + // from here on we have a valid replicator and a valid item + // that's why we cancel the event + event.setCancelled(true); + + // and spawn the items at the center location of the replicator + event.getPlayer().getWorld().dropItemNaturally( + replicatorCenter.getBlock().getRelative(Replicator.getDirection(replicatorCenter)).getLocation(), + itemFrame.getItem()); + } + } +} diff --git a/src/main/java/de/craftinc/replicator/Plugin.java b/src/main/java/de/craftinc/replicator/Plugin.java index d3ce17e..1ced1ba 100644 --- a/src/main/java/de/craftinc/replicator/Plugin.java +++ b/src/main/java/de/craftinc/replicator/Plugin.java @@ -45,6 +45,7 @@ public class Plugin extends JavaPlugin // create listeners BlockPlaceListener blockPlaceListener = new BlockPlaceListener(); + PlayerInteractEntityListener playerInteractEntityListener = new PlayerInteractEntityListener(); // commands Commands commandExecutor = new Commands(); @@ -53,5 +54,6 @@ public class Plugin extends JavaPlugin // register listeners PluginManager pm = this.getServer().getPluginManager(); pm.registerEvents(blockPlaceListener, this); + pm.registerEvents(playerInteractEntityListener, this); } } diff --git a/src/main/java/de/craftinc/replicator/Replicator.java b/src/main/java/de/craftinc/replicator/Replicator.java index 25e0ae5..29c272c 100644 --- a/src/main/java/de/craftinc/replicator/Replicator.java +++ b/src/main/java/de/craftinc/replicator/Replicator.java @@ -184,15 +184,28 @@ public class Replicator implements ConfigurationSerializable return replicators; } - private static Material[][][] getPattern( Location center ) + public static BlockFace getDirection( Location center ) { if ( center.getBlock().getRelative(BlockFace.NORTH).getType().equals(Material.AIR) ) - return Pattern.getNorth(); + return BlockFace.NORTH; if ( center.getBlock().getRelative(BlockFace.SOUTH).getType().equals(Material.AIR) ) - return Pattern.getSouth(); + return BlockFace.SOUTH; if ( center.getBlock().getRelative(BlockFace.WEST).getType().equals(Material.AIR) ) - return Pattern.getWest(); + return BlockFace.WEST; if ( center.getBlock().getRelative(BlockFace.EAST).getType().equals(Material.AIR) ) + return BlockFace.EAST; + return null; + } + + private static Material[][][] getPattern( Location center ) + { + if ( getDirection(center).equals(BlockFace.NORTH) ) + return Pattern.getNorth(); + if ( getDirection(center).equals(BlockFace.SOUTH) ) + return Pattern.getSouth(); + if ( getDirection(center).equals(BlockFace.WEST) ) + return Pattern.getWest(); + if ( getDirection(center).equals(BlockFace.EAST) ) return Pattern.getEast(); return null; }