Switch to arrow method

This commit is contained in:
cnaude 2013-07-14 16:55:03 -07:00
parent 2cb04a92f8
commit 1dabe40aad
3 changed files with 54 additions and 191 deletions

View File

@ -2,19 +2,16 @@ package com.cnaude.chairs;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
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.v1_6_R2.Packet40EntityMetadata;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.permissions.Permission;
@ -38,7 +35,7 @@ public class Chairs extends JavaPlugin {
private File pluginFolder;
private File configFile;
public byte sitByte;
public HashMap<String, Location> sit = new HashMap<String, Location>();
public HashMap<String, Entity> sit = new HashMap<String, Entity>();
public static final String PLUGIN_NAME = "Chairs";
public static final String LOG_HEADER = "[" + PLUGIN_NAME + "]";
static final Logger log = Logger.getLogger("Minecraft");
@ -222,86 +219,6 @@ public class Chairs extends JavaPlugin {
pm.addPermission(new Permission("chairs.self","Allow player to self disable or enable sitting.",pd));
}
private PacketContainer getSitPacket(Player p) {
PacketContainer fakeSit = protocolManager.createPacket(40);
fakeSit.getSpecificModifier(int.class).write(0, p.getEntityId());
WrappedDataWatcher watcher = new WrappedDataWatcher();
watcher.setObject(0, sitByte);
fakeSit.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
return fakeSit;
}
private PacketContainer getStandPacket(Player p) {
PacketContainer fakeSit = protocolManager.createPacket(40);
fakeSit.getSpecificModifier(int.class).write(0, p.getEntityId());
WrappedDataWatcher watcher = new WrappedDataWatcher();
watcher.setObject(0, (byte)0);
fakeSit.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
return fakeSit;
}
// Send sit packet to all online players that are on same world and can see player
public void sendSit(Player p) {
if (protocolManager != null) {
sendPacketToPlayers(getSitPacket(p),p);
} else {
sendPacketToPlayers(new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher(sitByte), true),p);
}
}
private void sendPacketToPlayers(PacketContainer pc, Player p) {
for (Player onlinePlayer : getServer().getOnlinePlayers()) {
if (onlinePlayer.canSee(p)) {
if (onlinePlayer.getWorld().equals(p.getWorld())) {
try {
protocolManager.sendServerPacket(onlinePlayer, pc);
} catch (Exception ex) {
// Nothing here
}
}
}
}
}
private void sendPacketToPlayers(Packet40EntityMetadata packet, Player p) {
for (Player onlinePlayer : getServer().getOnlinePlayers()) {
if (onlinePlayer.canSee(p)) {
if (onlinePlayer.getWorld().equals(p.getWorld())) {
try {
((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
} catch (Exception ex) {
// Nothing here
}
}
}
}
}
public void sendSit() {
for (String s : sit.keySet()) {
Player p = getServer().getPlayer(s);
if (p != null) {
sendSit(p);
}
}
}
// Send stand packet to all online players
public void sendStand(Player p) {
if (sit.containsKey(p.getName())) {
if (notifyplayer && !msgStanding.isEmpty()) {
p.sendMessage(msgStanding);
}
sit.remove(p.getName());
}
if (protocolManager != null) {
sendPacketToPlayers(getStandPacket(p),p);
} else {
Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte)0), false);
sendPacketToPlayers(packet,p);
}
}
public void logInfo(String _message) {
log.log(Level.INFO, String.format("%s %s", LOG_HEADER, _message));
}
@ -313,5 +230,6 @@ public class Chairs extends JavaPlugin {
public static Chairs get() {
return instance;
}
}

View File

@ -1,7 +1,5 @@
package com.cnaude.chairs;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -9,21 +7,17 @@ import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
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.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.event.player.PlayerTeleportEvent;
import org.bukkit.material.Directional;
import org.bukkit.material.Stairs;
import org.bukkit.material.Step;
import org.bukkit.material.WoodenStep;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.util.Vector;
public class EventListener implements Listener {
@ -35,74 +29,6 @@ public class EventListener implements Listener {
this.ignoreList = ignoreList;
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
String pname = player.getName();
if (plugin.sit.containsKey(player.getName())) {
Location from = player.getLocation();
Location to = plugin.sit.get(pname);
if (from.getWorld() == to.getWorld()) {
if (from.distance(to) > 1.5) {
plugin.sendStand(player);
} else {
plugin.sendSit(player);
}
} else {
plugin.sendStand(player);
}
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
delayedSitTask();
}
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
delayedSitTask();
}
private void delayedSitTask() {
plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
plugin.sendSit();
}
}, 20);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
if (plugin.sit.containsKey(player.getName())) {
plugin.sendStand(player);
Location loc = player.getLocation().clone();
loc.setY(loc.getY() + 1);
player.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
}
}
@EventHandler
public void onBlockDestroy(BlockBreakEvent event) {
Block block = event.getBlock();
if (!plugin.sit.isEmpty()) {
ArrayList<String> standList = new ArrayList<String>();
for (String s : plugin.sit.keySet()) {
if (plugin.sit.get(s).equals(block.getLocation())) {
standList.add(s);
}
}
for (String s : standList) {
Player player = Bukkit.getPlayer(s);
plugin.sendStand(player);
}
standList.clear();
}
}
public boolean isValidChair(Block block) {
for (ChairBlock cb : plugin.allowedBlocks) {
if (cb.getMat().equals(block.getType())) {
@ -114,6 +40,7 @@ public class EventListener implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (event.getPlayer().getItemInHand().getType().isBlock()
&& (event.getPlayer().getItemInHand().getTypeId() != 0)
&& plugin.ignoreIfBlockInHand) {
@ -128,7 +55,6 @@ public class EventListener implements Listener {
double sh = plugin.sittingHeight;
boolean blockOkay = false;
Player player = event.getPlayer();
if (ignoreList.isIgnored(player.getName())) {
return;
}
@ -211,13 +137,19 @@ public class EventListener implements Listener {
}
// Check if player is sitting.
if (plugin.sit.containsKey(event.getPlayer().getName())) {
if (plugin.sit.containsKey(player.getName())) {
if (plugin.sit.containsKey(player.getName())) {
// Eject from arrow
plugin.sit.get(player.getName()).eject();
// Remove arrow
plugin.sit.get(player.getName()).remove();
}
plugin.sit.remove(player.getName());
event.setCancelled(true);
if (plugin.notifyplayer && !plugin.msgStanding.isEmpty()) {
player.sendMessage(plugin.msgStanding);
}
plugin.sendStand(player);
return;
}
@ -290,46 +222,59 @@ public class EventListener implements Listener {
}
}
if (player.getVehicle() != null) {
player.getVehicle().remove();
}
Location chairLoc = event.getClickedBlock().getLocation().add(0.5, 0, 0.5);
// Rotate the player's view to the descending side of the block.
if (plugin.autoRotate && stairs != null) {
Location plocation = block.getLocation().clone();
plocation.add(0.5D, (sh - 0.5D), 0.5D);
switch (stairs.getDescendingDirection()) {
case NORTH:
plocation.setYaw(180);
break;
case EAST:
plocation.setYaw(-90);
break;
case SOUTH:
plocation.setYaw(0);
break;
case WEST:
plocation.setYaw(90);
BlockFace direction = ((Directional) event.getClickedBlock().getState().getData()).getFacing();
double dx = direction.getModX();
double dy = direction.getModY();
double dz = direction.getModZ();
if (dx != 0) {
if (dx < 0) {
chairLoc.setYaw((float) (1.5 * Math.PI));
} else {
chairLoc.setYaw((float) (0.5 * Math.PI));
}
chairLoc.setYaw((float) (chairLoc.getYaw() - Math.atan(dz / dx)));
} else if (dz < 0) {
chairLoc.setYaw((float) Math.PI);
}
player.teleport(plocation);
double dxz = Math.sqrt(Math.pow(dx, 2) + Math.pow(dz, 2));
chairLoc.setPitch((float) -Math.atan(dy / dxz));
chairLoc.setYaw(-chairLoc.getYaw() * 180f / (float) Math.PI);
chairLoc.setPitch(chairLoc.getPitch() * 180f / (float) Math.PI);
} else {
Location plocation = block.getLocation().clone();
plocation.setYaw(player.getLocation().getYaw());
player.teleport(plocation.add(0.5D, (sh - 0.5D), 0.5D));
chairLoc.setPitch(player.getPlayer().getLocation().getPitch());
chairLoc.setYaw(player.getPlayer().getLocation().getYaw());
}
player.setSneaking(true);
//player.setSneaking(true);
if (plugin.notifyplayer && !plugin.msgSitting.isEmpty()) {
player.sendMessage(plugin.msgSitting);
}
plugin.sit.put(player.getName(), block.getLocation());
event.setUseInteractedBlock(Result.DENY);
delayedSitTask();
player.getPlayer().teleport(chairLoc);
Entity ar = block.getWorld().spawnArrow(getBlockCentre(block).subtract(0, 0.5, 0), new Vector(0, 0, 0), 0, 0);
player.setSneaking(false);
ar.setPassenger(player);
ar.setTicksLived(1);
plugin.sit.put(player.getName(), ar);
event.setCancelled(true);
}
}
}
}
// https://github.com/sk89q/craftbook/blob/master/src/main/java/com/sk89q/craftbook/util/BlockUtil.java
public static Location getBlockCentre(Block block) {
return block.getLocation().add(0.5, 0.5, 0.5);
}
private int getChairWidth(Block block, BlockFace face) {
int width = 0;

View File

@ -1,5 +1,5 @@
name: Chairs
version: 2.1.6-SNAPSHOT1
version: 3.0.0
description: Let players sit on blocks.
website: http://dev.bukkit.org/bukkit-plugins/chairsreloaded/
authors: