Remove players from sit list after they log off.

Add chair break event check.
This commit is contained in:
cnaude 2012-11-24 16:19:48 -07:00
parent edf2434b30
commit aef19e5d2b
4 changed files with 123 additions and 37 deletions

View File

@ -10,6 +10,7 @@
# distance: The maximum distance between the chair (the center of the block) and the player to be able to sit down (to prevent glitching through walls, etc.). # distance: The maximum distance between the chair (the center of the block) and the player to be able to sit down (to prevent glitching through walls, etc.).
# sitting-height: Set how high you are sitting 'in' the stairs block (default is 0.7). # sitting-height: Set how high you are sitting 'in' the stairs block (default is 0.7).
# upsidedown-check: If true then prevent players from sitting on upside down stairs. # upsidedown-check: If true then prevent players from sitting on upside down stairs.
# seat-occupied-check: Check if seat is already occupied.
# ------ # ------
allowed-blocks: allowed-blocks:
- WOOD_STAIRS - WOOD_STAIRS
@ -29,4 +30,5 @@ distance: 2
sitting-height: 0.7 sitting-height: 0.7
permissions: true permissions: true
notify-player: true notify-player: true
upside-down-check: true upside-down-check: true
seat-occupied-check: true

View File

@ -4,6 +4,8 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.minecraft.server.Packet40EntityMetadata; import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -18,13 +20,16 @@ import org.bukkit.plugin.java.JavaPlugin;
public class Chairs extends JavaPlugin { public class Chairs extends JavaPlugin {
public List<Material> allowedBlocks = new ArrayList<Material>(); public List<Material> allowedBlocks = new ArrayList<Material>();
public boolean sneaking, autorotate, signcheck, permissions, notifyplayer, upsidedowncheck; public boolean sneaking, autoRotate, signCheck, permissions, notifyplayer, invertedStairCheck, seatOccupiedCheck;
public double sittingheight, distance; public double sittingHeight, distance;
public int maxchairwidth; public int maxChairWidth;
private File pluginFolder; private File pluginFolder;
private File configFile; private File configFile;
public byte metadata; public byte metadata;
public HashMap<String, Location> sit = new HashMap<String, Location>(); public HashMap<String, Location> sit = new HashMap<String, Location>();
public static final String PLUGIN_NAME = "Chairs";
public static final String LOG_HEADER = "[" + PLUGIN_NAME + "]";
static final Logger log = Logger.getLogger("Minecraft");
@Override @Override
public void onEnable() { public void onEnable() {
@ -47,7 +52,7 @@ public class Chairs extends JavaPlugin {
try { try {
pluginFolder.mkdir(); pluginFolder.mkdir();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logInfo("ERROR: " + e.getMessage());
} }
} }
@ -55,24 +60,30 @@ public class Chairs extends JavaPlugin {
try { try {
configFile.createNewFile(); configFile.createNewFile();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logInfo("ERROR: " + e.getMessage());
} }
} }
} }
private void loadConfig() { private void loadConfig() {
autorotate = getConfig().getBoolean("auto-rotate"); autoRotate = getConfig().getBoolean("auto-rotate");
sneaking = getConfig().getBoolean("sneaking"); sneaking = getConfig().getBoolean("sneaking");
signcheck = getConfig().getBoolean("sign-check"); signCheck = getConfig().getBoolean("sign-check");
sittingheight = getConfig().getDouble("sitting-height"); sittingHeight = getConfig().getDouble("sitting-height");
distance = getConfig().getDouble("distance"); distance = getConfig().getDouble("distance");
maxchairwidth = getConfig().getInt("max-chair-width"); maxChairWidth = getConfig().getInt("max-chair-width");
permissions = getConfig().getBoolean("permissions"); permissions = getConfig().getBoolean("permissions");
notifyplayer = getConfig().getBoolean("notify-player"); notifyplayer = getConfig().getBoolean("notify-player");
upsidedowncheck = getConfig().getBoolean("upside-down-check"); invertedStairCheck = getConfig().getBoolean("upside-down-check");
seatOccupiedCheck = getConfig().getBoolean("seat-occupied-check");
for (String type : getConfig().getStringList("allowed-blocks")) { for (String type : getConfig().getStringList("allowed-blocks")) {
allowedBlocks.add(Material.getMaterial(type)); try {
allowedBlocks.add(Material.getMaterial(type));
}
catch (Exception e) {
logInfo("ERROR: " + e.getMessage());
}
} }
} }
@ -103,6 +114,15 @@ public class Chairs extends JavaPlugin {
} }
} }
public void sendSit() {
for (String s : sit.keySet()) {
Player p = Bukkit.getPlayer(s);
if (p != null) {
sendSit(p);
}
}
}
// Send stand packet to all online players // Send stand packet to all online players
public void sendStand(Player p) { public void sendStand(Player p) {
if (sit.containsKey(p.getName())) { if (sit.containsKey(p.getName())) {
@ -116,4 +136,12 @@ public class Chairs extends JavaPlugin {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
} }
} }
public void logInfo(String _message) {
log.log(Level.INFO, String.format("%s %s", LOG_HEADER, _message));
}
public void logError(String _message) {
log.log(Level.SEVERE, String.format("%s %s", LOG_HEADER, _message));
}
} }

View File

@ -1,5 +1,8 @@
package net.spoothie.chairs; package net.spoothie.chairs;
import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -11,7 +14,10 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.material.Stairs; import org.bukkit.material.Stairs;
public class EventListener implements Listener { public class EventListener implements Listener {
@ -23,28 +29,65 @@ public class EventListener implements Listener {
} }
@EventHandler @EventHandler
public void onPlayerMove (PlayerMoveEvent event) { public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
String pname = player.getName(); String pname = player.getName();
if (plugin.sit.containsKey(player.getName())) { if (plugin.sit.containsKey(player.getName())) {
Location from = player.getLocation(); Location from = player.getLocation();
Location to = plugin.sit.get(pname); Location to = plugin.sit.get(pname);
if (from.getWorld() == to.getWorld()) { if (from.getWorld() == to.getWorld()) {
if (from.distance(to) > 1) { if (from.distance(to) > 1) {
plugin.sendStand(player); plugin.sendStand(player);
} else { } else {
plugin.sendSit(player); plugin.sendSit(player);
} }
} else { } else {
plugin.sendStand(player); plugin.sendStand(player);
} }
} }
} }
class sendSitTask extends TimerTask {
@Override
public void run() {
plugin.sendSit();
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Timer timer = new Timer();
long delay = 1 * 2000;
timer.schedule(new sendSitTask(), delay);
//plugin.sendSit();
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
String pName = event.getPlayer().getName();
if (plugin.sit.containsKey(pName)) {
plugin.sit.remove(pName);
}
}
@EventHandler
public void onBlockDestroy(BlockBreakEvent event) {
Block block = event.getBlock();
if (!plugin.sit.isEmpty()) {
for (String s : plugin.sit.keySet()) {
if (plugin.sit.get(s).equals(block.getLocation())) {
Player player = Bukkit.getPlayer(s);
plugin.sendStand(player);
}
}
}
}
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
Stairs stairs = null; Stairs stairs = null;
if (block.getState().getData() instanceof Stairs) { if (block.getState().getData() instanceof Stairs) {
stairs = (Stairs) block.getState().getData(); stairs = (Stairs) block.getState().getData();
@ -75,30 +118,30 @@ public class EventListener implements Listener {
} }
// Check if player is sitting. // Check if player is sitting.
if (plugin.sit.containsKey(event.getPlayer().getName())) { if (plugin.sit.containsKey(event.getPlayer().getName())) {
plugin.sit.remove(player.getName()); plugin.sit.remove(player.getName());
event.setCancelled(true); event.setCancelled(true);
if (plugin.notifyplayer) { if (plugin.notifyplayer) {
player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); player.sendMessage(ChatColor.GRAY + "You are no longer sitting.");
} }
plugin.sendStand(player); plugin.sendStand(player);
return; return;
} }
// Check for distance distance between player and chair. // Check for distance distance between player and chair.
if (plugin.distance > 0 && player.getLocation().distance(block.getLocation().add(0.5, 0, 0.5)) > plugin.distance) { if (plugin.distance > 0 && player.getLocation().distance(block.getLocation().add(0.5, 0, 0.5)) > plugin.distance) {
return; return;
} }
if (stairs != null) { if (stairs != null) {
if (stairs.isInverted() && plugin.upsidedowncheck) { if (stairs.isInverted() && plugin.invertedStairCheck) {
return; return;
} }
} }
// Check for signs. // Check for signs.
if (plugin.signcheck == true && stairs != null) { if (plugin.signCheck == true && stairs != null) {
boolean sign1 = false; boolean sign1 = false;
boolean sign2 = false; boolean sign2 = false;
@ -116,7 +159,7 @@ public class EventListener implements Listener {
} }
// Check for maximal chair width. // Check for maximal chair width.
if (plugin.maxchairwidth > 0 && stairs != null) { if (plugin.maxChairWidth > 0 && stairs != null) {
if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) { if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) {
chairwidth += getChairWidth(block, BlockFace.EAST); chairwidth += getChairWidth(block, BlockFace.EAST);
chairwidth += getChairWidth(block, BlockFace.WEST); chairwidth += getChairWidth(block, BlockFace.WEST);
@ -125,23 +168,32 @@ public class EventListener implements Listener {
chairwidth += getChairWidth(block, BlockFace.SOUTH); chairwidth += getChairWidth(block, BlockFace.SOUTH);
} }
if (chairwidth > plugin.maxchairwidth) { if (chairwidth > plugin.maxChairWidth) {
return; return;
} }
} }
// Sit-down process. // Sit-down process.
if (!plugin.sneaking || (plugin.sneaking && event.getPlayer().isSneaking())) { if (!plugin.sneaking || (plugin.sneaking && event.getPlayer().isSneaking())) {
if (plugin.seatOccupiedCheck) {
if (!plugin.sit.isEmpty()) {
for (String s : plugin.sit.keySet()) {
if (plugin.sit.get(s).equals(block.getLocation())) {
player.sendMessage(ChatColor.GRAY + "This seat is occupied by " + s + "!");
return;
}
}
}
}
if (player.getVehicle() != null) { if (player.getVehicle() != null) {
player.getVehicle().remove(); player.getVehicle().remove();
} }
plugin.sendSit(player);
// Rotate the player's view to the descending side of the block. // Rotate the player's view to the descending side of the block.
if (plugin.autorotate && stairs != null) { if (plugin.autoRotate && stairs != null) {
Location plocation = block.getLocation().clone(); Location plocation = block.getLocation().clone();
plocation.add(0.5D, (plugin.sittingheight - 0.5), 0.5D); plocation.add(0.5D, (plugin.sittingHeight - 0.5), 0.5D);
switch (stairs.getDescendingDirection()) { switch (stairs.getDescendingDirection()) {
case NORTH: case NORTH:
plocation.setYaw(90); plocation.setYaw(90);
@ -166,8 +218,12 @@ public class EventListener implements Listener {
player.sendMessage(ChatColor.GRAY + "You are now sitting."); player.sendMessage(ChatColor.GRAY + "You are now sitting.");
} }
plugin.sit.put(player.getName(), block.getLocation()); plugin.sit.put(player.getName(), block.getLocation());
event.setUseInteractedBlock(Result.DENY); event.setUseInteractedBlock(Result.DENY);
Timer timer = new Timer();
long delay = 1 * 2000;
timer.schedule(new sendSitTask(), delay);
//plugin.sendSit(player);
} }
} }
} }
@ -177,7 +233,7 @@ public class EventListener implements Listener {
int width = 0; int width = 0;
// Go through the blocks next to the clicked block and check if there are any further stairs. // Go through the blocks next to the clicked block and check if there are any further stairs.
for (int i = 1; i <= plugin.maxchairwidth; i++) { for (int i = 1; i <= plugin.maxChairWidth; i++) {
Block relative = block.getRelative(face, i); Block relative = block.getRelative(face, i);
if (plugin.allowedBlocks.contains(relative.getType()) && ((Stairs) relative.getState().getData()).getDescendingDirection() == ((Stairs) block.getState().getData()).getDescendingDirection()) { if (plugin.allowedBlocks.contains(relative.getType()) && ((Stairs) relative.getState().getData()).getDescendingDirection() == ((Stairs) block.getState().getData()).getDescendingDirection()) {

View File

@ -1,5 +1,5 @@
name: Chairs name: Chairs
version: 1.8 version: 1.10.1
description: Let players sit on stairs and slabs. description: Let players sit on stairs and slabs.
authors: authors:
- spoothie - spoothie