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.).
# 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.
# seat-occupied-check: Check if seat is already occupied.
# ------
allowed-blocks:
- WOOD_STAIRS
@ -30,3 +31,4 @@ sitting-height: 0.7
permissions: true
notify-player: 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.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -18,13 +20,16 @@ import org.bukkit.plugin.java.JavaPlugin;
public class Chairs extends JavaPlugin {
public List<Material> allowedBlocks = new ArrayList<Material>();
public boolean sneaking, autorotate, signcheck, permissions, notifyplayer, upsidedowncheck;
public double sittingheight, distance;
public int maxchairwidth;
public boolean sneaking, autoRotate, signCheck, permissions, notifyplayer, invertedStairCheck, seatOccupiedCheck;
public double sittingHeight, distance;
public int maxChairWidth;
private File pluginFolder;
private File configFile;
public byte metadata;
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
public void onEnable() {
@ -47,7 +52,7 @@ public class Chairs extends JavaPlugin {
try {
pluginFolder.mkdir();
} catch (Exception e) {
e.printStackTrace();
logInfo("ERROR: " + e.getMessage());
}
}
@ -55,25 +60,31 @@ public class Chairs extends JavaPlugin {
try {
configFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
logInfo("ERROR: " + e.getMessage());
}
}
}
private void loadConfig() {
autorotate = getConfig().getBoolean("auto-rotate");
autoRotate = getConfig().getBoolean("auto-rotate");
sneaking = getConfig().getBoolean("sneaking");
signcheck = getConfig().getBoolean("sign-check");
sittingheight = getConfig().getDouble("sitting-height");
signCheck = getConfig().getBoolean("sign-check");
sittingHeight = getConfig().getDouble("sitting-height");
distance = getConfig().getDouble("distance");
maxchairwidth = getConfig().getInt("max-chair-width");
maxChairWidth = getConfig().getInt("max-chair-width");
permissions = getConfig().getBoolean("permissions");
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")) {
try {
allowedBlocks.add(Material.getMaterial(type));
}
catch (Exception e) {
logInfo("ERROR: " + e.getMessage());
}
}
}
@Override
@ -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
public void sendStand(Player p) {
if (sit.containsKey(p.getName())) {
@ -116,4 +136,12 @@ public class Chairs extends JavaPlugin {
((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;
import java.util.Timer;
import java.util.TimerTask;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
@ -11,7 +14,10 @@ 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.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.material.Stairs;
public class EventListener implements Listener {
@ -23,7 +29,7 @@ public class EventListener implements Listener {
}
@EventHandler
public void onPlayerMove (PlayerMoveEvent event) {
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
String pname = player.getName();
if (plugin.sit.containsKey(player.getName())) {
@ -41,6 +47,43 @@ public class EventListener implements Listener {
}
}
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
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
@ -92,13 +135,13 @@ public class EventListener implements Listener {
}
if (stairs != null) {
if (stairs.isInverted() && plugin.upsidedowncheck) {
if (stairs.isInverted() && plugin.invertedStairCheck) {
return;
}
}
// Check for signs.
if (plugin.signcheck == true && stairs != null) {
if (plugin.signCheck == true && stairs != null) {
boolean sign1 = false;
boolean sign2 = false;
@ -116,7 +159,7 @@ public class EventListener implements Listener {
}
// 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) {
chairwidth += getChairWidth(block, BlockFace.EAST);
chairwidth += getChairWidth(block, BlockFace.WEST);
@ -125,23 +168,32 @@ public class EventListener implements Listener {
chairwidth += getChairWidth(block, BlockFace.SOUTH);
}
if (chairwidth > plugin.maxchairwidth) {
if (chairwidth > plugin.maxChairWidth) {
return;
}
}
// Sit-down process.
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) {
player.getVehicle().remove();
}
plugin.sendSit(player);
// 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();
plocation.add(0.5D, (plugin.sittingheight - 0.5), 0.5D);
plocation.add(0.5D, (plugin.sittingHeight - 0.5), 0.5D);
switch (stairs.getDescendingDirection()) {
case NORTH:
plocation.setYaw(90);
@ -166,8 +218,12 @@ public class EventListener implements Listener {
player.sendMessage(ChatColor.GRAY + "You are now sitting.");
}
plugin.sit.put(player.getName(), block.getLocation());
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;
// 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);
if (plugin.allowedBlocks.contains(relative.getType()) && ((Stairs) relative.getState().getData()).getDescendingDirection() == ((Stairs) block.getState().getData()).getDescendingDirection()) {

View File

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