From e9cd29c8163b93820dc0ba5ba899ed8e022ffa37 Mon Sep 17 00:00:00 2001 From: cnaude Date: Tue, 20 Nov 2012 19:05:35 -0700 Subject: [PATCH] Switched from Item.passenger mode to PacketGenerator mode. --- src/config.yml | 3 +- src/net/spoothie/chairs/ChairWatcher.java | 30 ++++++ src/net/spoothie/chairs/Chairs.java | 7 +- src/net/spoothie/chairs/EventListener.java | 109 ++++++++++++--------- src/plugin.yml | 6 +- 5 files changed, 105 insertions(+), 50 deletions(-) create mode 100644 src/net/spoothie/chairs/ChairWatcher.java diff --git a/src/config.yml b/src/config.yml index b2ffda5..8e20167 100644 --- a/src/config.yml +++ b/src/config.yml @@ -28,4 +28,5 @@ max-chair-width: 3 sign-check: false distance: 2 sitting-height: 0.7 -permissions: true \ No newline at end of file +permissions: true +notify-player: true \ No newline at end of file diff --git a/src/net/spoothie/chairs/ChairWatcher.java b/src/net/spoothie/chairs/ChairWatcher.java new file mode 100644 index 0000000..7e9ae47 --- /dev/null +++ b/src/net/spoothie/chairs/ChairWatcher.java @@ -0,0 +1,30 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package net.spoothie.chairs; + +import java.util.ArrayList; +import net.minecraft.server.DataWatcher; +import net.minecraft.server.WatchableObject; + +/** + * + * @author cnaude + */ +public class ChairWatcher extends DataWatcher { + + private byte metadata; + + public ChairWatcher(byte i) { + this.metadata = i; + } + + @Override + public ArrayList b() { + ArrayList list = new ArrayList(); + WatchableObject wo = new WatchableObject(0, 0, Byte.valueOf(this.metadata)); + list.add(wo); + return list; + } +} diff --git a/src/net/spoothie/chairs/Chairs.java b/src/net/spoothie/chairs/Chairs.java index 84dcc9a..3b65570 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -2,7 +2,9 @@ package net.spoothie.chairs; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -13,11 +15,13 @@ public class Chairs extends JavaPlugin { public List allowedBlocks = new ArrayList(); public Material item; - public boolean sneaking, autorotate, signcheck, permissions; + public boolean sneaking, autorotate, signcheck, permissions, notifyplayer; public double sittingheight, distance; public int maxchairwidth; private File pluginFolder; private File configFile; + public byte metadata; + public HashMap sit = new HashMap(); @Override public void onEnable() { @@ -62,6 +66,7 @@ public class Chairs extends JavaPlugin { distance = getConfig().getDouble("distance"); maxchairwidth = getConfig().getInt("max-chair-width"); permissions = getConfig().getBoolean("permissions"); + notifyplayer = getConfig().getBoolean("notify-player"); for (String type : getConfig().getStringList("allowed-blocks")) { allowedBlocks.add(Material.getMaterial(type)); diff --git a/src/net/spoothie/chairs/EventListener.java b/src/net/spoothie/chairs/EventListener.java index 09e0d7f..866a41d 100644 --- a/src/net/spoothie/chairs/EventListener.java +++ b/src/net/spoothie/chairs/EventListener.java @@ -2,10 +2,14 @@ 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; @@ -15,6 +19,7 @@ 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; @@ -28,16 +33,37 @@ public class EventListener implements Listener { this.plugin = plugin; } + @EventHandler + public void onPlayerMove (PlayerMoveEvent event) { + 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); + } + } else { + Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false); + for (Player play : Bukkit.getOnlinePlayers()) { + ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet); + } + } + } + } + @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { - Block block = event.getClickedBlock(); - //event.getPlayer().sendMessage("Block: " + block.getType()); + Block block = event.getClickedBlock(); if (plugin.allowedBlocks.contains(block.getType())) { Player player = event.getPlayer(); - Stairs stairs = (Stairs) block.getState().getData(); int chairwidth = 1; - + // Check if block beneath chair is solid. if (block.getRelative(BlockFace.DOWN).getType() == Material.AIR) { return; @@ -48,7 +74,7 @@ public class EventListener implements Listener { if (block.getRelative(BlockFace.DOWN).getType() == Material.LAVA) { return; } - if (!net.minecraft.server.Block.byId[block.getTypeId()].material.isSolid()) { + if (!net.minecraft.server.Block.byId[block.getTypeId()].material.isSolid()) { return; } @@ -60,9 +86,14 @@ public class EventListener implements Listener { } // Check if player is sitting. - if (!player.isSneaking() && player.getVehicle() != null) { - player.getVehicle().remove(); - return; + + if (plugin.sit.containsKey(event.getPlayer().getName())) { + plugin.sit.remove(player.getName()); + event.setCancelled(true); + if (plugin.notifyplayer) { + player.sendMessage(ChatColor.GRAY + "You are no longer sitting."); + } + return; } // Check for distance distance between player and chair. @@ -71,7 +102,8 @@ public class EventListener implements Listener { } // Check for signs. - if (plugin.signcheck == true) { + if (plugin.signcheck == true && block instanceof Stairs) { + Stairs stairs = (Stairs) block.getState().getData(); boolean sign1 = false; boolean sign2 = false; @@ -89,7 +121,8 @@ public class EventListener implements Listener { } // Check for maximal chair width. - if (plugin.maxchairwidth > 0) { + if (plugin.maxchairwidth > 0 && block instanceof Stairs) { + Stairs stairs = (Stairs) block.getState().getData(); if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) { chairwidth += getChairWidth(block, BlockFace.EAST); chairwidth += getChairWidth(block, BlockFace.WEST); @@ -109,18 +142,16 @@ public class EventListener implements Listener { player.getVehicle().remove(); } - Item drop = dropSeat(block); - List drops = checkChair(drop); - - if (drops != null) { - drop.remove(); - return; - } - // Rotate the player's view to the descending side of the block. - if (plugin.autorotate == true) { - Location plocation = player.getLocation(); - + + 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(); switch (stairs.getDescendingDirection()) { case NORTH: plocation.setYaw(90); @@ -136,14 +167,16 @@ public class EventListener implements Listener { } player.teleport(plocation); + + } else { + player.teleport(block.getLocation().add(0.5D, 0.0D, 0.5D)); } - - // Changing the drop material is only necessary for the item merge feature of CB++ - // The client won't update the material, though. - drop.setItemStack(new ItemStack(Material.PUMPKIN_STEM)); - drop.setPassenger(player); - - // Cancel BlockPlaceEvent Result, if player is rightclicking with a block in his hand. + player.setSneaking(true); + if (plugin.notifyplayer) { + player.sendMessage(ChatColor.GRAY + "You are now sitting."); + } + plugin.sit.put(player.getName(), block.getLocation()); + event.setUseInteractedBlock(Result.DENY); } } @@ -177,30 +210,14 @@ public class EventListener implements Listener { private Item dropSeat(Block chair) { Location location = chair.getLocation().add(0.5, (plugin.sittingheight - 0.5), 0.5); - Item drop = location.getWorld().dropItemNaturally(location, new ItemStack(plugin.item)); + + 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 List checkChair(Item drop) { - List drops = new ArrayList(); - - // Check for already existing chair items. - for (Entity e : drop.getNearbyEntities(0.2, 0.2, 0.2)) { - if (e != null && e instanceof Item && e.getPassenger() != null) { - drops.add(drop); - } - } - - if (drops.isEmpty() == false) { - return drops; - } - - return null; - } - private int getChairWidth(Block block, BlockFace face) { int width = 0; diff --git a/src/plugin.yml b/src/plugin.yml index 078438c..44c0ab9 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,7 +1,9 @@ name: Chairs -version: 1.5b +version: 1.7 description: Let players sit on stairs and slabs. -authors: spoothie, cnaude +authors: + - spoothie + - cnaude main: net.spoothie.chairs.Chairs commands: chairs: