From 1ff4a0e29bcda15f499cdde34974d100f067e65b Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 17 May 2015 13:21:54 +0800 Subject: [PATCH] init project... Signed-off-by: j502647092 --- .gitignore | 40 ++++++ pom.xml | 46 +++++++ .../TeleportRandom/TeleportRandom.java | 116 ++++++++++++++++++ src/cn/citycraft/Utils/config/Config.java | 55 +++++++++ .../citycraft/Utils/config/ConfigLoader.java | 102 +++++++++++++++ src/cn/citycraft/Utils/config/FileConfig.java | 88 +++++++++++++ src/config.yml | 39 ++++++ src/plugin.yml | 20 +++ 8 files changed, 506 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/cn/citycraft/TeleportRandom/TeleportRandom.java create mode 100644 src/cn/citycraft/Utils/config/Config.java create mode 100644 src/cn/citycraft/Utils/config/ConfigLoader.java create mode 100644 src/cn/citycraft/Utils/config/FileConfig.java create mode 100644 src/config.yml create mode 100644 src/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9626090 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Eclipse stuff +/.classpath +/.project +/.settings + +# netbeans +/nbproject + +# we use maven! +/build.xml + +# maven +/target +/repo + +# vim +.*.sw[a-p] + +# various other potential build files +/build +/bin +/dist +/manifest.mf + +/world + +# Mac filesystem dust +*.DS_Store + +# intellij +*.iml +*.ipr +*.iws +.idea/ + +# Project Stuff +/src/main/resources/Soulbound + +# Atlassian Stuff +/atlassian-ide-plugin.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8bc726a --- /dev/null +++ b/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + cn.citycraft + TeleportRandom + 0.0.1-SNAPSHOT + cn.CityCraft.TeleportRandom + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + + + org.spigotmc + spigot-api + jar + 1.8.3-R0.1-SNAPSHOT + + + + UTF-8 + + \ No newline at end of file diff --git a/src/cn/citycraft/TeleportRandom/TeleportRandom.java b/src/cn/citycraft/TeleportRandom/TeleportRandom.java new file mode 100644 index 0000000..28d4d1d --- /dev/null +++ b/src/cn/citycraft/TeleportRandom/TeleportRandom.java @@ -0,0 +1,116 @@ +package cn.citycraft.TeleportRandom; + +import java.util.Random; + +import org.bukkit.Location; +import org.bukkit.Material; +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.Utils.config.Config; + +public class TeleportRandom extends JavaPlugin implements CommandExecutor { + public String servername; + public String pluginname; + + public void onLoad() { + Config.load(this); + servername = getmessage("servername"); + pluginname = getmessage("pluginname"); + } + + public boolean onCommand(CommandSender sender, Command cmd, String string, + String[] args) { + if (cmd.getName().equalsIgnoreCase("tpr")) { + if (args.length != 1) + return false; + if (args[0].equalsIgnoreCase("reload")) { + this.onLoad(); + sender.sendMessage(servername + pluginname + + getmessage("Message.Reload")); + return true; + } + if (sender instanceof Player) { + Player p = (Player) sender; + if (!p.hasPermission("tpr.use")) { + sender.sendMessage(servername + pluginname + + getmessage("Message.NoPerm")); + return true; + } + RandomTP(args, p); + return true; + } + sender.sendMessage(servername + pluginname + "控制台无法使用此命令!"); + return true; + } + return false; + } + + public void RandomTP(String[] limit, Player p) { + Random rr = new Random(); + int lr = Config.getInstance().getInt("default"); + if (limit.length != 1) { + p.sendMessage(servername + pluginname + + getmessage("Message.default1")); + p.sendMessage(servername + pluginname + + getmessage("Message.default2")); + } else { + lr = Integer.parseInt(limit[0]); + int lrLimit = Config.getInstance().getInt("Limit"); + if (lr > lrLimit) { + lr = lrLimit; + p.sendMessage(servername + + pluginname + + getmessage("Message.Wran").replace("%limit%", + lrLimit + "")); + } + } + Location l = p.getLocation(); + World world = l.getWorld(); + int x = rr.nextInt(lr); + int z = rr.nextInt(lr); + int xf = rr.nextInt(x); + int yf = rr.nextInt(z); + if (xf % 2 != 0) { + x = -x; + } + if (yf % 2 != 0) { + z = -z; + } + int y = world.getHighestBlockYAt(x, z); + final Location trl = new Location(world, x, y - 1, z); + String blockname = trl.getBlock().getType().name(); + final Material rbm = trl.getBlock().getType(); + for (String protectblock : Config.getInstance().getStringList( + "ProtectBlock")) { + if (protectblock.equalsIgnoreCase(blockname)) { + trl.getBlock().setType(Material.GLASS); + p.sendMessage(servername + pluginname + + getmessage("Message.Protect")); + this.getServer().getScheduler() + .runTaskLater(this, new Runnable() { + public void run() { + trl.getBlock().setType(rbm); + } + }, 200); + } + } + Location nrl = new Location(world, x, y + 2, z); + p.teleport(nrl); + p.sendMessage(servername + + pluginname + + getmessage("Message.Tip").replace( + "%world%", + world.getName().replace("%x%", x + "") + .replace("%z%", z + ""))); + } + + public String getmessage(String path) { + String message = Config.getMessage(path).replaceAll("&", "§"); + return message; + } +} diff --git a/src/cn/citycraft/Utils/config/Config.java b/src/cn/citycraft/Utils/config/Config.java new file mode 100644 index 0000000..48b17ae --- /dev/null +++ b/src/cn/citycraft/Utils/config/Config.java @@ -0,0 +1,55 @@ +package cn.citycraft.Utils.config; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.plugin.Plugin; + +public class Config extends ConfigLoader { + private static String CONFIG_NAME = "config.yml"; + private static FileConfig instance; + private static File file; + + public Config(Plugin p) { + super(p, CONFIG_NAME); + file = new File(p.getDataFolder(), CONFIG_NAME); + instance = super.getInstance(); + } + + public Config(Plugin p, String ver) { + super(p, CONFIG_NAME, ver); + instance = super.getInstance(); + } + + public static void load(Plugin p) { + new Config(p); + } + + public static void load(Plugin p, String ver) { + new Config(p, ver); + } + + 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 String[] getStringArray(String path) { + return instance.getStringList(path).toArray(new String[0]); + } + + public static void save(){ + try { + instance.save(file); + } catch (IOException e) { + saveError(file); + e.printStackTrace(); + } + } +} diff --git a/src/cn/citycraft/Utils/config/ConfigLoader.java b/src/cn/citycraft/Utils/config/ConfigLoader.java new file mode 100644 index 0000000..e53e9eb --- /dev/null +++ b/src/cn/citycraft/Utils/config/ConfigLoader.java @@ -0,0 +1,102 @@ +package cn.citycraft.Utils.config; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.plugin.Plugin; + +public class ConfigLoader extends FileConfig { + protected static FileConfig config; + protected static boolean tip = true; + protected static Plugin plugin; + + public ConfigLoader(Plugin p, File file) { + ConfigLoader.plugin = p; + config = loadConfig(p, file, null, true); + } + + public ConfigLoader(Plugin p, File file, boolean res) { + ConfigLoader.plugin = p; + config = loadConfig(p, file, null, res); + } + + public ConfigLoader(Plugin p, File file, String ver) { + ConfigLoader.plugin = p; + config = loadConfig(p, file, ver, true); + } + + public ConfigLoader(Plugin p, File file, String ver, boolean res) { + ConfigLoader.plugin = p; + config = loadConfig(p, file, ver, res); + } + + public ConfigLoader(Plugin p, String filename) { + ConfigLoader.plugin = p; + config = loadConfig(p, new File(p.getDataFolder(), filename), null, + true); + } + + public ConfigLoader(Plugin p, String filename, boolean res) { + ConfigLoader.plugin = p; + config = loadConfig(p, new File(p.getDataFolder(), filename), null, res); + } + + public ConfigLoader(Plugin p, String filename, String ver) { + ConfigLoader.plugin = p; + config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true); + } + + public ConfigLoader(Plugin p, String filename, String ver, boolean res) { + ConfigLoader.plugin = p; + config = loadConfig(p, new File(p.getDataFolder(), filename), ver, true); + } + + public static FileConfig getInstance() { + return config; + } + + public FileConfig loadConfig(Plugin p, File file, String ver, boolean res) { + tip = res ; + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + p.getLogger().info("创建新的文件夹" + file.getParentFile().getAbsolutePath() + "..."); + } + if (!file.exists()) { + fileCreate(p, file, res); + } else { + if (ver != null) { + FileConfig configcheck = init(file); + String version = configcheck.getString("version"); + if (version == null || !version.equals(ver)) { + p.saveResource(file.getName(), true); + p.getLogger().warning( + "配置文件: " + file.getName() + " 版本过低 正在升级..."); + } + } + } + if (tip) + p.getLogger().info( + "载入配置文件: " + file.getName() + + (ver != null ? " 版本: " + ver : "")); + return init(file); + } + + private void fileCreate(Plugin p, File file, boolean res) { + if (res) { + p.saveResource(file.getName(), false); + } else { + try { + p.getLogger().info("创建新的配置文件" + file.getAbsolutePath() + "..."); + file.createNewFile(); + } catch (IOException e) { + p.getLogger().info("配置文件" + file.getName() + "创建失败..."); + e.printStackTrace(); + } + } + } + + public static void saveError(File file) { + plugin.getLogger().info("配置文件" + file.getName() + "保存错误..."); + } + +} diff --git a/src/cn/citycraft/Utils/config/FileConfig.java b/src/cn/citycraft/Utils/config/FileConfig.java new file mode 100644 index 0000000..f381ae8 --- /dev/null +++ b/src/cn/citycraft/Utils/config/FileConfig.java @@ -0,0 +1,88 @@ +package cn.citycraft.Utils.config; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.logging.Level; + +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.file.YamlConstructor; +import org.bukkit.configuration.file.YamlRepresenter; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.representer.Representer; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; + +/** + * An implementation of {@link Configuration} which saves all files in Yaml. + * Note that this implementation is not synchronized. + */ +public class FileConfig extends YamlConfiguration { + + protected final DumperOptions yamlOptions = new DumperOptions(); + protected final Representer yamlRepresenter = new YamlRepresenter(); + protected final Yaml yaml = new Yaml(new YamlConstructor(), + yamlRepresenter, yamlOptions); + + public static FileConfig init(File file) { + return FileConfig.loadConfiguration(file); + } + + public static FileConfig loadConfiguration(File file) { + Validate.notNull(file, "File cannot be null"); + FileConfig config = new FileConfig(); + try { + config.load(file); + } catch (FileNotFoundException ex) { + } catch (IOException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex); + } catch (InvalidConfigurationException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex); + } + return config; + } + + @Override + public String saveToString() { + yamlOptions.setIndent(options().indent()); + yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + String header = buildHeader(); + String dump = yaml.dump(getValues(false)); + if (dump.equals(BLANK_CONFIG)) { + dump = ""; + } + return header + dump; + } + + public void load(File file) throws FileNotFoundException, IOException, + InvalidConfigurationException { + Validate.notNull(file, "File cannot be null"); + final FileInputStream stream = new FileInputStream(file); + load(new InputStreamReader(stream, Charsets.UTF_8)); + } + + public void save(File file) throws IOException { + Validate.notNull(file, "File cannot be null"); + Files.createParentDirs(file); + String data = saveToString(); + Writer writer = new OutputStreamWriter(new FileOutputStream(file), + Charsets.UTF_8); + try { + writer.write(data); + } finally { + writer.close(); + } + } +} diff --git a/src/config.yml b/src/config.yml new file mode 100644 index 0000000..aa7b573 --- /dev/null +++ b/src/config.yml @@ -0,0 +1,39 @@ +#ļΪͲļ +# +servername: '' +# +pluginname: '6[b͡6]&r' +#Ƿʾ +tipplayer: true +#ʾϢ +Message: + Reload: '&aļ룡' + +#ʾ + #Ĭʾ + default1: '&5δָΧ ʹĬֵ1000!' + default2: '&cȷʹ÷/tpr [ͷΧ]!' + #ʾ + Tip: '&a &a%world% &dX:%x% Z:%z%' + # + Wran: '&cͷΧ Ϊ%limit%!' + #ͱʾ + Protect: '&3ͱ ·&cΣ&3 Զɲ(10ʧ) !' + #ûд͵Ȩ + NoPerm: '&cǰûʹ͵Ȩޣ' + +#͵(ִСд) +AllowWorld: + - FutureCity + - ZiYuan + +#Ĭϴ; +default: 10000 +#; +Limit: 10000 + +#ͱб(ִСд) +ProtectBlock: + - WATER + - LAVA + - CACTUS \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..8993f4e --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,20 @@ +name: TeleportRandom +main: cn.citycraft.TeleportRandom.TeleportRandom +version: 1.0 +commands: + tpr: + description: 随机传送插件命令 + usage: 使用/tpr [随机传送范围] 进行随机传送! +permissions: + tpr.*: + description: 允许使用所有命令! + default: op + children: + tpr.use: true + tpr.reload: true + tpr.use: + description: 允许使用随机传送! + default: true + tpr.reload: + description: 允许重载随机传送! + default: op \ No newline at end of file