Code refactoring.
Add website to plugin.yml
This commit is contained in:
29
src/com/cnaude/chairs/ChairBlock.java
Normal file
29
src/com/cnaude/chairs/ChairBlock.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.cnaude.chairs;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author cnaude
|
||||
*/
|
||||
public class ChairBlock {
|
||||
private Material mat;
|
||||
private double sitHeight;
|
||||
|
||||
public ChairBlock(Material m, double d) {
|
||||
mat = m;
|
||||
sitHeight = d;
|
||||
}
|
||||
|
||||
public Material getMat() {
|
||||
return mat;
|
||||
}
|
||||
|
||||
public double getSitHeight() {
|
||||
return sitHeight;
|
||||
}
|
||||
}
|
57
src/com/cnaude/chairs/ChairEffects.java
Normal file
57
src/com/cnaude/chairs/ChairEffects.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.cnaude.chairs;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author cnaude
|
||||
*/
|
||||
public class ChairEffects {
|
||||
|
||||
Chairs plugin;
|
||||
int taskID;
|
||||
|
||||
public ChairEffects(Chairs plugin) {
|
||||
this.plugin = plugin;
|
||||
effectsTask();
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
plugin.getServer().getScheduler().cancelTask(taskID);
|
||||
taskID = 0;
|
||||
}
|
||||
|
||||
public void restart() {
|
||||
this.cancel();
|
||||
this.effectsTask();
|
||||
}
|
||||
|
||||
private void effectsTask() {
|
||||
taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
String pName = p.getName();
|
||||
if (plugin.sit.containsKey(pName)) {
|
||||
if (p.hasPermission("chairs.sit.health")) {
|
||||
double pHealthPcnt = (double) p.getHealth() / (double) p.getMaxHealth() * 100d;
|
||||
if ((pHealthPcnt < plugin.sitMaxHealth)
|
||||
&& (p.getHealth() < p.getMaxHealth())) {
|
||||
int newHealth = plugin.sitHealthPerInterval + p.getHealth();
|
||||
if (newHealth > p.getMaxHealth()) {
|
||||
newHealth = p.getMaxHealth();
|
||||
}
|
||||
p.setHealth(newHealth);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, plugin.sitEffectInterval, plugin.sitEffectInterval);
|
||||
}
|
||||
}
|
30
src/com/cnaude/chairs/ChairWatcher.java
Normal file
30
src/com/cnaude/chairs/ChairWatcher.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.cnaude.chairs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import net.minecraft.server.v1_5_R3.DataWatcher;
|
||||
import net.minecraft.server.v1_5_R3.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;
|
||||
}
|
||||
}
|
309
src/com/cnaude/chairs/Chairs.java
Normal file
309
src/com/cnaude/chairs/Chairs.java
Normal file
@@ -0,0 +1,309 @@
|
||||
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_5_R3.Packet40EntityMetadata;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Chairs extends JavaPlugin {
|
||||
private static Chairs instance = null;
|
||||
public static ChairEffects chairEffects;
|
||||
public List<ChairBlock> allowedBlocks = new ArrayList<ChairBlock>();
|
||||
public List<Material> validSigns = new ArrayList<Material>();
|
||||
public boolean sneaking, autoRotate, signCheck, permissions, notifyplayer, opsOverridePerms;
|
||||
public boolean invertedStairCheck, seatOccupiedCheck, invertedStepCheck, perItemPerms, ignoreIfBlockInHand;
|
||||
public boolean sitEffectsEnabled;
|
||||
public double sittingHeight, sittingHeightAdj, distance;
|
||||
public int maxChairWidth;
|
||||
public int sitMaxHealth;
|
||||
public int sitHealthPerInterval;
|
||||
public int sitEffectInterval;
|
||||
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");
|
||||
public PluginManager pm;
|
||||
public static ChairsIgnoreList ignoreList;
|
||||
public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled;
|
||||
private ProtocolManager protocolManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
ignoreList = new ChairsIgnoreList();
|
||||
ignoreList.load();
|
||||
pm = this.getServer().getPluginManager();
|
||||
pluginFolder = getDataFolder();
|
||||
configFile = new File(pluginFolder, "config.yml");
|
||||
createConfig();
|
||||
this.getConfig().options().copyDefaults(true);
|
||||
saveConfig();
|
||||
loadConfig();
|
||||
getServer().getPluginManager().registerEvents(new EventListener(this, ignoreList), this);
|
||||
getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList));
|
||||
if (sitEffectsEnabled) {
|
||||
logInfo("Enabling sitting effects.");
|
||||
chairEffects = new ChairEffects(this);
|
||||
}
|
||||
if (isProtocolLibLoaded()) {
|
||||
logInfo("ProtocolLib detected.");
|
||||
protocolManager = ProtocolLibrary.getProtocolManager();
|
||||
} else {
|
||||
logInfo("ProtocolLib not detected. Using NMS code methods instead.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
for (String pName : sit.keySet()) {
|
||||
Player player = getServer().getPlayer(pName);
|
||||
Location loc = player.getLocation().clone();
|
||||
loc.setY(loc.getY() + 1);
|
||||
player.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
||||
|
||||
}
|
||||
if (ignoreList != null) {
|
||||
ignoreList.save();
|
||||
}
|
||||
if (chairEffects != null) {
|
||||
chairEffects.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public void restartEffectsTask() {
|
||||
if (chairEffects != null) {
|
||||
chairEffects.restart();
|
||||
}
|
||||
}
|
||||
|
||||
private void createConfig() {
|
||||
if (!pluginFolder.exists()) {
|
||||
try {
|
||||
pluginFolder.mkdir();
|
||||
} catch (Exception e) {
|
||||
logInfo("ERROR: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (!configFile.exists()) {
|
||||
try {
|
||||
configFile.createNewFile();
|
||||
} catch (Exception e) {
|
||||
logInfo("ERROR: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isProtocolLibLoaded() {
|
||||
return (getServer().getPluginManager().getPlugin("ProtocolLib") != null);
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
autoRotate = getConfig().getBoolean("auto-rotate");
|
||||
sneaking = getConfig().getBoolean("sneaking");
|
||||
signCheck = getConfig().getBoolean("sign-check");
|
||||
sittingHeight = getConfig().getDouble("sitting-height");
|
||||
sittingHeightAdj = getConfig().getDouble("sitting-height-adj");
|
||||
distance = getConfig().getDouble("distance");
|
||||
maxChairWidth = getConfig().getInt("max-chair-width");
|
||||
permissions = getConfig().getBoolean("permissions");
|
||||
notifyplayer = getConfig().getBoolean("notify-player");
|
||||
invertedStairCheck = getConfig().getBoolean("upside-down-check");
|
||||
seatOccupiedCheck = getConfig().getBoolean("seat-occupied-check");
|
||||
invertedStepCheck = getConfig().getBoolean("upper-step-check");
|
||||
perItemPerms = getConfig().getBoolean("per-item-perms");
|
||||
opsOverridePerms = getConfig().getBoolean("ops-override-perms");
|
||||
ignoreIfBlockInHand = getConfig().getBoolean("ignore-if-block-in-hand");
|
||||
|
||||
sitEffectsEnabled = getConfig().getBoolean("sit-effects.enabled", false);
|
||||
sitEffectInterval = getConfig().getInt("sit-effects.interval",20);
|
||||
sitMaxHealth = getConfig().getInt("sit-effects.healing.max-percent",100);
|
||||
sitHealthPerInterval = getConfig().getInt("sit-effects.healing.amount",1);
|
||||
|
||||
msgSitting = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.sitting"));
|
||||
msgStanding = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.standing"));
|
||||
msgOccupied = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.occupied"));
|
||||
msgNoPerm = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.no-permission"));
|
||||
msgEnabled = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.enabled"));
|
||||
msgDisabled = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.disabled"));
|
||||
msgReloaded = ChatColor.translateAlternateColorCodes('&',getConfig().getString("messages.reloaded"));
|
||||
|
||||
for (String s : getConfig().getStringList("allowed-blocks")) {
|
||||
String type;
|
||||
double sh = sittingHeight;
|
||||
if (s.contains(":")) {
|
||||
String tmp[] = s.split(":",2);
|
||||
type = tmp[0];
|
||||
sh = Double.parseDouble(tmp[1]);
|
||||
} else {
|
||||
type = s;
|
||||
}
|
||||
try {
|
||||
Material mat;
|
||||
if (type.matches("\\d+")) {
|
||||
mat = Material.getMaterial(Integer.parseInt(type));
|
||||
} else {
|
||||
mat = Material.matchMaterial(type);
|
||||
}
|
||||
if (mat != null) {
|
||||
logInfo("Allowed block: " + mat.toString() + " => " + sh);
|
||||
allowedBlocks.add(new ChairBlock(mat,sh));
|
||||
} else {
|
||||
logError("Invalid block: " + type);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
for (String type : getConfig().getStringList("valid-signs")) {
|
||||
try {
|
||||
if (type.matches("\\d+")) {
|
||||
validSigns.add(Material.getMaterial(Integer.parseInt(type)));
|
||||
} else {
|
||||
validSigns.add(Material.matchMaterial(type));
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<String> perms = new ArrayList<String>();
|
||||
perms.add("chairs.sit");
|
||||
perms.add("chairs.reload");
|
||||
perms.add("chairs.self");
|
||||
perms.add("chairs.sit.health");
|
||||
for (String s : perms) {
|
||||
if (pm.getPermission(s) != null) {
|
||||
pm.removePermission(s);
|
||||
}
|
||||
}
|
||||
PermissionDefault pd;
|
||||
if (opsOverridePerms) {
|
||||
pd = PermissionDefault.OP;
|
||||
} else {
|
||||
pd = PermissionDefault.FALSE;
|
||||
}
|
||||
|
||||
pm.addPermission(new Permission("chairs.sit","Allow player to sit on a block.",pd));
|
||||
pm.addPermission(new Permission("chairs.reload","Allow player to reload the Chairs configuration.",pd));
|
||||
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, (byte)4);
|
||||
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 {
|
||||
Packet40EntityMetadata packet = new Packet40EntityMetadata(p.getPlayer().getEntityId(), new ChairWatcher((byte) 4), false);
|
||||
sendPacketToPlayers(packet,p);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendPacketToPlayers(PacketContainer pc, Player p) {
|
||||
for (Player onlinePlayer : Bukkit.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 : Bukkit.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 = 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())) {
|
||||
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));
|
||||
}
|
||||
|
||||
public void logError(String _message) {
|
||||
log.log(Level.SEVERE, String.format("%s %s", LOG_HEADER, _message));
|
||||
}
|
||||
|
||||
public static Chairs get() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
74
src/com/cnaude/chairs/ChairsCommand.java
Normal file
74
src/com/cnaude/chairs/ChairsCommand.java
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.cnaude.chairs;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author cnaude
|
||||
*/
|
||||
public class ChairsCommand implements CommandExecutor {
|
||||
|
||||
private final Chairs plugin;
|
||||
public ChairsIgnoreList ignoreList;
|
||||
|
||||
public ChairsCommand(Chairs instance, ChairsIgnoreList ignoreList) {
|
||||
this.plugin = instance;
|
||||
this.ignoreList = ignoreList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (sender.hasPermission("chairs.reload") || !(sender instanceof Player)) {
|
||||
plugin.reloadConfig();
|
||||
plugin.loadConfig();
|
||||
plugin.restartEffectsTask();
|
||||
if (!plugin.msgReloaded.isEmpty()) {
|
||||
sender.sendMessage(plugin.msgReloaded);
|
||||
}
|
||||
} else {
|
||||
if (!plugin.msgNoPerm.isEmpty()) {
|
||||
sender.sendMessage(plugin.msgNoPerm);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sender instanceof Player) {
|
||||
Player p = (Player) sender;
|
||||
if (args[0].equalsIgnoreCase("on")) {
|
||||
if (p.hasPermission("chairs.self") || !plugin.permissions) {
|
||||
ignoreList.removePlayer(p.getName());
|
||||
if (!plugin.msgEnabled.isEmpty()) {
|
||||
p.sendMessage(plugin.msgEnabled);
|
||||
}
|
||||
} else {
|
||||
if (!plugin.msgNoPerm.isEmpty()) {
|
||||
p.sendMessage(plugin.msgNoPerm);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("off")) {
|
||||
if (p.hasPermission("chairs.self") || !plugin.permissions) {
|
||||
ignoreList.addPlayer(p.getName());
|
||||
if (!plugin.msgDisabled.isEmpty()) {
|
||||
p.sendMessage(plugin.msgDisabled);
|
||||
}
|
||||
} else {
|
||||
if (!plugin.msgNoPerm.isEmpty()) {
|
||||
p.sendMessage(plugin.msgNoPerm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
73
src/com/cnaude/chairs/ChairsIgnoreList.java
Normal file
73
src/com/cnaude/chairs/ChairsIgnoreList.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.cnaude.chairs;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author naudec
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class ChairsIgnoreList implements Serializable{
|
||||
private static ArrayList<String> ignoreList = new ArrayList<String>();
|
||||
private static final String IGNORE_FILE = "plugins/Chairs/ignores.ser";
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void load() {
|
||||
File file = new File(IGNORE_FILE);
|
||||
if (!file.exists()) {
|
||||
Chairs.get().logInfo("Ignore file '"+file.getAbsolutePath()+"' does not exist.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
FileInputStream f_in = new FileInputStream(file);
|
||||
ObjectInputStream obj_in = new ObjectInputStream (f_in);
|
||||
ignoreList = (ArrayList<String>) obj_in.readObject();
|
||||
obj_in.close();
|
||||
Chairs.get().logInfo("Loaded ignore list. (Count = "+ignoreList.size()+")");
|
||||
}
|
||||
catch(Exception e) {
|
||||
Chairs.get().logError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
File file = new File(IGNORE_FILE);
|
||||
FileOutputStream f_out = new FileOutputStream (file);
|
||||
ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
|
||||
obj_out.writeObject (ignoreList);
|
||||
obj_out.close();
|
||||
Chairs.get().logInfo("Saved ignore list. (Count = "+ignoreList.size()+")");
|
||||
}
|
||||
catch(Exception e) {
|
||||
Chairs.get().logError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void addPlayer(String s) {
|
||||
if (ignoreList.contains(s)) {
|
||||
return;
|
||||
}
|
||||
//Chairs.get().logInfo("Adding " + s + " to ignore list.");
|
||||
ignoreList.add(s);
|
||||
}
|
||||
|
||||
public void removePlayer(String s) {
|
||||
//Chairs.get().logInfo("Removing " + s + " from ignore list.");
|
||||
ignoreList.remove(s);
|
||||
}
|
||||
|
||||
public boolean isIgnored(String s) {
|
||||
if (ignoreList.contains(s)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
361
src/com/cnaude/chairs/EventListener.java
Normal file
361
src/com/cnaude/chairs/EventListener.java
Normal file
@@ -0,0 +1,361 @@
|
||||
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;
|
||||
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.Stairs;
|
||||
import org.bukkit.material.Step;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
|
||||
public class EventListener implements Listener {
|
||||
|
||||
public Chairs plugin;
|
||||
public ChairsIgnoreList ignoreList;
|
||||
|
||||
public EventListener(Chairs plugin, ChairsIgnoreList ignoreList) {
|
||||
this.plugin = plugin;
|
||||
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())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if (event.getPlayer().getItemInHand().getType().isBlock()
|
||||
&& (event.getPlayer().getItemInHand().getTypeId() != 0)
|
||||
&& plugin.ignoreIfBlockInHand) {
|
||||
return;
|
||||
}
|
||||
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||
|
||||
Block block = event.getClickedBlock();
|
||||
Stairs stairs = null;
|
||||
Step step = null;
|
||||
double sh = plugin.sittingHeight;
|
||||
boolean blockOkay = false;
|
||||
|
||||
Player player = event.getPlayer();
|
||||
if (ignoreList.isIgnored(player.getName())) {
|
||||
return;
|
||||
}
|
||||
// Permissions Check
|
||||
if (plugin.permissions) {
|
||||
if (!player.hasPermission("chairs.sit")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (plugin.perItemPerms) {
|
||||
if (plugin.pm.getPermission("chairs.sit." + block.getTypeId()) == null) {
|
||||
plugin.pm.addPermission(new Permission("chairs.sit." + block.getTypeId(),
|
||||
"Allow players to sit on a '" + block.getType().name() + "'",
|
||||
PermissionDefault.FALSE));
|
||||
}
|
||||
if (plugin.pm.getPermission("chairs.sit." + block.getType().toString()) == null) {
|
||||
plugin.pm.addPermission(new Permission("chairs.sit." + block.getType().toString(),
|
||||
"Allow players to sit on a '" + block.getType().name() + "'",
|
||||
PermissionDefault.FALSE));
|
||||
}
|
||||
}
|
||||
|
||||
for (ChairBlock cb : plugin.allowedBlocks) {
|
||||
if (cb.getMat().equals(block.getType())) {
|
||||
blockOkay = true;
|
||||
sh = cb.getSitHeight();
|
||||
}
|
||||
}
|
||||
if (blockOkay
|
||||
|| (player.hasPermission("chairs.sit." + block.getTypeId()) && plugin.perItemPerms)
|
||||
|| (player.hasPermission("chairs.sit." + block.getType().toString()) && plugin.perItemPerms)) {
|
||||
|
||||
if (block.getState().getData() instanceof Stairs) {
|
||||
stairs = (Stairs) block.getState().getData();
|
||||
} else if (block.getState().getData() instanceof Step) {
|
||||
step = (Step) block.getState().getData();
|
||||
} else {
|
||||
sh += plugin.sittingHeightAdj;
|
||||
}
|
||||
|
||||
int chairwidth = 1;
|
||||
|
||||
// Check if block beneath chair is solid.
|
||||
if (block.getRelative(BlockFace.DOWN).isLiquid()) {
|
||||
return;
|
||||
}
|
||||
if (block.getRelative(BlockFace.DOWN).isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!block.getRelative(BlockFace.DOWN).getType().isSolid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if player is sitting.
|
||||
if (plugin.sit.containsKey(event.getPlayer().getName())) {
|
||||
plugin.sit.remove(player.getName());
|
||||
event.setCancelled(true);
|
||||
if (plugin.notifyplayer && !plugin.msgStanding.isEmpty()) {
|
||||
player.sendMessage(plugin.msgStanding);
|
||||
}
|
||||
plugin.sendStand(player);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for distance distance between player and chair.
|
||||
if (plugin.distance > 0 && player.getLocation().distance(block.getLocation().add(0.5, 0, 0.5)) > plugin.distance) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (stairs != null) {
|
||||
if (stairs.isInverted() && plugin.invertedStairCheck) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (step != null) {
|
||||
if (step.isInverted() && plugin.invertedStepCheck) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for signs.
|
||||
if (plugin.signCheck == true && stairs != null) {
|
||||
boolean sign1 = false;
|
||||
boolean sign2 = false;
|
||||
|
||||
if (stairs.getDescendingDirection() == BlockFace.NORTH || stairs.getDescendingDirection() == BlockFace.SOUTH) {
|
||||
sign1 = checkSign(block, BlockFace.EAST) || checkFrame(block, BlockFace.EAST, player);
|
||||
sign2 = checkSign(block, BlockFace.WEST) || checkFrame(block, BlockFace.WEST, player);
|
||||
} else if (stairs.getDescendingDirection() == BlockFace.EAST || stairs.getDescendingDirection() == BlockFace.WEST) {
|
||||
sign1 = checkSign(block, BlockFace.NORTH) || checkFrame(block, BlockFace.NORTH, player);
|
||||
sign2 = checkSign(block, BlockFace.SOUTH) || checkFrame(block, BlockFace.SOUTH, player);
|
||||
}
|
||||
|
||||
if (!(sign1 == true && sign2 == true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for maximal chair width.
|
||||
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);
|
||||
} else if (stairs.getDescendingDirection() == BlockFace.EAST || stairs.getDescendingDirection() == BlockFace.WEST) {
|
||||
chairwidth += getChairWidth(block, BlockFace.NORTH);
|
||||
chairwidth += getChairWidth(block, BlockFace.SOUTH);
|
||||
}
|
||||
|
||||
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())) {
|
||||
if (!plugin.msgOccupied.isEmpty()) {
|
||||
player.sendMessage(plugin.msgOccupied.replaceAll("%PLAYER%", s));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (player.getVehicle() != null) {
|
||||
player.getVehicle().remove();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
player.teleport(plocation);
|
||||
} else {
|
||||
Location plocation = block.getLocation().clone();
|
||||
plocation.setYaw(player.getLocation().getYaw());
|
||||
player.teleport(plocation.add(0.5D, (sh - 0.5D), 0.5D));
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int getChairWidth(Block block, BlockFace face) {
|
||||
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++) {
|
||||
Block relative = block.getRelative(face, i);
|
||||
if (relative.getState().getData() instanceof Stairs) {
|
||||
if (isValidChair(relative) && ((Stairs) relative.getState().getData()).getDescendingDirection() == ((Stairs) block.getState().getData()).getDescendingDirection()) {
|
||||
width++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
private boolean checkSign(Block block, BlockFace face) {
|
||||
// Go through the blocks next to the clicked block and check if are signs on the end.
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
Block relative = block.getRelative(face, i);
|
||||
if (!isValidChair(relative) || (block.getState().getData() instanceof Stairs
|
||||
&& ((Stairs) relative.getState().getData()).getDescendingDirection()
|
||||
!= ((Stairs) block.getState().getData()).getDescendingDirection())) {
|
||||
if (plugin.validSigns.contains(relative.getType())) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean checkFrame(Block block, BlockFace face, Player player) {
|
||||
// Go through the blocks next to the clicked block and check if are signs on the end.
|
||||
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
Block relative = block.getRelative(face, i);
|
||||
int x = relative.getLocation().getBlockX();
|
||||
int y = relative.getLocation().getBlockY();
|
||||
int z = relative.getLocation().getBlockZ();
|
||||
if (!isValidChair(relative) || (block.getState().getData() instanceof Stairs
|
||||
&& ((Stairs) relative.getState().getData()).getDescendingDirection()
|
||||
!= ((Stairs) block.getState().getData()).getDescendingDirection())) {
|
||||
if (relative.getType().equals(Material.AIR)) {
|
||||
for (Entity e : player.getNearbyEntities(plugin.distance, plugin.distance, plugin.distance)) {
|
||||
if (e instanceof ItemFrame && plugin.validSigns.contains(Material.ITEM_FRAME)) {
|
||||
int x2 = e.getLocation().getBlockX();
|
||||
int y2 = e.getLocation().getBlockY();
|
||||
int z2 = e.getLocation().getBlockZ();
|
||||
if (x == x2 && y == y2 && z == z2) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user