From cd65375bd9d0200f89f53d3a85b98e461dccb06b Mon Sep 17 00:00:00 2001 From: drtshock Date: Tue, 1 Jul 2014 13:19:36 -0500 Subject: [PATCH] Add TL file. Use CommandExecutor to properly handle commands instead of using the preprocess listener and breaking other plugins. --- .../com/massivecraft/factions/Faction.java | 13 +-- .../com/massivecraft/factions/Factions.java | 26 +++--- .../factions/zcore/FCommandHandler.java | 19 +++++ .../massivecraft/factions/zcore/MPlugin.java | 68 +++++++++++++++- .../zcore/MPluginSecretPlayerListener.java | 3 +- .../zcore/persist/PlayerEntityCollection.java | 7 +- .../massivecraft/factions/zcore/util/TL.java | 80 +++++++++++++++++++ src/main/resources/lang.yml | 11 +++ 8 files changed, 195 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/zcore/FCommandHandler.java create mode 100644 src/main/java/com/massivecraft/factions/zcore/util/TL.java create mode 100644 src/main/resources/lang.yml diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index fcc28de0..5e376812 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -652,16 +652,9 @@ public class Faction extends Entity implements EconomyParticipator { public boolean playerHasOwnershipRights(FPlayer fplayer, FLocation loc) { // in own faction, with sufficient role or permission to bypass ownership? - if - ( - fplayer.getFaction() == this - && - ( - fplayer.getRole().isAtLeast(Conf.ownedAreaModeratorsBypass ? Role.MODERATOR : Role.ADMIN) - || - Permission.OWNERSHIP_BYPASS.has(fplayer.getPlayer()) - ) - ) { + if (fplayer.getFaction() == this + && (fplayer.getRole().isAtLeast(Conf.ownedAreaModeratorsBypass ? Role.MODERATOR : Role.ADMIN) + || Permission.OWNERSHIP_BYPASS.has(fplayer.getPlayer()))) { return true; } diff --git a/src/main/java/com/massivecraft/factions/Factions.java b/src/main/java/com/massivecraft/factions/Factions.java index 749c9eda..cdf7f4d2 100644 --- a/src/main/java/com/massivecraft/factions/Factions.java +++ b/src/main/java/com/massivecraft/factions/Factions.java @@ -2,6 +2,7 @@ package com.massivecraft.factions; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.zcore.persist.EntityCollection; +import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TextUtil; import org.bukkit.ChatColor; import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken; @@ -21,14 +22,7 @@ public class Factions extends EntityCollection { P p = P.p; private Factions() { - super - ( - Faction.class, - new CopyOnWriteArrayList(), - new ConcurrentHashMap(), - new File(P.p.getDataFolder(), "factions.json"), - P.p.gson - ); + super(Faction.class, new CopyOnWriteArrayList(), new ConcurrentHashMap(), new File(P.p.getDataFolder(), "factions.json"), P.p.gson); } @Override @@ -44,32 +38,32 @@ public class Factions extends EntityCollection { // Make sure the default neutral faction exists if (!this.exists("0")) { Faction faction = this.create("0"); - faction.setTag(ChatColor.DARK_GREEN + "Wilderness"); - faction.setDescription(""); + faction.setTag(TL.WILDERNESS.toString()); + faction.setDescription(TL.WILDERNESS_DESCRIPTION.toString()); } // Make sure the safe zone faction exists if (!this.exists("-1")) { Faction faction = this.create("-1"); - faction.setTag("SafeZone"); - faction.setDescription("Free from PVP and monsters"); + faction.setTag(TL.SAFEZONE.toString()); + faction.setDescription(TL.SAFEZONE_DESCRIPTION.toString()); } else { // if SafeZone has old pre-1.6.0 name, rename it to remove troublesome " " Faction faction = this.getSafeZone(); if (faction.getTag().contains(" ")) - faction.setTag("SafeZone"); + faction.setTag(TL.SAFEZONE.toString()); } // Make sure the war zone faction exists if (!this.exists("-2")) { Faction faction = this.create("-2"); - faction.setTag("WarZone"); - faction.setDescription("Not the safest place to be"); + faction.setTag(TL.WARZONE.toString()); + faction.setDescription(TL.WARZONE_DESCRIPTION.toString()); } else { // if WarZone has old pre-1.6.0 name, rename it to remove troublesome " " Faction faction = this.getWarZone(); if (faction.getTag().contains(" ")) - faction.setTag("WarZone"); + faction.setTag(TL.WARZONE.toString()); } // populate all faction player lists diff --git a/src/main/java/com/massivecraft/factions/zcore/FCommandHandler.java b/src/main/java/com/massivecraft/factions/zcore/FCommandHandler.java new file mode 100644 index 00000000..f94a63c8 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/zcore/FCommandHandler.java @@ -0,0 +1,19 @@ +package com.massivecraft.factions.zcore; + +import com.massivecraft.factions.P; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class FCommandHandler implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (P.p.handleCommand(sender, cmd.getName() + args)) { + if (P.p.logPlayerCommands()) + Bukkit.getLogger().info("[PLAYER_COMMAND] " + sender.getName() + ": " + cmd.getName() + args); + } + return false; + } +} diff --git a/src/main/java/com/massivecraft/factions/zcore/MPlugin.java b/src/main/java/com/massivecraft/factions/zcore/MPlugin.java index 6a12ae81..97bebe87 100644 --- a/src/main/java/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/main/java/com/massivecraft/factions/zcore/MPlugin.java @@ -6,15 +6,17 @@ import com.massivecraft.factions.zcore.persist.EM; import com.massivecraft.factions.zcore.persist.SaveTask; import com.massivecraft.factions.zcore.util.PermUtil; import com.massivecraft.factions.zcore.util.Persist; +import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TextUtil; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.craftbukkit.libs.com.google.gson.Gson; import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder; import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken; import org.bukkit.plugin.java.JavaPlugin; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.*; @@ -98,6 +100,7 @@ public abstract class MPlugin extends JavaPlugin { this.mPluginSecretServerListener = new MPluginSecretServerListener(this); getServer().getPluginManager().registerEvents(this.mPluginSecretPlayerListener, this); getServer().getPluginManager().registerEvents(this.mPluginSecretServerListener, this); + getCommand("factions").setExecutor(new FCommandHandler()); // Register recurring tasks @@ -106,6 +109,8 @@ public abstract class MPlugin extends JavaPlugin { saveTask = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new SaveTask(this), saveTicks, saveTicks); } + loadLang(); + loadSuccessful = true; return true; } @@ -114,6 +119,67 @@ public abstract class MPlugin extends JavaPlugin { log("=== ENABLE DONE (Took " + (System.currentTimeMillis() - timeEnableStart) + "ms) ==="); } + private void loadLang() { + File lang = new File(getDataFolder(), "lang.yml"); + OutputStream out = null; + InputStream defLangStream = this.getResource("lang.yml"); + if (!lang.exists()) { + try { + getDataFolder().mkdir(); + lang.createNewFile(); + if (defLangStream != null) { + out = new FileOutputStream(lang); + int read; + byte[] bytes = new byte[1024]; + + while ((read = defLangStream.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defLangStream); + TL.setFile(defConfig); + return; + } + } catch (IOException e) { + e.printStackTrace(); // So they notice + getLogger().severe("[Factions] Couldn't create language file."); + getLogger().severe("[Factions] This is a fatal error. Now disabling"); + this.setEnabled(false); // Without it loaded, we can't send them messages + } finally { + if (defLangStream != null) { + try { + defLangStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (out != null) { + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + } + + YamlConfiguration conf = YamlConfiguration.loadConfiguration(lang); + for (TL item : TL.values()) { + if (conf.getString(item.getPath()) == null) { + conf.set(item.getPath(), item.getDefault()); + } + } + + TL.setFile(conf); + try { + conf.save(lang); + } catch (IOException e) { + getLogger().log(Level.WARNING, "Factions: Failed to save lang.yml."); + getLogger().log(Level.WARNING, "Factions: Report this stack trace to drtshock."); + e.printStackTrace(); + } + } + public void onDisable() { if (saveTask != null) { this.getServer().getScheduler().cancelTask(saveTask); diff --git a/src/main/java/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java b/src/main/java/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java index 6f03821e..79937618 100644 --- a/src/main/java/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/zcore/MPluginSecretPlayerListener.java @@ -22,7 +22,8 @@ public class MPluginSecretPlayerListener implements Listener { this.p = p; } - @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + // We're now using FCommandHandler for this to do things properly. + //@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { if (p.handleCommand(event.getPlayer(), event.getMessage())) { if (p.logPlayerCommands()) diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java b/src/main/java/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java index dcd4d1bf..3945469f 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java @@ -29,10 +29,9 @@ public abstract class PlayerEntityCollection extends EntityCol return this.get(player.getUniqueId().toString()); } - public E get(Player player) - { - return this.get(player.getUniqueId().toString()); - } + public E get(Player player) { + return this.get(player.getUniqueId().toString()); + } public Set getOnline() { Set entities = new HashSet(); diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java new file mode 100644 index 00000000..6a0f8bd0 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2013 drtshock + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.massivecraft.factions.zcore.util; + +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.YamlConfiguration; + +/** + * An enum for requesting strings from the language file. + */ +public enum TL { + TITLE("title", "&bFactions &0|&r"), + WILDERNESS("wilderness", "&2Wilderness"), + WILDERNESS_DESCRIPTION("wilderness-description", ""), + WARZONE("warzone", "&4Warzone"), + WARZONE_DESCRIPTION("warzone-description", "Not the safest place to be."), + SAFEZONE("safezone", "&6Safezone"), + SAFEZONE_DESCRIPTION("safezone-description", "Free from pvp and monsters."); + + private String path; + private String def; + private static YamlConfiguration LANG; + + /** + * Lang enum constructor. + * + * @param path The string path. + * @param start The default string. + */ + TL(String path, String start) { + this.path = path; + this.def = start; + } + + /** + * Set the {@code YamlConfiguration} to use. + * + * @param config The config to set. + */ + public static void setFile(YamlConfiguration config) { + LANG = config; + } + + @Override + public String toString() { + return this == TITLE ? ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def)) + " " : ChatColor.translateAlternateColorCodes('&', LANG.getString(this.path, def)); + } + + /** + * Get the default value of the path. + * + * @return The default value of the path. + */ + public String getDefault() { + return this.def; + } + + /** + * Get the path to the string. + * + * @return The path to the string. + */ + public String getPath() { + return this.path; + } +} \ No newline at end of file diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml new file mode 100644 index 00000000..cd982c25 --- /dev/null +++ b/src/main/resources/lang.yml @@ -0,0 +1,11 @@ +# Lang file for FactionsUUID by Puzl Inc. +# Use & for color codes. +# Made with love <3 + +title: "&bFactions &0|&r" +wilderness: "&2Wilderness" +wilderness-description: " " +warzone: "&4Warzone" +warzone-description: "Not the safest place to be." +safezone: "&6Safezone" +safezone-description: "Free from pvp and monsters." \ No newline at end of file