Switched from Item.passenger mode to PacketGenerator mode.

This commit is contained in:
cnaude 2012-11-20 19:05:35 -07:00
parent 0a8f6b94bb
commit e9cd29c816
5 changed files with 105 additions and 50 deletions

View File

@ -29,3 +29,4 @@ sign-check: false
distance: 2 distance: 2
sitting-height: 0.7 sitting-height: 0.7
permissions: true permissions: true
notify-player: true

View File

@ -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<WatchableObject> b() {
ArrayList<WatchableObject> list = new ArrayList<WatchableObject>();
WatchableObject wo = new WatchableObject(0, 0, Byte.valueOf(this.metadata));
list.add(wo);
return list;
}
}

View File

@ -2,7 +2,9 @@ package net.spoothie.chairs;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -13,11 +15,13 @@ public class Chairs extends JavaPlugin {
public List<Material> allowedBlocks = new ArrayList<Material>(); public List<Material> allowedBlocks = new ArrayList<Material>();
public Material item; public Material item;
public boolean sneaking, autorotate, signcheck, permissions; public boolean sneaking, autorotate, signcheck, permissions, notifyplayer;
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 HashMap<String, Location> sit = new HashMap<String, Location>();
@Override @Override
public void onEnable() { public void onEnable() {
@ -62,6 +66,7 @@ public class Chairs extends JavaPlugin {
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");
for (String type : getConfig().getStringList("allowed-blocks")) { for (String type : getConfig().getStringList("allowed-blocks")) {
allowedBlocks.add(Material.getMaterial(type)); allowedBlocks.add(Material.getMaterial(type));

View File

@ -2,10 +2,14 @@ package net.spoothie.chairs;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.server.Packet40EntityMetadata;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player; 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.Action;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Stairs; import org.bukkit.material.Stairs;
@ -28,14 +33,35 @@ public class EventListener implements Listener {
this.plugin = plugin; 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 @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();
//event.getPlayer().sendMessage("Block: " + block.getType());
if (plugin.allowedBlocks.contains(block.getType())) { if (plugin.allowedBlocks.contains(block.getType())) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Stairs stairs = (Stairs) block.getState().getData();
int chairwidth = 1; int chairwidth = 1;
// Check if block beneath chair is solid. // Check if block beneath chair is solid.
@ -60,8 +86,13 @@ public class EventListener implements Listener {
} }
// Check if player is sitting. // Check if player is sitting.
if (!player.isSneaking() && player.getVehicle() != null) {
player.getVehicle().remove(); 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; return;
} }
@ -71,7 +102,8 @@ public class EventListener implements Listener {
} }
// Check for signs. // Check for signs.
if (plugin.signcheck == true) { if (plugin.signcheck == true && block instanceof Stairs) {
Stairs stairs = (Stairs) block.getState().getData();
boolean sign1 = false; boolean sign1 = false;
boolean sign2 = false; boolean sign2 = false;
@ -89,7 +121,8 @@ public class EventListener implements Listener {
} }
// Check for maximal chair width. // 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) { 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);
@ -109,18 +142,16 @@ public class EventListener implements Listener {
player.getVehicle().remove(); player.getVehicle().remove();
} }
Item drop = dropSeat(block); // Rotate the player's view to the descending side of the block.
List<Item> drops = checkChair(drop);
if (drops != null) { Packet40EntityMetadata packet = new Packet40EntityMetadata(player.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false);
drop.remove(); for (Player play : Bukkit.getOnlinePlayers()) {
return; ((CraftPlayer) play).getHandle().netServerHandler.sendPacket(packet);
} }
// Rotate the player's view to the descending side of the block. if (plugin.autorotate == true && block instanceof Stairs) {
if (plugin.autorotate == true) { Location plocation = block.getLocation();
Location plocation = player.getLocation(); Stairs stairs = (Stairs) block.getState().getData();
switch (stairs.getDescendingDirection()) { switch (stairs.getDescendingDirection()) {
case NORTH: case NORTH:
plocation.setYaw(90); plocation.setYaw(90);
@ -136,14 +167,16 @@ public class EventListener implements Listener {
} }
player.teleport(plocation); player.teleport(plocation);
} else {
player.teleport(block.getLocation().add(0.5D, 0.0D, 0.5D));
} }
player.setSneaking(true);
if (plugin.notifyplayer) {
player.sendMessage(ChatColor.GRAY + "You are now sitting.");
}
plugin.sit.put(player.getName(), block.getLocation());
// 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.
event.setUseInteractedBlock(Result.DENY); event.setUseInteractedBlock(Result.DENY);
} }
} }
@ -177,30 +210,14 @@ public class EventListener implements Listener {
private Item dropSeat(Block chair) { private Item dropSeat(Block chair) {
Location location = chair.getLocation().add(0.5, (plugin.sittingheight - 0.5), 0.5); 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.setPickupDelay(Integer.MAX_VALUE);
drop.teleport(location); drop.teleport(location);
drop.setVelocity(new Vector(0, 0, 0)); drop.setVelocity(new Vector(0, 0, 0));
return drop; return drop;
} }
private List<Item> checkChair(Item drop) {
List<Item> drops = new ArrayList<Item>();
// 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) { private int getChairWidth(Block block, BlockFace face) {
int width = 0; int width = 0;

View File

@ -1,7 +1,9 @@
name: Chairs name: Chairs
version: 1.5b version: 1.7
description: Let players sit on stairs and slabs. description: Let players sit on stairs and slabs.
authors: spoothie, cnaude authors:
- spoothie
- cnaude
main: net.spoothie.chairs.Chairs main: net.spoothie.chairs.Chairs
commands: commands:
chairs: chairs: