From bee15556c9d38ae5a393eeab5c97006155042308 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 23 Mar 2011 12:00:38 +0100 Subject: [PATCH] Some case insensitivity fixes --- src/com/bukkit/mcteam/factions/Board.java | 4 +- src/com/bukkit/mcteam/factions/Conf.java | 26 +++++++- src/com/bukkit/mcteam/factions/FLocation.java | 12 +++- src/com/bukkit/mcteam/factions/FPlayer.java | 61 +++++++++---------- src/com/bukkit/mcteam/factions/Faction.java | 48 ++++++++------- src/com/bukkit/mcteam/factions/Factions.java | 3 +- .../listeners/FactionsBlockListener.java | 22 ++----- .../listeners/FactionsPlayerListener.java | 18 ++---- 8 files changed, 105 insertions(+), 89 deletions(-) diff --git a/src/com/bukkit/mcteam/factions/Board.java b/src/com/bukkit/mcteam/factions/Board.java index eeea067a..addd9b4b 100644 --- a/src/com/bukkit/mcteam/factions/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -16,7 +16,7 @@ import com.bukkit.mcteam.gson.reflect.TypeToken; import com.bukkit.mcteam.util.AsciiCompass; import com.bukkit.mcteam.util.DiscUtil; -//import com.bukkit.mcteam.factions.util.*; +// TODO rework to use single layer hash map and convert from and to the formay while saving and loading!! public class Board { protected static transient File file = new File(Factions.instance.getDataFolder(), "board.json"); @@ -182,6 +182,8 @@ public class Board { } public static boolean load() { + Factions.log("Loading board from disk"); + if ( ! file.exists()) { Factions.log("No board to load from disk. Creating new file."); save(); diff --git a/src/com/bukkit/mcteam/factions/Conf.java b/src/com/bukkit/mcteam/factions/Conf.java index 77fe2a13..d67f479d 100644 --- a/src/com/bukkit/mcteam/factions/Conf.java +++ b/src/com/bukkit/mcteam/factions/Conf.java @@ -49,7 +49,9 @@ public class Conf { public static boolean territoryBlockCreepers = false; public static boolean territoryBlockFireballs = false; - public static List territoryProtectedMaterials = new ArrayList(); + public static Set territoryProtectedMaterials = new HashSet(); + public static Set territoryDenyUseageMaterials = new HashSet(); + public static transient Set instaDestroyMaterials = new HashSet(); // This one is not really configuration therefore transient public static boolean allowNoSlashCommand = true; @@ -60,6 +62,25 @@ public class Conf { territoryProtectedMaterials.add(Material.DISPENSER); territoryProtectedMaterials.add(Material.CHEST); territoryProtectedMaterials.add(Material.FURNACE); + + territoryDenyUseageMaterials.add(Material.REDSTONE); + territoryDenyUseageMaterials.add(Material.SIGN); + territoryDenyUseageMaterials.add(Material.FLINT_AND_STEEL); + territoryDenyUseageMaterials.add(Material.BED); + territoryDenyUseageMaterials.add(Material.BUCKET); + territoryDenyUseageMaterials.add(Material.WATER_BUCKET); + territoryDenyUseageMaterials.add(Material.DIODE); + territoryDenyUseageMaterials.add(Material.SUGAR_CANE); + + instaDestroyMaterials.add(Material.SAPLING); + instaDestroyMaterials.add(Material.TORCH); + instaDestroyMaterials.add(Material.REDSTONE_WIRE); + instaDestroyMaterials.add(Material.CROPS); + instaDestroyMaterials.add(Material.REDSTONE_TORCH_OFF); + instaDestroyMaterials.add(Material.REDSTONE_TORCH_ON); + instaDestroyMaterials.add(Material.SUGAR_CANE_BLOCK); + instaDestroyMaterials.add(Material.DIODE_BLOCK_OFF); + instaDestroyMaterials.add(Material.DIODE_BLOCK_ON); } // -------------------------------------------- // @@ -80,6 +101,8 @@ public class Conf { } public static boolean load() { + Factions.log("Loading conf from disk"); + if ( ! file.exists()) { Factions.log("No conf to load from disk. Creating new file."); save(); @@ -97,3 +120,4 @@ public class Conf { return true; } } + diff --git a/src/com/bukkit/mcteam/factions/FLocation.java b/src/com/bukkit/mcteam/factions/FLocation.java index ab807bcf..d046ffd4 100644 --- a/src/com/bukkit/mcteam/factions/FLocation.java +++ b/src/com/bukkit/mcteam/factions/FLocation.java @@ -86,6 +86,14 @@ public class FLocation { // Comparison //----------------------------------------------// + public int hashCode() { + int hash = 3; + hash = 19 * hash + (this.worldName != null ? this.worldName.hashCode() : 0); + hash = 19 * hash + this.x; + hash = 19 * hash + this.z; + return hash; + }; + @Override public boolean equals(Object obj) { if (obj == this) @@ -93,7 +101,7 @@ public class FLocation { if (!(obj instanceof FLocation)) return false; - FLocation o = (FLocation) obj; - return this.x == o.x && this.z == o.z && this.worldName.equals(o.worldName); + FLocation that = (FLocation) obj; + return this.x == that.x && this.z == that.z && ( this.worldName==null ? that.worldName==null : this.worldName.equals(that.worldName) ); } } \ No newline at end of file diff --git a/src/com/bukkit/mcteam/factions/FPlayer.java b/src/com/bukkit/mcteam/factions/FPlayer.java index fd57b16b..1b1743ce 100644 --- a/src/com/bukkit/mcteam/factions/FPlayer.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -19,8 +19,7 @@ import com.bukkit.mcteam.util.DiscUtil; * Logged out players may or may not have an FPlayer instance. They will always have one if they are part of a faction. * This is because only players with a faction are saved to disk (in order to not waste disk space). * - * The FPlayer is linked to a minecraft player using the player name in lowercase form. - * Lowercase is enforced while loading from disk TODO + * The FPlayer is linked to a minecraft player using the player name. * * The same instance is always returned for the same player. * This means you can use the == operator. No .equals method necessary. @@ -32,7 +31,7 @@ public class FPlayer { // Fields // -------------------------------------------- // - private static transient Map instances = new HashMap(); + private static transient TreeMap instances = new TreeMap(String.CASE_INSENSITIVE_ORDER); private static transient File file = new File(Factions.instance.getDataFolder(), "players.json"); private transient String playerName; @@ -75,8 +74,6 @@ public class FPlayer { return Factions.instance.getServer().getPlayer(playerName); } - - // TODO lowercase vs mixedcase for logged in chars... public String getPlayerName() { return this.playerName; } @@ -202,15 +199,15 @@ public class FPlayer { public String getNameAndTitle(Faction faction) { return this.getRelationColor(faction)+this.getNameAndTitle(); } - public String getNameAndTitle(FPlayer follower) { - return this.getRelationColor(follower)+this.getNameAndTitle(); + public String getNameAndTitle(FPlayer fplayer) { + return this.getRelationColor(fplayer)+this.getNameAndTitle(); } public String getNameAndTag(Faction faction) { return this.getRelationColor(faction)+this.getNameAndTag(); } - public String getNameAndTag(FPlayer follower) { - return this.getRelationColor(follower)+this.getNameAndTag(); + public String getNameAndTag(FPlayer fplayer) { + return this.getRelationColor(fplayer)+this.getNameAndTag(); } public String getNameAndRelevant(Faction faction) { @@ -225,8 +222,8 @@ public class FPlayer { // For non members we show tag return rel.getColor() + this.getNameAndTag(); } - public String getNameAndRelevant(FPlayer follower) { - return getNameAndRelevant(follower.getFaction()); + public String getNameAndRelevant(FPlayer fplayer) { + return getNameAndRelevant(fplayer.getFaction()); } // Chat Tag: @@ -248,12 +245,12 @@ public class FPlayer { return this.getRelation(faction).getColor()+getChatTag(); } - public String getChatTag(FPlayer follower) { + public String getChatTag(FPlayer fplayer) { if ( ! this.hasFaction()) { return ""; } - return this.getRelation(follower).getColor()+getChatTag(); + return this.getRelation(fplayer).getColor()+getChatTag(); } // ------------------------------- @@ -264,16 +261,16 @@ public class FPlayer { return faction.getRelation(this); } - public Relation getRelation(FPlayer follower) { - return this.getFaction().getRelation(follower); + public Relation getRelation(FPlayer fplayer) { + return this.getFaction().getRelation(fplayer); } public ChatColor getRelationColor(Faction faction) { return faction.getRelationColor(this); } - public ChatColor getRelationColor(FPlayer follower) { - return this.getRelation(follower).getColor(); + public ChatColor getRelationColor(FPlayer fplayer) { + return this.getRelation(fplayer).getColor(); } @@ -402,8 +399,13 @@ public class FPlayer { // -------------------------------------------- // // Get and search // -------------------------------------------- // - public static FPlayer get(String playerName) { - playerName = playerName.toLowerCase(); + + // You should use this one to be sure you do not spell the player name wrong. + public static FPlayer get(Player player) { + return get(player.getName()); + } + + private static FPlayer get(String playerName) { if (instances.containsKey(playerName)) { return instances.get(playerName); } @@ -415,11 +417,6 @@ public class FPlayer { return vplayer; } - // You should use this one to be sure you do not spell the player name wrong. - public static FPlayer get(Player player) { - return get(player.getName()); - } - public static Set getAllOnline() { Set fplayers = new HashSet(); for (Player player : Factions.instance.getServer().getOnlinePlayers()) { @@ -452,16 +449,16 @@ public class FPlayer { public static boolean save() { //Factions.log("Saving players to disk"); - // We only wan't to save the vplayers with non default values - Map vplayersToSave = new HashMap(); + // We only wan't to save the players with non default values + Map playersToSave = new HashMap(); for (Entry entry : instances.entrySet()) { if (entry.getValue().shouldBeSaved()) { - vplayersToSave.put(entry.getKey(), entry.getValue()); + playersToSave.put(entry.getKey(), entry.getValue()); } } try { - DiscUtil.write(file, Factions.gson.toJson(vplayersToSave)); + DiscUtil.write(file, Factions.gson.toJson(playersToSave)); } catch (IOException e) { Factions.log("Failed to save the players to disk."); e.printStackTrace(); @@ -471,6 +468,7 @@ public class FPlayer { } public static boolean load() { + Factions.log("Loading players from disk"); if ( ! file.exists()) { Factions.log("No players to load from disk. Creating new file."); save(); @@ -480,11 +478,8 @@ public class FPlayer { try { Type type = new TypeToken>(){}.getType(); Map instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); - - instances = new HashMap(); - for (Entry instanceFromFile : instancesFromFile.entrySet()) { - instances.put(instanceFromFile.getKey().toLowerCase(), instanceFromFile.getValue()); - } + instances.clear(); + instances.putAll(instancesFromFile); } catch (IOException e) { e.printStackTrace(); return false; diff --git a/src/com/bukkit/mcteam/factions/Faction.java b/src/com/bukkit/mcteam/factions/Faction.java index f74f7d26..7520c44f 100644 --- a/src/com/bukkit/mcteam/factions/Faction.java +++ b/src/com/bukkit/mcteam/factions/Faction.java @@ -28,7 +28,7 @@ public class Faction { private transient int id; private Map relationWish; - private Set invites; // Where string is a follower id (lower case name) + private Set invites; // Where string is a lowercase player name private boolean open; private String tag; private String description; @@ -70,8 +70,8 @@ public class Faction { public String getTag(Faction otherFaction) { return this.getTag(otherFaction.getRelationColor(this).toString()); } - public String getTag(FPlayer otherFollower) { - return this.getTag(otherFollower.getRelationColor(this).toString()); + public String getTag(FPlayer otherFplayer) { + return this.getTag(otherFplayer.getRelationColor(this).toString()); } public void setTag(String str) { if (Conf.factionTagForceUpperCase) { @@ -89,19 +89,19 @@ public class Faction { } // ------------------------------- - // Invites + // Invites - uses lowercase name // ------------------------------- public void invite(FPlayer fplayer) { - this.invites.add(fplayer.getName()); //TODO Lowercase paradigm shit.... + this.invites.add(fplayer.getName().toLowerCase()); } public void deinvite(FPlayer fplayer) { - this.invites.remove(fplayer.getName()); //TODO Lowercase paradigm shit.... + this.invites.remove(fplayer.getName().toLowerCase()); } public boolean isInvited(FPlayer fplayer) { - return this.invites.contains(fplayer.getName()); //TODO Lowercase paradigm shit.... + return this.invites.contains(fplayer.getName().toLowerCase()); } // ------------------------------- @@ -136,8 +136,8 @@ public class Faction { return this.getRelationWish(otherFaction); } - public Relation getRelation(FPlayer follower) { - return getRelation(follower.getFaction()); + public Relation getRelation(FPlayer fplayer) { + return getRelation(fplayer.getFaction()); } //----------------------------------------------// @@ -145,16 +145,16 @@ public class Faction { //----------------------------------------------// public double getPower() { double ret = 0; - for (FPlayer follower : this.getFPlayers()) { - ret += follower.getPower(); + for (FPlayer fplayer : this.getFPlayers()) { + ret += fplayer.getPower(); } return ret; } public double getPowerMax() { double ret = 0; - for (FPlayer follower : this.getFPlayers()) { - ret += follower.getPowerMax(); + for (FPlayer fplayer : this.getFPlayers()) { + ret += fplayer.getPowerMax(); } return ret; } @@ -176,14 +176,14 @@ public class Faction { } // ------------------------------- - // Followers + // Fplayers // ------------------------------- public ArrayList getFPlayers() { ArrayList ret = new ArrayList(); - for (FPlayer follower : FPlayer.getAll()) { - if (follower.getFaction() == this) { - ret.add(follower); + for (FPlayer fplayer : FPlayer.getAll()) { + if (fplayer.getFaction() == this) { + ret.add(fplayer); } } return ret; @@ -214,8 +214,8 @@ public class Faction { public ArrayList getOnlinePlayers() { ArrayList ret = new ArrayList(); for (Player player: Factions.instance.getServer().getOnlinePlayers()) { - FPlayer follower = FPlayer.get(player); - if (follower.getFaction() == this) { + FPlayer fplayer = FPlayer.get(player); + if (fplayer.getFaction() == this) { ret.add(player); } } @@ -287,8 +287,8 @@ public class Faction { return this.getRelation(otherFaction).getColor(); } - public ChatColor getRelationColor(FPlayer follower) { - return this.getRelation(follower).getColor(); + public ChatColor getRelationColor(FPlayer fplayer) { + return this.getRelation(fplayer).getColor(); } @@ -312,6 +312,8 @@ public class Faction { } public static boolean load() { + Factions.log("Loading factions from disk"); + if ( ! file.exists()) { Factions.log("No factions to load from disk. Creating new file."); save(); @@ -319,7 +321,9 @@ public class Faction { try { Type type = new TypeToken>(){}.getType(); - instances = Factions.gson.fromJson(DiscUtil.read(file), type); + Map instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); + instances.clear(); + instances.putAll(instancesFromFile); } catch (IOException e) { e.printStackTrace(); return false; diff --git a/src/com/bukkit/mcteam/factions/Factions.java b/src/com/bukkit/mcteam/factions/Factions.java index c3b1943a..ee1e5d10 100644 --- a/src/com/bukkit/mcteam/factions/Factions.java +++ b/src/com/bukkit/mcteam/factions/Factions.java @@ -181,7 +181,8 @@ public class Factions extends JavaPlugin { if (test != null) { helpPlugin = ((Help) test); Factions.log("Found and will use plugin "+helpPlugin.getDescription().getFullName()); - helpPlugin.registerCommand(this.getBaseCommand()+" help *[page]", "Factions plugin help.", helpPlugin, true); + helpPlugin.registerCommand(this.getBaseCommand()+" help *[page]", "Factions plugin help.", this, false); + helpPlugin.registerCommand("help factions", "instead use: /f help", helpPlugin, true); } } diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java index 82138375..9d22207f 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsBlockListener.java @@ -1,10 +1,5 @@ package com.bukkit.mcteam.factions.listeners; -import java.util.Arrays; -import java.util.Set; -import java.util.HashSet; - - import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockDamageLevel; @@ -34,22 +29,15 @@ public class FactionsBlockListener extends BlockListener { } } - //special cases, check for destruction of: torch, redstone torch (on & off), repeater (on & off), redstonewire, sapling, crops, sugar cane - private static Set specialBlocks = new HashSet(Arrays.asList( - new Integer[] {50, 75, 76, 93, 94, 55, 6, 59, 83} - )); - @Override public void onBlockDamage(BlockDamageEvent event) { - // debug - //event.getPlayer().sendMessage("Block damaged: " + event.getBlock().getTypeId() + " (" + event.getBlock().getType().toString() + ")"); - if (event.isCancelled()) { - return; // Alright. lets listen to that. + return; } - boolean badBlock = event.getDamageLevel() == BlockDamageLevel.STOPPED || specialBlocks.contains(new Integer(event.getBlock().getTypeId())); - if (badBlock && ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "destroy")) { + boolean blockDestroyed = event.getDamageLevel() == BlockDamageLevel.STOPPED || Conf.instaDestroyMaterials.contains(event.getBlock().getType()); + + if (blockDestroyed && ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "destroy")) { event.setCancelled(true); } } @@ -57,7 +45,7 @@ public class FactionsBlockListener extends BlockListener { public boolean playerCanBuildDestroyBlock(Player player, Block block, String action) { Faction otherFaction = Board.getFactionAt(new FLocation(block)); - if (otherFaction == null || otherFaction.getId() == 0) { + if (otherFaction.getId() == 0) { return true; // This is no faction territory. You may build or break stuff here. } diff --git a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java index 7f7ada68..b639aa88 100644 --- a/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java +++ b/src/com/bukkit/mcteam/factions/listeners/FactionsPlayerListener.java @@ -1,11 +1,9 @@ package com.bukkit.mcteam.factions.listeners; -import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.logging.Logger; +import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -96,7 +94,7 @@ public class FactionsPlayerListener extends PlayerListener{ // Update the lastLoginTime for this fplayer me.setLastLoginTime(System.currentTimeMillis()); - // Run the member auto kick routine. Twice to getToTheAdmins... + // Run the member auto kick routine. Twice to get to the admins... FPlayer.autoLeaveOnInactivityRoutine(); FPlayer.autoLeaveOnInactivityRoutine(); } @@ -146,7 +144,7 @@ public class FactionsPlayerListener extends PlayerListener{ return; // right-clicked on air, not a block; no worries then } - if ( ! this.playerCanUseItemHere(event.getPlayer(), event.getBlockClicked(), event.getItem().getTypeId())) { + if ( ! this.playerCanUseItemHere(event.getPlayer(), event.getBlockClicked(), event.getMaterial())) { event.setCancelled(true); return; } @@ -154,19 +152,15 @@ public class FactionsPlayerListener extends PlayerListener{ } //currently checking placement/use of: redstone, sign, flint&steel, beds (not currently detected by Bukkit), buckets (empty, water, lava), repeater (not currently detected by Bukkit) - private static Set badItems = new HashSet(Arrays.asList( - new Integer[] {331, 323, 259, 355, 325, 326, 327, 356} - )); + public boolean playerCanUseItemHere(Player player, Block block, Material material) { - public boolean playerCanUseItemHere(Player player, Block block, int itemId) { - - if ( ! badItems.contains(new Integer(itemId))) { + if ( ! Conf.territoryDenyUseageMaterials.contains(material)) { return true; // Item isn't one we're preventing. } Faction otherFaction = Board.getFactionAt(new FLocation(block)); - if (otherFaction == null || otherFaction.getId() == 0) { + if (otherFaction.getId() == 0) { return true; // This is not faction territory. Use whatever you like here. }