From de7134f0a4d953d9b7b5ae46a27ad58f6979b78f Mon Sep 17 00:00:00 2001 From: Steven Scott Date: Sun, 16 Sep 2012 16:11:19 -0700 Subject: [PATCH] [Version 0.7.8.0] - Updated languages to include sign text (Please update any languages you are able!) - Resolved NPE due to Bukkit bug with signs - Resolved issue regarding new getTargetBlock code throwing an exception - Languages now auto-update based on the .JAR version (New entries only, doesnt overwrite customization) - New command "/sg about", will list the author of the current language file if available - Language now has a fallback to English for missing lines (Its the only language I can personally update on release) - Added Spanish (Thanks Manuestaire) and Hungarian (Thanks HPoltergeist) --- README | 8 ++ src/net/TheDgtl/Stargate/LangLoader.java | 83 ++++++++++++++++---- src/net/TheDgtl/Stargate/Portal.java | 16 ++-- src/net/TheDgtl/Stargate/Stargate.java | 32 +++++--- src/net/TheDgtl/Stargate/StargateSign.java | 58 ++++++++++++++ src/net/TheDgtl/Stargate/resources/de.txt | 8 +- src/net/TheDgtl/Stargate/resources/en.txt | 7 +- src/net/TheDgtl/Stargate/resources/es.txt | 28 +++++++ src/net/TheDgtl/Stargate/resources/fr.txt | 8 +- src/net/TheDgtl/Stargate/resources/hu.txt | 28 +++++++ src/net/TheDgtl/Stargate/resources/it.txt | 6 ++ src/net/TheDgtl/Stargate/resources/nl.txt | 6 ++ src/net/TheDgtl/Stargate/resources/pt-br.txt | 6 ++ src/net/TheDgtl/Stargate/resources/ru.txt | 8 +- src/plugin.yml | 2 +- 15 files changed, 265 insertions(+), 39 deletions(-) create mode 100644 src/net/TheDgtl/Stargate/StargateSign.java create mode 100644 src/net/TheDgtl/Stargate/resources/es.txt create mode 100644 src/net/TheDgtl/Stargate/resources/hu.txt diff --git a/README b/README index 66a7b0b..3b700c7 100644 --- a/README +++ b/README @@ -212,6 +212,14 @@ Client randomly crashes on teleport. ============= Changes ============= +[Version 0.7.8.0] + - Updated languages to include sign text (Please update any languages you are able!) + - Resolved NPE due to Bukkit bug with signs + - Resolved issue regarding new getTargetBlock code throwing an exception + - Languages now auto-update based on the .JAR version (New entries only, doesn't overwrite customization) + - New command "/sg about", will list the author of the current language file if available + - Language now has a fallback to English for missing lines (It's the only language I can personally update on release) + - Added Spanish (Thanks Manuestaire) and Hungarian (Thanks HPoltergeist) [Version 0.7.7.5] - Resolve issue of right clicking introduced in 1.3.1/2 [Version 0.7.7.4] diff --git a/src/net/TheDgtl/Stargate/LangLoader.java b/src/net/TheDgtl/Stargate/LangLoader.java index 68ead06..b1c6d48 100644 --- a/src/net/TheDgtl/Stargate/LangLoader.java +++ b/src/net/TheDgtl/Stargate/LangLoader.java @@ -35,29 +35,39 @@ public class LangLoader { private String datFolder; private String lang; private HashMap strList; + private HashMap defList; public LangLoader(String datFolder, String lang) { this.lang = lang; this.datFolder = datFolder; - strList = new HashMap(); File tmp = new File(datFolder, lang + ".txt"); if (!tmp.exists()) { tmp.getParentFile().mkdirs(); - loadDefaults(); } + updateLanguage(lang); - load(); + strList = load(lang); + // We have a default hashMap used for when new text is added. + InputStream is = Stargate.class.getResourceAsStream("resources/" + lang + ".txt"); + if (is != null) { + defList = load("en", is); + } else { + defList = null; + Stargate.log.severe("[Stargate] Error loading backup language. There may be missing text ingame"); + } } public boolean reload() { - strList = new HashMap(); - load(); + // This extracts/updates the language as needed + updateLanguage(lang); + strList = load(lang); return true; } public String getString(String name) { String val = strList.get(name); + if (val == null && defList != null) val = defList.get(name); if (val == null) return ""; return val; } @@ -66,11 +76,16 @@ public class LangLoader { this.lang = lang; } - private void loadDefaults() { + // This function updates on-disk language files + // with missing lines from the in-JAR files + private void updateLanguage(String lang) { + // Load the current language file + HashMap curLang = load(lang); + InputStream is = Stargate.class.getResourceAsStream("resources/" + lang + ".txt"); if (is == null) return; - Stargate.log.info("[Stargate] Extracting initial language file -- " + lang + ".txt"); + boolean updated = false; FileOutputStream fos = null; try { // Input stuff @@ -83,9 +98,28 @@ public class LangLoader { BufferedWriter bw = new BufferedWriter(out); String line = br.readLine(); + boolean firstLine = true; while (line != null) { - bw.write(line); - bw.newLine(); + // Strip UTF BOM + if (firstLine) line = removeUTF8BOM(line); + firstLine = false; + // Split at first "=" + int eq = line.indexOf('='); + if (eq == -1) { + bw.newLine(); + line = br.readLine(); + continue; + } + String key = line.substring(0, eq); + + if (curLang == null || curLang.get(key) == null) { + bw.write(line); + bw.newLine(); + updated = true; + } else { + bw.write(key + "=" + curLang.get(key)); + bw.newLine(); + } line = br.readLine(); } bw.close(); @@ -96,13 +130,25 @@ public class LangLoader { try {fos.close();} catch (Exception ex) {} } } + if (updated) + Stargate.log.info("[Stargate] Your language file (" + lang + ".txt) has been updated"); } - private boolean load() { + private HashMap load(String lang) { + return load(lang, null); + } + + private HashMap load(String lang, InputStream is) { + HashMap strings = new HashMap(); FileInputStream fis = null; + InputStreamReader isr = null; try { - fis = new FileInputStream(datFolder + lang + ".txt"); - InputStreamReader isr = new InputStreamReader(fis, "UTF8"); + if (is == null) { + fis = new FileInputStream(datFolder + lang + ".txt"); + isr = new InputStreamReader(fis, "UTF8"); + } else { + isr = new InputStreamReader(is, "UTF8"); + } BufferedReader br = new BufferedReader(isr); String line = br.readLine(); boolean firstLine = true; @@ -118,24 +164,29 @@ public class LangLoader { } String key = line.substring(0, eq); String val = line.substring(eq + 1); - strList.put(key, val); + strings.put(key, val); line = br.readLine(); } } catch (Exception ex) { - return false; + return null; } finally { if (fis != null) { try {fis.close();} catch (Exception ex) {} } } - return true; + return strings; } public void debug() { Set keys = strList.keySet(); for (String key : keys) { - Stargate.debug("LangLoader::Debug", key + " => " + strList.get(key)); + Stargate.debug("LangLoader::Debug::strList", key + " => " + strList.get(key)); + } + if (defList == null) return; + keys = defList.keySet(); + for (String key : keys) { + Stargate.debug("LangLoader::Debug::defList", key + " => " + defList.get(key)); } } diff --git a/src/net/TheDgtl/Stargate/Portal.java b/src/net/TheDgtl/Stargate/Portal.java index 65e5570..489752f 100644 --- a/src/net/TheDgtl/Stargate/Portal.java +++ b/src/net/TheDgtl/Stargate/Portal.java @@ -23,7 +23,6 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.block.BlockState; import org.bukkit.block.Sign; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -670,28 +669,27 @@ public class Portal { } public final void drawSign() { - BlockState bs = null; - bs = id.getBlock().getState(); - if (!(bs instanceof Sign)) { + Material sMat = id.getBlock().getType(); + if (sMat != Material.SIGN && sMat != Material.WALL_SIGN && sMat != Material.SIGN_POST) { Stargate.log.warning("[Stargate] Sign block is not a Sign object"); Stargate.debug("Portal::drawSign", "Block: " + id.getBlock().getType() + " @ " + id.getBlock().getLocation()); return; } - Sign sign = (Sign)id.getBlock().getState(); + Sign sign = (Sign)new StargateSign(id.getBlock()); Stargate.setLine(sign, 0, "-" + name + "-"); int max = destinations.size() - 1; int done = 0; if (!isActive()) { - Stargate.setLine(sign, ++done, "Right click"); - Stargate.setLine(sign, ++done, "to use gate"); + Stargate.setLine(sign, ++done, Stargate.getString("signRightClick")); + Stargate.setLine(sign, ++done, Stargate.getString("signToUse")); if (!noNetwork) { Stargate.setLine(sign, ++done, "(" + network + ")"); } } else { if (isFixed()) { if (isRandom()) { - Stargate.setLine(sign, ++done, "> Random <"); + Stargate.setLine(sign, ++done, "> " + Stargate.getString("signRandom") + " <"); } else { Stargate.setLine(sign, ++done, ">" + destination + "<"); } @@ -702,7 +700,7 @@ public class Portal { } Portal dest = Portal.getByName(destination, network); if (dest == null && !isRandom()) { - Stargate.setLine(sign, ++done, "Disconnected"); + Stargate.setLine(sign, ++done, Stargate.getString("signDisconnected")); } else { Stargate.setLine(sign, ++done, ""); } diff --git a/src/net/TheDgtl/Stargate/Stargate.java b/src/net/TheDgtl/Stargate/Stargate.java index fdc7fc5..dd58552 100644 --- a/src/net/TheDgtl/Stargate/Stargate.java +++ b/src/net/TheDgtl/Stargate/Stargate.java @@ -144,9 +144,12 @@ public class Stargate extends JavaPlugin { pm.registerEvents(new sListener(), this); this.loadConfig(); + + // It is important to load languages here, as they are used during reloadGates() + lang = new LangLoader(langFolder, Stargate.langName); + this.migrate(); this.reloadGates(); - lang = new LangLoader(langFolder, Stargate.langName); // Check to see if iConomy/Permissions is loaded yet. permissions = (Permissions)checkPlugin("Permissions"); @@ -792,7 +795,12 @@ public class Stargate extends JavaPlugin { Player player = event.getPlayer(); Block block = null; if (event.isCancelled() && event.getAction() == Action.RIGHT_CLICK_AIR) { - block = player.getTargetBlock(null, 5); + try { + block = player.getTargetBlock(null, 5); + } catch (IllegalStateException ex) { + // We can safely ignore this exception, it only happens in void or max height + return; + } } else { block = event.getClickedBlock(); } @@ -1228,16 +1236,22 @@ public class Stargate extends JavaPlugin { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (sender instanceof Player) { - Player p = (Player)sender; - if (!hasPerm(p, "stargate.admin") && !hasPerm(p, "stargate.admin.reload")) { - sendMessage(sender, "Permission Denied"); - return true; - } - } String cmd = command.getName(); if (cmd.equalsIgnoreCase("sg")) { if (args.length != 1) return false; + if (args[0].equalsIgnoreCase("about")) { + sender.sendMessage("Stargate Plugin created by Drakia"); + if (!lang.getString("author").isEmpty()) + sender.sendMessage("Language created by " + lang.getString("author")); + return true; + } + if (sender instanceof Player) { + Player p = (Player)sender; + if (!hasPerm(p, "stargate.admin") && !hasPerm(p, "stargate.admin.reload")) { + sendMessage(sender, "Permission Denied"); + return true; + } + } if (args[0].equalsIgnoreCase("reload")) { // Deactivate portals for (Portal p : activeList) { diff --git a/src/net/TheDgtl/Stargate/StargateSign.java b/src/net/TheDgtl/Stargate/StargateSign.java new file mode 100644 index 0000000..bcf9bde --- /dev/null +++ b/src/net/TheDgtl/Stargate/StargateSign.java @@ -0,0 +1,58 @@ +package net.TheDgtl.Stargate; + +import net.minecraft.server.TileEntitySign; + +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.craftbukkit.CraftWorld; +import org.bukkit.craftbukkit.block.CraftBlockState; + +public class StargateSign extends CraftBlockState implements Sign { + private final TileEntitySign sign; + private final String[] lines; + + public StargateSign(final Block block) { + super(block); + + CraftWorld world = (CraftWorld) block.getWorld(); + sign = (TileEntitySign) world.getTileEntityAt(getX(), getY(), getZ()); + if (sign != null) { + lines = new String[sign.lines.length]; + System.arraycopy(sign.lines, 0, lines, 0, lines.length); + } else { + // Sadly, due to Minecraft having many issues with blocks, chunks + // and entities, we must assume a 4-line sign if the sign is null + lines = new String[4]; + } + } + + public String[] getLines() { + return lines; + } + + public String getLine(int index) throws IndexOutOfBoundsException { + return lines[index]; + } + + public void setLine(int index, String line) throws IndexOutOfBoundsException { + lines[index] = line; + } + + @Override + public boolean update(boolean force) { + boolean result = super.update(force) && (sign != null); + + if (result) { + for(int i = 0; i < sign.lines.length; i++) { + if(lines[i] != null) { + sign.lines[i] = lines[i]; + } else { + sign.lines[i] = ""; + } + } + sign.update(); + } + + return result; + } +} diff --git a/src/net/TheDgtl/Stargate/resources/de.txt b/src/net/TheDgtl/Stargate/resources/de.txt index 8e09ee2..4bcfbb4 100644 --- a/src/net/TheDgtl/Stargate/resources/de.txt +++ b/src/net/TheDgtl/Stargate/resources/de.txt @@ -1,3 +1,4 @@ +author=EduardBaer prefix=[Stargate] teleportMsg=Du wurdest Teleportiert. destroyMsg=Gate zerstört @@ -19,4 +20,9 @@ createNameLength=Name zu kurz oder zu lang. createExists=Ein Gate mit diesem Name existiert bereits. createFull=Dieses Netzwerk ist voll. createWorldDeny=Du hast keinen Zugriff auf diese Welt. -createConflict=Dieses Gate steht im Konflikt mit einem bereits existierenden. \ No newline at end of file +createConflict=Dieses Gate steht im Konflikt mit einem bereits existierenden. + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/en.txt b/src/net/TheDgtl/Stargate/resources/en.txt index 3eed42d..ed041f6 100644 --- a/src/net/TheDgtl/Stargate/resources/en.txt +++ b/src/net/TheDgtl/Stargate/resources/en.txt @@ -19,4 +19,9 @@ createNameLength=Name too short or too long. createExists=A gate by that name already exists createFull=This network is full createWorldDeny=You do not have access to that world -createConflict=Gate conflicts with existing gate \ No newline at end of file +createConflict=Gate conflicts with existing gate + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/es.txt b/src/net/TheDgtl/Stargate/resources/es.txt new file mode 100644 index 0000000..906b1ea --- /dev/null +++ b/src/net/TheDgtl/Stargate/resources/es.txt @@ -0,0 +1,28 @@ +author=Manuestaire +prefix=[Stargate] +teleportMsg=Teletransportado +destroyMsg=Portal Destruido +invalidMsg=Destino Inválido +blockMsg=Destino Bloqueado +destEmpty=La lista de destinos está vacía +denyMsg=Acceso denegado + +ecoDeduct=Pagaste %cost% +ecoRefund=Reembolsado %cost% +ecoObtain=Obtenido %cost% del Portal %portal% +ecoInFunds=No tienes suficiente dinero + +createMsg=Portal creado +createNetDeny=No tienes acceso a esta red +createGateDeny=No tienes acceso a este diseño de portal +createPersonal=Creando el portal en una red personal +createNameLength=Nombre demasiado largo o demasiado corto +createExists=Ya existe una puerta con este nombre +createFull=Esta red está llena +createWorldDeny=No tienes permisos para acceder a ese mundo +createConflict=El portal entra en conflicto con un portal ya existente + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/fr.txt b/src/net/TheDgtl/Stargate/resources/fr.txt index c5fc73e..8b945a4 100644 --- a/src/net/TheDgtl/Stargate/resources/fr.txt +++ b/src/net/TheDgtl/Stargate/resources/fr.txt @@ -1,4 +1,5 @@ -prefix=[Stargate] +author=Dauphin14 +prefix=[Stargate] teleportMsg=Téléportation Réussie. destroyMsg=Portail detruit. invalidMsg=Destination invalide. @@ -20,3 +21,8 @@ createExists=Il existe déjà un portail avec le même nom. createFull=Le reseau est plein. createWorldDeny=Vous n'avez pas accès à ce monde. createConflict=Ce portail entre en conflit avec un portail déjà existant. + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/hu.txt b/src/net/TheDgtl/Stargate/resources/hu.txt new file mode 100644 index 0000000..c1a7860 --- /dev/null +++ b/src/net/TheDgtl/Stargate/resources/hu.txt @@ -0,0 +1,28 @@ +author=HPoltergeist +prefix=[Stargate] +teleportMsg=Teleportálás +destroyMsg=Kapu törölve +invalidMsg=Nincs ilyen cél +blockMsg=Cél blokkolva +destEmpty=A cél lista üres +denyMsg=Hozzáférés megtagadva + +ecoDeduct=%cost% levonva +ecoRefund=%cost% visszatérítve +ecoObtain=%cost% bevétel a következÅ‘ kapuból: %portal% +ecoInFunds=Nincs elég pénzed + +createMsg=Kapu elkészült +createNetDeny=Nincs hozzáférésed ahhoz a hálózathoz +createGateDeny=Nincs hozzáférésed ahhoz a kapucsoporthoz +createPersonal=Kapu készítése magán hálózaton +createNameLength=A név túl rövid vagy túl hosszú +createExists=Már van ilyen nevű kapu +createFull=Ez a hálózat tele van +createWorldDeny=Nincs hozzáférésed ahhoz a világhoz +createConflict=Ez a kapu ütközik egy meglévö kapuval + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/it.txt b/src/net/TheDgtl/Stargate/resources/it.txt index ef852f0..1fa18e4 100644 --- a/src/net/TheDgtl/Stargate/resources/it.txt +++ b/src/net/TheDgtl/Stargate/resources/it.txt @@ -1,3 +1,4 @@ +author=fr33soul prefix=[Stargate] teleportMsg=Teletrasporto destroyMsg=Portale distrutto @@ -20,3 +21,8 @@ createExists=Questo nome gia' esiste createFull=Questa rete e' piena createWorldDeny=Non hai accesso a questo mondo createConflict=Il portale crea conflitti con uno gia' esistente + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/nl.txt b/src/net/TheDgtl/Stargate/resources/nl.txt index f466a2f..57f3db3 100644 --- a/src/net/TheDgtl/Stargate/resources/nl.txt +++ b/src/net/TheDgtl/Stargate/resources/nl.txt @@ -1,3 +1,4 @@ +author=Grovert11 prefix=[Stargate] teleportMsg=Je bent geteleporteerd. destroyMsg=Gate kapot gemaakt @@ -20,3 +21,8 @@ createExists=Er bestaat al een gate met die naam createFull=Dit netwerk is vol. createWorldDeny=Je mag niet in die wereld komen. createConflict=Gate maakt een conflict met een bestaande gate. + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/pt-br.txt b/src/net/TheDgtl/Stargate/resources/pt-br.txt index cadd61a..5022e25 100644 --- a/src/net/TheDgtl/Stargate/resources/pt-br.txt +++ b/src/net/TheDgtl/Stargate/resources/pt-br.txt @@ -1,3 +1,4 @@ +author=TheProbleemm prefix=[Stargate] teleportMsg=Voce foi teleportado. destroyMsg=Portal destruido. @@ -20,3 +21,8 @@ createExists=J createFull=Esta rede esta cheia. createWorldDeny=Voce nao tem acesso a esse mundo. createConflict=Portal em conflito com um portal ja existente. + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/net/TheDgtl/Stargate/resources/ru.txt b/src/net/TheDgtl/Stargate/resources/ru.txt index 9442ee1..6431462 100644 --- a/src/net/TheDgtl/Stargate/resources/ru.txt +++ b/src/net/TheDgtl/Stargate/resources/ru.txt @@ -1,4 +1,5 @@ -prefix=[Портал] +author=ckr@jk +prefix=[Портал] teleportMsg=Ð’Ñ‹ перемещены destroyMsg=Портал уничтожен invalidMsg=ÐедопуÑтимый пункт Ð½Ð°Ð·Ð½Ð°Ñ‡ÐµÐ½Ð¸Ñ @@ -20,3 +21,8 @@ createExists=Портал Ñ Ñ‚Ð°ÐºÐ¸Ð¼ именем уже ÑущеÑтвует createFull=Эта Ñеть порталов заполнена createWorldDeny=У Ð²Ð°Ñ Ð½ÐµÑ‚ доÑтупа к Ñтому миру createConflict=Портал конфликтует Ñ ÑƒÐ¶Ðµ ÑущеÑтвующим порталом + +signRightClick=Right click +signToUse=to use gate +signRandom=Random +signDisconnected=Disconnected \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index 8f9678c..84b0c7d 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: Stargate main: net.TheDgtl.Stargate.Stargate -version: 0.7.7.5 +version: 0.7.8.0 description: Stargate mod for Bukkit author: Drakia website: http://www.thedgtl.net