Skip to content

Commit

Permalink
Reworked to store Air blocks in room, instead of floor.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerstonge committed Apr 2, 2015
1 parent a157a11 commit a7b32f9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.tylerstonge</groupId>
<artifactId>MeetingRoom</artifactId>
<version>0.0.2-BETA</version>
<version>1.0.0-BETA</version>
<build>
<plugins>
<plugin>
Expand Down
115 changes: 78 additions & 37 deletions src/main/java/io/github/tylerstonge/meetingroom/MeetingRoom.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.tylerstonge.meetingroom;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -15,7 +16,6 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand All @@ -26,8 +26,8 @@ public class MeetingRoom extends JavaPlugin implements Listener {

HashMap<Integer, Room> roomBlocks = new HashMap<Integer, Room>();
String defaultRoomName = "Room";
Material meetingRoomMaterial = Material.OBSIDIAN;
Material meetingRoomCatalyst = Material.DIAMOND;
int maxRoomSize = 400;

ConfigAccessor data;

Expand All @@ -39,9 +39,10 @@ public void onEnable() {

data = new ConfigAccessor(this, "data.yml");
loadRoomBlocks();

meetingRoomMaterial = Material.getMaterial(getConfig().getString("material"));
scanAllRooms();

meetingRoomCatalyst = Material.getMaterial(getConfig().getString("catalyst"));
maxRoomSize = getConfig().getInt("maxsize");
refreshPlayers();
}

Expand All @@ -56,8 +57,8 @@ public void onPlayerMove(PlayerMoveEvent e) {
return;

Player player = e.getPlayer();
Block dest = e.getTo().getBlock().getRelative(BlockFace.DOWN);
Block org = e.getFrom().getBlock().getRelative(BlockFace.DOWN);
Block dest = e.getTo().getBlock();
Block org = e.getFrom().getBlock();

if(!roomBlocks.containsKey(org.getLocation().hashCode()) && roomBlocks.containsKey(dest.getLocation().hashCode())) {
// Entering meeting room.
Expand All @@ -82,28 +83,33 @@ public void onAsyncPlayerChat(AsyncPlayerChatEvent e) {
String id = MetadataManipulator.getMetadata(orig, "meetingroom", this);
for(Player dest : e.getRecipients()) {
if(MetadataManipulator.getMetadata(dest, "meetingroom", this).equals(id)) {
dest.sendMessage("["+orig.getName()+"->"+roomBlocks.get(orig.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation().hashCode()).getName()+"] "+e.getMessage());
dest.sendMessage("["+orig.getName()+"->"+roomBlocks.get(orig.getLocation().getBlock().getLocation().hashCode()).getName()+"] "+e.getMessage());
}
}
getLogger().info("MESSAGE SENT: "+e.getMessage()+ "RECIPIENTS: "+e.getRecipients()+"ROOM: "+MetadataManipulator.getMetadata(orig, "meetingroom", this));
}
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {

if((e.getAction() == Action.LEFT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_BLOCK) && e.hasItem() && e.hasBlock()) {
Block targeted = e.getClickedBlock();
if(e.getItem().getType() == meetingRoomCatalyst && targeted.getType() == meetingRoomMaterial && !roomBlocks.containsKey(targeted.getLocation().hashCode())) {
Player player = e.getPlayer();
player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount() - 1);

Random rand = new Random();
String id = Integer.toString(rand.nextInt(1000000));
final Room r = new Room(id, defaultRoomName, player.getDisplayName());
roomBlocks.put(targeted.getLocation().hashCode(), r);
addBlockToRoom(targeted, r);
refreshPlayers();
Block targeted = e.getClickedBlock().getRelative(e.getBlockFace().getOppositeFace());
if(e.getItem().getType() == meetingRoomCatalyst && e.getClickedBlock().getType() == Material.WOODEN_DOOR && targeted.getType() == Material.AIR) {
if(!roomBlocks.containsKey(targeted.getLocation().hashCode())) {
Player player = e.getPlayer();
player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount() - 1);

Random rand = new Random();
String id = Integer.toString(rand.nextInt(1000000));
final Room r = new Room(id, defaultRoomName, player.getDisplayName());

roomBlocks.put(targeted.getLocation().hashCode(), r);
r.setInitialBlock(targeted);
addBlockToRoom(targeted, r);
refreshPlayers();
} else {
scanRoom(roomBlocks.get(targeted.getLocation().hashCode()));
}
}
}
}
Expand All @@ -112,7 +118,7 @@ public void onPlayerInteract(PlayerInteractEvent e) {
public void onSignPlaced(SignChangeEvent e) {
Player player = e.getPlayer();
if(MetadataManipulator.getMetadata(player, "meetingroom", this) != null && MetadataManipulator.getMetadata(player, "meetingroom", this) != "") {
Room room = roomBlocks.get(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation().hashCode());
Room room = roomBlocks.get(player.getLocation().getBlock().getLocation().hashCode());
if(room.getOwner() == player.getDisplayName()) {
if(e.getLine(0).equals("[meetingroom]")) {
getLogger().info("Room set!");
Expand All @@ -122,52 +128,87 @@ public void onSignPlaced(SignChangeEvent e) {
}
}

@EventHandler
public void onBlockBreak(BlockBreakEvent e) {
Block target = e.getBlock();
if(roomBlocks.containsKey(target.getLocation().hashCode())) {
String toDelete = roomBlocks.get(target.getLocation().hashCode()).getId();
Iterator<Map.Entry<Integer, Room>> iter = roomBlocks.entrySet().iterator();
while(iter.hasNext()) {
Entry<Integer, Room> entry = iter.next();
if(toDelete.equals(entry.getValue().getId()))
iter.remove();
}
@SuppressWarnings("deprecation")
public void removeRoom(Room r, boolean displayError) {
Iterator<Map.Entry<Integer, Room>> iter = roomBlocks.entrySet().iterator();
String toDelete = r.getId();
while(iter.hasNext()) {
Entry<Integer, Room> entry = iter.next();
if(toDelete.equals(entry.getValue().getId()))
iter.remove();
}
if(displayError)
this.getServer().getPlayer(r.getOwner()).sendMessage("This room is not enclosed, or exceeds maximum size.");
refreshPlayers();
}

private void addBlockToRoom(Block t, final Room r) {
if(roomBlocks.size() > maxRoomSize) {
// Stop this madness
r.markForDeletion();
removeRoom(r, true);
return;
}

Block nextBlock = null;

nextBlock = t.getRelative(BlockFace.NORTH);
if(nextBlock.getType() == meetingRoomMaterial && !roomBlocks.containsKey(nextBlock.getLocation().hashCode())) {
if(nextBlock.getType() == Material.AIR && !roomBlocks.containsKey(nextBlock.getLocation().hashCode()) && !r.isMarkedForDeletion()) {
roomBlocks.put(nextBlock.getLocation().hashCode(), r);
addBlockToRoom(nextBlock, r);
}

nextBlock = t.getRelative(BlockFace.SOUTH);
if(nextBlock.getType() == meetingRoomMaterial && !roomBlocks.containsKey(nextBlock.getLocation().hashCode())) {
if(nextBlock.getType() == Material.AIR && !roomBlocks.containsKey(nextBlock.getLocation().hashCode()) && !r.isMarkedForDeletion()) {
roomBlocks.put(nextBlock.getLocation().hashCode(), r);
addBlockToRoom(nextBlock, r);
}

nextBlock = t.getRelative(BlockFace.EAST);
if(nextBlock.getType() == meetingRoomMaterial && !roomBlocks.containsKey(nextBlock.getLocation().hashCode())) {
if(nextBlock.getType() == Material.AIR && !roomBlocks.containsKey(nextBlock.getLocation().hashCode()) && !r.isMarkedForDeletion()) {
roomBlocks.put(nextBlock.getLocation().hashCode(), r);
addBlockToRoom(nextBlock, r);
}

nextBlock = t.getRelative(BlockFace.WEST);
if(nextBlock.getType() == meetingRoomMaterial && !roomBlocks.containsKey(nextBlock.getLocation().hashCode())) {
if(nextBlock.getType() == Material.AIR && !roomBlocks.containsKey(nextBlock.getLocation().hashCode()) && !r.isMarkedForDeletion()) {
roomBlocks.put(nextBlock.getLocation().hashCode(), r);
addBlockToRoom(nextBlock, r);
}

nextBlock = t.getRelative(BlockFace.DOWN);
if(nextBlock.getType() == Material.AIR && !roomBlocks.containsKey(nextBlock.getLocation().hashCode()) && !r.isMarkedForDeletion()) {
roomBlocks.put(nextBlock.getLocation().hashCode(), r);
addBlockToRoom(nextBlock, r);
}

nextBlock = t.getRelative(BlockFace.UP);
if(nextBlock.getType() == Material.AIR && !roomBlocks.containsKey(nextBlock.getLocation().hashCode()) && !r.isMarkedForDeletion()) {
roomBlocks.put(nextBlock.getLocation().hashCode(), r);
addBlockToRoom(nextBlock, r);
}
}

private void scanRoom(Room r) {
removeRoom(r, false);
roomBlocks.put(r.getInitialBlock().getLocation().hashCode(), r);
addBlockToRoom(r.getInitialBlock(), r);
refreshPlayers();
}


private void scanAllRooms() {
Set<Room> uniqueRooms = new HashSet<Room>();
for(Room r : roomBlocks.values()) {
if(!uniqueRooms.contains(r)) {
uniqueRooms.add(r);
scanRoom(r);
}
}
}

private void refreshPlayers() {
for(Player p : this.getServer().getOnlinePlayers()) {
Integer hash = p.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation().hashCode();
Integer hash = p.getLocation().getBlock().getLocation().hashCode();
if(roomBlocks.containsKey(hash)) {
MetadataManipulator.setMetadata(p, "meetingroom", hash, this);
} else {
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/io/github/tylerstonge/meetingroom/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import java.util.HashMap;
import java.util.Map;

import org.bukkit.block.Block;
import org.bukkit.configuration.serialization.ConfigurationSerializable;

public class Room implements ConfigurationSerializable {

private String id;
private String name;
private String owner;
private Block initialBlock;
private boolean delete;

public Room(String id, String name, String owner) {
this.id = id;
this.name = name;
this.owner = owner;
this.delete = false;
}

public Room(Map<String, Object> map) {
Expand All @@ -31,6 +35,14 @@ public String getId() {
return id;
}

public void markForDeletion() {
this.delete = true;
}

public boolean isMarkedForDeletion() {
return this.delete;
}

public String getName() {
return name;
}
Expand All @@ -46,7 +58,14 @@ public String getOwner() {
public void setOwner(String owner) {
this.owner = owner;
}

public Block getInitialBlock() {
return initialBlock;
}

public void setInitialBlock(Block initialBlock) {
this.initialBlock = initialBlock;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<String, Object>();
Expand All @@ -55,7 +74,4 @@ public Map<String, Object> serialize() {
map.put("owner", owner);
return map;
}



}
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
material: OBSIDIAN
catalyst: DIAMOND
catalyst: DIAMOND
maxsize: 400
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: MeetingRoom
main: io.github.tylerstonge.meetingroom.MeetingRoom
version: 0.0.2
version: 1.0.0

0 comments on commit a7b32f9

Please sign in to comment.