From 51400658c0a1de0b23a83ed3de6f19cd61b65d16 Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 23 Aug 2015 23:55:48 +0800 Subject: [PATCH] Update... --- src/cn/citycraft/CityBuild/CityBuild.java | 122 ++++++++++++------ src/cn/citycraft/CityBuild/config/Data.java | 43 +++++++ src/cn/citycraft/CityBuild/listen/Build.java | 123 +++++++++---------- src/config.yml | 5 +- 4 files changed, 186 insertions(+), 107 deletions(-) create mode 100644 src/cn/citycraft/CityBuild/config/Data.java diff --git a/src/cn/citycraft/CityBuild/CityBuild.java b/src/cn/citycraft/CityBuild/CityBuild.java index 8ead958..789c536 100644 --- a/src/cn/citycraft/CityBuild/CityBuild.java +++ b/src/cn/citycraft/CityBuild/CityBuild.java @@ -1,56 +1,102 @@ package cn.citycraft.CityBuild; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import cn.citycraft.CityBuild.config.Config; +import cn.citycraft.CityBuild.config.Data; import cn.citycraft.CityBuild.listen.Build; public class CityBuild extends JavaPlugin implements CommandExecutor { - public String servername; - public String pluginname; - public boolean tipplayer; + public Map mhl; + public String servername; + public String pluginname; + public boolean tipplayer; - public String getfullmsg(String path) { - return servername + pluginname + " " + getmessage(path); - } - - public String getmessage(String path) { - return Config.getMessage(path); - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String string, - String[] args) { - if (args.length == 1) { - if (args[0].equalsIgnoreCase("reload")) { - onLoad(); - sender.sendMessage(pluginname + getmessage("Message.Reload")); - return true; - } + public String getfullmsg(String path) { + return servername + pluginname + " " + getmessage(path); } - return false; - } - @Override - public void onDisable() { - getLogger().info(pluginname + "城市世界建筑保护已卸载!"); - } + public String getmessage(String path) { + return Config.getMessage(path); + } - @Override - public void onEnable() { - Bukkit.getPluginManager().registerEvents(new Build(this), this); - getLogger().info(pluginname + "城市世界建筑保护已加载!"); - } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) { + if (args.length == 1) { + switch (args[0]) { + case "reload": + Data.save(); + onLoad(); + sender.sendMessage(getfullmsg("Message.Reload")); + return true; + case "look": + sender.sendMessage(getfullmsg("Message.Look")); + if (mhl.isEmpty()) { + sender.sendMessage(getfullmsg("Message.NotHave")); + } else { + for (Entry info : mhl.entrySet()) { + String name = info.getValue(); + Location loc = info.getKey(); + World w = loc.getWorld(); + int x = loc.getBlockX(); + int z = loc.getBlockZ(); + sender.sendMessage(getfullmsg("Message.Info").replaceAll("%player%", name) + .replaceAll("%world%", w.getName()).replaceAll("%x%", x + "") + .replaceAll("%z%", z + "")); + } + } + return true; + case "clear": + mhl.clear(); + Data.getInstance().set("MaxHeightList", null); + Data.save(); + sender.sendMessage(getfullmsg("Message.NotHave")); + return true; + case "put": + Player p = (Player) sender; + mhl.put(p.getLocation(), p.getName()); + Data.getInstance().set("MaxHeightList", mhl); + Data.save(); + } + } + return false; + } - @Override - public void onLoad() { - Config.load(this, "1.1"); - servername = getmessage("servername"); - pluginname = getmessage("pluginname"); - tipplayer = Config.getInstance().getBoolean("tipplayer"); - } + @Override + public void onDisable() { + Data.getInstance().set("MaxHeightList", mhl); + Data.save(); + getLogger().info(pluginname + "城市世界建筑保护已卸载!"); + } + + @Override + public void onEnable() { + Bukkit.getPluginManager().registerEvents(new Build(this), this); + getLogger().info(pluginname + "城市世界建筑保护已加载!"); + } + + @SuppressWarnings("unchecked") + @Override + public void onLoad() { + Config.load(this, "1.1"); + Data.load(this); + mhl = (Map) Config.getInstance().get("MaxHeightList"); + if (mhl == null) { + mhl = new HashMap<>(); + } + servername = getmessage("servername"); + pluginname = getmessage("pluginname"); + tipplayer = Config.getInstance().getBoolean("tipplayer"); + } } diff --git a/src/cn/citycraft/CityBuild/config/Data.java b/src/cn/citycraft/CityBuild/config/Data.java new file mode 100644 index 0000000..f070014 --- /dev/null +++ b/src/cn/citycraft/CityBuild/config/Data.java @@ -0,0 +1,43 @@ +package cn.citycraft.CityBuild.config; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.plugin.Plugin; + +public class Data extends ConfigLoader { + private static String CONFIG_NAME = "data.yml"; + private static FileConfig instance; + private static File file; + + public static FileConfig getInstance() { + return instance; + } + + public static String getMessage(String path) { + String message = instance.getString(path); + if (message != null) { + message = message.replaceAll("&", "§"); + } + return message; + } + + public static void load(Plugin p) { + new Data(p); + } + + public static void save() { + try { + instance.save(file); + } catch (IOException e) { + saveError(file); + e.printStackTrace(); + } + } + + public Data(Plugin p) { + super(p, CONFIG_NAME, false); + file = new File(p.getDataFolder(), CONFIG_NAME); + instance = super.getInstance(); + } +} diff --git a/src/cn/citycraft/CityBuild/listen/Build.java b/src/cn/citycraft/CityBuild/listen/Build.java index 90255ec..1fda9ca 100644 --- a/src/cn/citycraft/CityBuild/listen/Build.java +++ b/src/cn/citycraft/CityBuild/listen/Build.java @@ -1,11 +1,5 @@ package cn.citycraft.CityBuild.listen; -import cn.citycraft.CityBuild.CityBuild; -import cn.citycraft.CityBuild.config.Config; - -import com.bekvon.bukkit.residence.Residence; -import com.bekvon.bukkit.residence.protection.ClaimedResidence; - import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -18,6 +12,12 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerInteractEvent; +import cn.citycraft.CityBuild.CityBuild; +import cn.citycraft.CityBuild.config.Config; + +import com.bekvon.bukkit.residence.Residence; +import com.bekvon.bukkit.residence.protection.ClaimedResidence; + public class Build implements Listener { CityBuild plugin; @@ -28,44 +28,14 @@ public class Build implements Listener { // PlayerInteractEvent.class - @EventHandler(ignoreCancelled = true) - public void playerInteract(PlayerInteractEvent event) { - event.setCancelled(CancelEvent(event.getPlayer(), event.getMaterial())); - } - @EventHandler(ignoreCancelled = true) public void blockBreak(BlockBreakEvent event) { - event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), - false)); + event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), false)); } @EventHandler(ignoreCancelled = true) public void blockplace(BlockPlaceEvent event) { - event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), - true)); - } - - public boolean CancelEvent(Player p, Material m) { - ClaimedResidence res = Residence.getResidenceManager().getByLoc( - p.getLocation()); - if (res == null && !p.hasPermission("cb.build")) { - String blockname = m.name(); - for (String protectworld : Config.getInstance().getStringList( - "ProtectWorld")) { - if (protectworld.equalsIgnoreCase(p.getWorld().getName())) { - for (String notallowblock : Config.getInstance() - .getStringList("NotAllowList")) { - if (blockname.equalsIgnoreCase(notallowblock)) { - if (plugin.tipplayer) - p.getPlayer().sendMessage( - plugin.getfullmsg("Message.NotAllow")); - return true; - } - } - } - } - } - return false; + event.setCancelled(CancelEvent(event.getPlayer(), event.getBlock(), true)); } public boolean CancelEvent(Player p, Block b, boolean place) { @@ -77,46 +47,37 @@ public class Build implements Listener { int blockx = b.getX(); int blocky = b.getY(); int blockz = b.getZ(); - for (String protectworld : Config.getInstance().getStringList( - "ProtectWorld")) { + for (String protectworld : Config.getInstance().getStringList("ProtectWorld")) { if (protectworld.equalsIgnoreCase(blockw.getName())) { - for (String allowblock : Config.getInstance() - .getStringList("AllowList")) { + for (String allowblock : Config.getInstance().getStringList("AllowList")) { if (blockname.equalsIgnoreCase(allowblock)) { - for (String noticeblock : Config.getInstance() - .getStringList("NoticeList")) { - if (blockname.equalsIgnoreCase(noticeblock) - && place) { - if (plugin.tipplayer) - p.getPlayer() - .sendMessage( - plugin.getfullmsg("Message.Tip")); + for (String noticeblock : Config.getInstance().getStringList( + "NoticeList")) { + if (blockname.equalsIgnoreCase(noticeblock) && place) { + if (plugin.tipplayer) { + p.getPlayer().sendMessage(plugin.getfullmsg("Message.Tip")); + } return false; } } - int maxheight = Config.getInstance().getInt( - "MaxHeight"); - if (blocky > maxheight - && blockname.equalsIgnoreCase("Dirt")) { - if (plugin.tipplayer) + int maxheight = Config.getInstance().getInt("MaxHeight"); + if (blocky > maxheight && blockname.equalsIgnoreCase("Dirt")) { + if (plugin.tipplayer) { p.getPlayer().sendMessage( - plugin.getfullmsg("Message.Max") - .replaceAll("%maxheight%", - maxheight + "")); + plugin.getfullmsg("Message.Max").replaceAll( + "%maxheight%", maxheight + "")); + } Bukkit.broadcast( plugin.getfullmsg("Message.Notice1") - .replaceAll("%player%", - p.getDisplayName()) - .replaceAll("%maxheight%", - maxheight + ""), + .replaceAll("%player%", p.getDisplayName()) + .replaceAll("%maxheight%", maxheight + ""), "cb.notice"); Bukkit.broadcast( plugin.getfullmsg("Message.Notice2") - .replaceAll("%world%", - blockw.getName()) - .replaceAll("%x%", blockx + "") - .replaceAll("%z%", blockz + ""), - "cb.notice"); + .replaceAll("%world%", blockw.getName()) + .replaceAll("%x%", blockx + "") + .replaceAll("%z%", blockz + ""), "cb.notice"); + plugin.mhl.put(loc, p.getName()); return true; } return false; @@ -125,12 +86,38 @@ public class Build implements Listener { String[] message = { plugin.getfullmsg("Message.Wran"), plugin.getfullmsg("Message.Allow"), plugin.getfullmsg("Message.Relieve") }; - if (plugin.tipplayer) + if (plugin.tipplayer) { p.getPlayer().sendMessage(message); + } return true; } } } return false; } + + public boolean CancelEvent(Player p, Material m) { + ClaimedResidence res = Residence.getResidenceManager().getByLoc(p.getLocation()); + if (res == null && !p.hasPermission("cb.build")) { + String blockname = m.name(); + for (String protectworld : Config.getInstance().getStringList("ProtectWorld")) { + if (protectworld.equalsIgnoreCase(p.getWorld().getName())) { + for (String notallowblock : Config.getInstance().getStringList("NotAllowList")) { + if (blockname.equalsIgnoreCase(notallowblock)) { + if (plugin.tipplayer) { + p.getPlayer().sendMessage(plugin.getfullmsg("Message.NotAllow")); + } + return true; + } + } + } + } + } + return false; + } + + @EventHandler(ignoreCancelled = true) + public void playerInteract(PlayerInteractEvent event) { + event.setCancelled(CancelEvent(event.getPlayer(), event.getMaterial())); + } } diff --git a/src/config.yml b/src/config.yml index 714dba3..b5650a9 100644 --- a/src/config.yml +++ b/src/config.yml @@ -18,7 +18,10 @@ Message: #管理组收到的提示 Notice1: '&6玩家&r:&a%player%&e未在领地内&c用&3泥土&c建筑高度超过 &5%maxheight%&c 的建筑物!' Notice2: '&5坐标定位&r:&a世界%world% &dX:%x% Z:%z% &4请管理到目标地点检查!' - + Look: '&c当前已检测到有可能的擎天柱数据如下: ' + Info: '&6玩家&r:&a%player%&e在地点&r:&a世界%world% &dX:%x% Z:%z% &c建筑过高&4请检查!' + NotHave: '&5未找到有可能的擎天柱数据!' + Clear: '&c已清理擎天柱数据!' #未圈地建筑提示 #警告 Wran: '&4城市世界除以下方块外禁止放置/破坏!'