Properly check for stair type.

Add upside down chair sit prevention
This commit is contained in:
cnaude 2012-11-23 01:39:10 -07:00
parent 53ec8b02db
commit edf2434b30
4 changed files with 65 additions and 89 deletions

View File

@ -3,13 +3,13 @@
# A list of all compatible block and item names: http://bit.ly/AmJgMb.
# ------
# allowed-blocks: Set the blocks you want to be able to sit down on. Currently, only stairs-blocks are working.
# item: Set the item you want to sit on (default is LEVER, because it is quite small so that you can't really see it).
# auto-rotate: If set to true, you are automatically rotated to the descending face of the stairs-block when sitting down.
# sneaking: If set to true, you have to sneak to sit down on a chair.
# max-chair-width: Define how many blocks a chair can be long (set to number <= 0 for unlimited width).
# sign-check: If set to true, you will only be able to sit down when there are signs on both of the ends of the chair.
# 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.
# ------
allowed-blocks:
- WOOD_STAIRS
@ -21,7 +21,6 @@ allowed-blocks:
- BRICK_STAIRS
- SMOOTH_STAIRS
- NETHER_BRICK_STAIRS
item: LEVER
auto-rotate: true
sneaking: true
max-chair-width: 3
@ -30,3 +29,4 @@ distance: 2
sitting-height: 0.7
permissions: true
notify-player: true
upside-down-check: true

View File

@ -4,18 +4,21 @@ import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Chairs extends JavaPlugin {
public List<Material> allowedBlocks = new ArrayList<Material>();
public Material item;
public boolean sneaking, autorotate, signcheck, permissions, notifyplayer;
public boolean sneaking, autorotate, signcheck, permissions, notifyplayer, upsidedowncheck;
public double sittingheight, distance;
public int maxchairwidth;
private File pluginFolder;
@ -58,7 +61,6 @@ public class Chairs extends JavaPlugin {
}
private void loadConfig() {
item = Material.getMaterial(getConfig().getString("item"));
autorotate = getConfig().getBoolean("auto-rotate");
sneaking = getConfig().getBoolean("sneaking");
signcheck = getConfig().getBoolean("sign-check");
@ -67,6 +69,7 @@ public class Chairs extends JavaPlugin {
maxchairwidth = getConfig().getInt("max-chair-width");
permissions = getConfig().getBoolean("permissions");
notifyplayer = getConfig().getBoolean("notify-player");
upsidedowncheck = getConfig().getBoolean("upside-down-check");
for (String type : getConfig().getStringList("allowed-blocks")) {
allowedBlocks.add(Material.getMaterial(type));
@ -91,4 +94,26 @@ public class Chairs extends JavaPlugin {
return true;
}
// Send sit packet to all online players
public void sendSit(Player p) {
Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
}
// Send stand packet to all online players
public void sendStand(Player p) {
if (sit.containsKey(p.getName())) {
if (notifyplayer) {
p.sendMessage(ChatColor.GRAY + "You are no longer sitting.");
}
sit.remove(p.getName());
}
Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
}
}

View File

@ -1,29 +1,18 @@
package net.spoothie.chairs;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
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.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Stairs;
import org.bukkit.util.Vector;
public class EventListener implements Listener {
@ -38,20 +27,16 @@ public class EventListener implements Listener {
Player player = event.getPlayer();
String pname = player.getName();
if (plugin.sit.containsKey(player.getName())) {
if (player.getLocation().distance(plugin.sit.get(pname)) > 1) {
if (plugin.notifyplayer) {
player.sendMessage(ChatColor.GRAY + "You are no longer sitting.");
}
plugin.sit.remove(player.getName());
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
Location from = player.getLocation();
Location to = plugin.sit.get(pname);
if (from.getWorld() == to.getWorld()) {
if (from.distance(to) > 1) {
plugin.sendStand(player);
} else {
plugin.sendSit(player);
}
} else {
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
plugin.sendStand(player);
}
}
}
@ -60,6 +45,10 @@ public class EventListener implements Listener {
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block block = event.getClickedBlock();
Stairs stairs = null;
if (block.getState().getData() instanceof Stairs) {
stairs = (Stairs) block.getState().getData();
}
if (plugin.allowedBlocks.contains(block.getType())) {
Player player = event.getPlayer();
int chairwidth = 1;
@ -93,10 +82,7 @@ public class EventListener implements Listener {
if (plugin.notifyplayer) {
player.sendMessage(ChatColor.GRAY + "You are no longer sitting.");
}
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 0), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
plugin.sendStand(player);
return;
}
@ -105,9 +91,14 @@ public class EventListener implements Listener {
return;
}
if (stairs != null) {
if (stairs.isInverted() && plugin.upsidedowncheck) {
return;
}
}
// Check for signs.
if (plugin.signcheck == true && block instanceof Stairs) {
Stairs stairs = (Stairs) block.getState().getData();
if (plugin.signcheck == true && stairs != null) {
boolean sign1 = false;
boolean sign2 = false;
@ -125,8 +116,7 @@ public class EventListener implements Listener {
}
// Check for maximal chair width.
if (plugin.maxchairwidth > 0 && block instanceof Stairs) {
Stairs stairs = (Stairs) block.getState().getData();
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);
@ -141,21 +131,17 @@ public class EventListener implements Listener {
}
// Sit-down process.
if (plugin.sneaking == false || (plugin.sneaking == true && event.getPlayer().isSneaking())) {
if (!plugin.sneaking || (plugin.sneaking && event.getPlayer().isSneaking())) {
if (player.getVehicle() != null) {
player.getVehicle().remove();
}
plugin.sendSit(player);
// Rotate the player's view to the descending side of the block.
Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false);
for (Player play : Bukkit.getOnlinePlayers()) {
((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
}
if (plugin.autorotate == true && block instanceof Stairs) {
Location plocation = block.getLocation();
Stairs stairs = (Stairs) block.getState().getData();
if (plugin.autorotate && stairs != null) {
Location plocation = block.getLocation().clone();
plocation.add(0.5D, (plugin.sittingheight - 0.5), 0.5D);
switch (stairs.getDescendingDirection()) {
case NORTH:
plocation.setYaw(90);
@ -187,41 +173,6 @@ public class EventListener implements Listener {
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (plugin.allowedBlocks.contains(event.getBlock().getType())) {
Item drop = dropSeat(event.getBlock());
for (Entity e : drop.getNearbyEntities(0.2, 0.2, 0.2)) {
if (e != null && e instanceof Item && e.getPassenger() != null) {
e.remove();
}
}
drop.remove();
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Entity vehicle = event.getPlayer().getVehicle();
// Let players stand up when leaving the server.
if (vehicle != null && vehicle instanceof Item) {
vehicle.remove();
}
}
private Item dropSeat(Block chair) {
Location location = chair.getLocation().add(0.5, (plugin.sittingheight - 0.5), 0.5);
Item drop = location.getWorld().dropItem(location, new ItemStack(plugin.item));
drop.setPickupDelay(Integer.MAX_VALUE);
drop.teleport(location);
drop.setVelocity(new Vector(0, 0, 0));
return drop;
}
private int getChairWidth(Block block, BlockFace face) {
int width = 0;
@ -243,7 +194,7 @@ public class EventListener implements Listener {
// Go through the blocks next to the clicked block and check if are signs on the end.
for (int i = 1; true; i++) {
Block relative = block.getRelative(face, i);
if (!plugin.allowedBlocks.contains(relative.getType()) || (block instanceof Stairs && ((Stairs) relative.getState().getData()).getDescendingDirection() != ((Stairs) block.getState().getData()).getDescendingDirection())) {
if (!plugin.allowedBlocks.contains(relative.getType()) || (block.getState().getData() instanceof Stairs && ((Stairs) relative.getState().getData()).getDescendingDirection() != ((Stairs) block.getState().getData()).getDescendingDirection())) {
if (relative.getType() == Material.SIGN || relative.getType() == Material.WALL_SIGN || relative.getType() == Material.SIGN_POST) {
return true;
} else {

View File

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