Add TL file. Use CommandExecutor to properly handle commands instead of using the preprocess listener and breaking other plugins.

This commit is contained in:
drtshock 2014-07-01 13:19:36 -05:00
parent 753c32785d
commit cd65375bd9
8 changed files with 195 additions and 32 deletions

View File

@ -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;
}

View File

@ -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<Faction> {
P p = P.p;
private Factions() {
super
(
Faction.class,
new CopyOnWriteArrayList<Faction>(),
new ConcurrentHashMap<String, Faction>(),
new File(P.p.getDataFolder(), "factions.json"),
P.p.gson
);
super(Faction.class, new CopyOnWriteArrayList<Faction>(), new ConcurrentHashMap<String, Faction>(), new File(P.p.getDataFolder(), "factions.json"), P.p.gson);
}
@Override
@ -44,32 +38,32 @@ public class Factions extends EntityCollection<Faction> {
// 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

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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())

View File

@ -29,10 +29,9 @@ public abstract class PlayerEntityCollection<E extends Entity> 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<E> getOnline() {
Set<E> entities = new HashSet<E>();

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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."