Some case insensitivity fixes

This commit is contained in:
Olof Larsson 2011-03-23 12:00:38 +01:00
parent 8bab4540cd
commit bee15556c9
8 changed files with 105 additions and 89 deletions

View File

@ -16,7 +16,7 @@ import com.bukkit.mcteam.gson.reflect.TypeToken;
import com.bukkit.mcteam.util.AsciiCompass; import com.bukkit.mcteam.util.AsciiCompass;
import com.bukkit.mcteam.util.DiscUtil; 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 { public class Board {
protected static transient File file = new File(Factions.instance.getDataFolder(), "board.json"); protected static transient File file = new File(Factions.instance.getDataFolder(), "board.json");
@ -182,6 +182,8 @@ public class Board {
} }
public static boolean load() { public static boolean load() {
Factions.log("Loading board from disk");
if ( ! file.exists()) { if ( ! file.exists()) {
Factions.log("No board to load from disk. Creating new file."); Factions.log("No board to load from disk. Creating new file.");
save(); save();

View File

@ -49,7 +49,9 @@ public class Conf {
public static boolean territoryBlockCreepers = false; public static boolean territoryBlockCreepers = false;
public static boolean territoryBlockFireballs = false; public static boolean territoryBlockFireballs = false;
public static List<Material> territoryProtectedMaterials = new ArrayList<Material>(); public static Set<Material> territoryProtectedMaterials = new HashSet<Material>();
public static Set<Material> territoryDenyUseageMaterials = new HashSet<Material>();
public static transient Set<Material> instaDestroyMaterials = new HashSet<Material>(); // This one is not really configuration therefore transient
public static boolean allowNoSlashCommand = true; public static boolean allowNoSlashCommand = true;
@ -60,6 +62,25 @@ public class Conf {
territoryProtectedMaterials.add(Material.DISPENSER); territoryProtectedMaterials.add(Material.DISPENSER);
territoryProtectedMaterials.add(Material.CHEST); territoryProtectedMaterials.add(Material.CHEST);
territoryProtectedMaterials.add(Material.FURNACE); 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() { public static boolean load() {
Factions.log("Loading conf from disk");
if ( ! file.exists()) { if ( ! file.exists()) {
Factions.log("No conf to load from disk. Creating new file."); Factions.log("No conf to load from disk. Creating new file.");
save(); save();
@ -97,3 +120,4 @@ public class Conf {
return true; return true;
} }
} }

View File

@ -86,6 +86,14 @@ public class FLocation {
// Comparison // 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 @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == this) if (obj == this)
@ -93,7 +101,7 @@ public class FLocation {
if (!(obj instanceof FLocation)) if (!(obj instanceof FLocation))
return false; return false;
FLocation o = (FLocation) obj; FLocation that = (FLocation) obj;
return this.x == o.x && this.z == o.z && this.worldName.equals(o.worldName); return this.x == that.x && this.z == that.z && ( this.worldName==null ? that.worldName==null : this.worldName.equals(that.worldName) );
} }
} }

View File

@ -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. * 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). * 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. * The FPlayer is linked to a minecraft player using the player name.
* Lowercase is enforced while loading from disk TODO
* *
* The same instance is always returned for the same player. * The same instance is always returned for the same player.
* This means you can use the == operator. No .equals method necessary. * This means you can use the == operator. No .equals method necessary.
@ -32,7 +31,7 @@ public class FPlayer {
// Fields // Fields
// -------------------------------------------- // // -------------------------------------------- //
private static transient Map<String, FPlayer> instances = new HashMap<String, FPlayer>(); private static transient TreeMap<String, FPlayer> instances = new TreeMap<String, FPlayer>(String.CASE_INSENSITIVE_ORDER);
private static transient File file = new File(Factions.instance.getDataFolder(), "players.json"); private static transient File file = new File(Factions.instance.getDataFolder(), "players.json");
private transient String playerName; private transient String playerName;
@ -75,8 +74,6 @@ public class FPlayer {
return Factions.instance.getServer().getPlayer(playerName); return Factions.instance.getServer().getPlayer(playerName);
} }
// TODO lowercase vs mixedcase for logged in chars...
public String getPlayerName() { public String getPlayerName() {
return this.playerName; return this.playerName;
} }
@ -202,15 +199,15 @@ public class FPlayer {
public String getNameAndTitle(Faction faction) { public String getNameAndTitle(Faction faction) {
return this.getRelationColor(faction)+this.getNameAndTitle(); return this.getRelationColor(faction)+this.getNameAndTitle();
} }
public String getNameAndTitle(FPlayer follower) { public String getNameAndTitle(FPlayer fplayer) {
return this.getRelationColor(follower)+this.getNameAndTitle(); return this.getRelationColor(fplayer)+this.getNameAndTitle();
} }
public String getNameAndTag(Faction faction) { public String getNameAndTag(Faction faction) {
return this.getRelationColor(faction)+this.getNameAndTag(); return this.getRelationColor(faction)+this.getNameAndTag();
} }
public String getNameAndTag(FPlayer follower) { public String getNameAndTag(FPlayer fplayer) {
return this.getRelationColor(follower)+this.getNameAndTag(); return this.getRelationColor(fplayer)+this.getNameAndTag();
} }
public String getNameAndRelevant(Faction faction) { public String getNameAndRelevant(Faction faction) {
@ -225,8 +222,8 @@ public class FPlayer {
// For non members we show tag // For non members we show tag
return rel.getColor() + this.getNameAndTag(); return rel.getColor() + this.getNameAndTag();
} }
public String getNameAndRelevant(FPlayer follower) { public String getNameAndRelevant(FPlayer fplayer) {
return getNameAndRelevant(follower.getFaction()); return getNameAndRelevant(fplayer.getFaction());
} }
// Chat Tag: // Chat Tag:
@ -248,12 +245,12 @@ public class FPlayer {
return this.getRelation(faction).getColor()+getChatTag(); return this.getRelation(faction).getColor()+getChatTag();
} }
public String getChatTag(FPlayer follower) { public String getChatTag(FPlayer fplayer) {
if ( ! this.hasFaction()) { if ( ! this.hasFaction()) {
return ""; return "";
} }
return this.getRelation(follower).getColor()+getChatTag(); return this.getRelation(fplayer).getColor()+getChatTag();
} }
// ------------------------------- // -------------------------------
@ -264,16 +261,16 @@ public class FPlayer {
return faction.getRelation(this); return faction.getRelation(this);
} }
public Relation getRelation(FPlayer follower) { public Relation getRelation(FPlayer fplayer) {
return this.getFaction().getRelation(follower); return this.getFaction().getRelation(fplayer);
} }
public ChatColor getRelationColor(Faction faction) { public ChatColor getRelationColor(Faction faction) {
return faction.getRelationColor(this); return faction.getRelationColor(this);
} }
public ChatColor getRelationColor(FPlayer follower) { public ChatColor getRelationColor(FPlayer fplayer) {
return this.getRelation(follower).getColor(); return this.getRelation(fplayer).getColor();
} }
@ -402,8 +399,13 @@ public class FPlayer {
// -------------------------------------------- // // -------------------------------------------- //
// Get and search // 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)) { if (instances.containsKey(playerName)) {
return instances.get(playerName); return instances.get(playerName);
} }
@ -415,11 +417,6 @@ public class FPlayer {
return vplayer; 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<FPlayer> getAllOnline() { public static Set<FPlayer> getAllOnline() {
Set<FPlayer> fplayers = new HashSet<FPlayer>(); Set<FPlayer> fplayers = new HashSet<FPlayer>();
for (Player player : Factions.instance.getServer().getOnlinePlayers()) { for (Player player : Factions.instance.getServer().getOnlinePlayers()) {
@ -452,16 +449,16 @@ public class FPlayer {
public static boolean save() { public static boolean save() {
//Factions.log("Saving players to disk"); //Factions.log("Saving players to disk");
// We only wan't to save the vplayers with non default values // We only wan't to save the players with non default values
Map<String, FPlayer> vplayersToSave = new HashMap<String, FPlayer>(); Map<String, FPlayer> playersToSave = new HashMap<String, FPlayer>();
for (Entry<String, FPlayer> entry : instances.entrySet()) { for (Entry<String, FPlayer> entry : instances.entrySet()) {
if (entry.getValue().shouldBeSaved()) { if (entry.getValue().shouldBeSaved()) {
vplayersToSave.put(entry.getKey(), entry.getValue()); playersToSave.put(entry.getKey(), entry.getValue());
} }
} }
try { try {
DiscUtil.write(file, Factions.gson.toJson(vplayersToSave)); DiscUtil.write(file, Factions.gson.toJson(playersToSave));
} catch (IOException e) { } catch (IOException e) {
Factions.log("Failed to save the players to disk."); Factions.log("Failed to save the players to disk.");
e.printStackTrace(); e.printStackTrace();
@ -471,6 +468,7 @@ public class FPlayer {
} }
public static boolean load() { public static boolean load() {
Factions.log("Loading players from disk");
if ( ! file.exists()) { if ( ! file.exists()) {
Factions.log("No players to load from disk. Creating new file."); Factions.log("No players to load from disk. Creating new file.");
save(); save();
@ -480,11 +478,8 @@ public class FPlayer {
try { try {
Type type = new TypeToken<Map<String, FPlayer>>(){}.getType(); Type type = new TypeToken<Map<String, FPlayer>>(){}.getType();
Map<String, FPlayer> instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type); Map<String, FPlayer> instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type);
instances.clear();
instances = new HashMap<String, FPlayer>(); instances.putAll(instancesFromFile);
for (Entry<String, FPlayer> instanceFromFile : instancesFromFile.entrySet()) {
instances.put(instanceFromFile.getKey().toLowerCase(), instanceFromFile.getValue());
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;

View File

@ -28,7 +28,7 @@ public class Faction {
private transient int id; private transient int id;
private Map<Integer, Relation> relationWish; private Map<Integer, Relation> relationWish;
private Set<String> invites; // Where string is a follower id (lower case name) private Set<String> invites; // Where string is a lowercase player name
private boolean open; private boolean open;
private String tag; private String tag;
private String description; private String description;
@ -70,8 +70,8 @@ public class Faction {
public String getTag(Faction otherFaction) { public String getTag(Faction otherFaction) {
return this.getTag(otherFaction.getRelationColor(this).toString()); return this.getTag(otherFaction.getRelationColor(this).toString());
} }
public String getTag(FPlayer otherFollower) { public String getTag(FPlayer otherFplayer) {
return this.getTag(otherFollower.getRelationColor(this).toString()); return this.getTag(otherFplayer.getRelationColor(this).toString());
} }
public void setTag(String str) { public void setTag(String str) {
if (Conf.factionTagForceUpperCase) { if (Conf.factionTagForceUpperCase) {
@ -89,19 +89,19 @@ public class Faction {
} }
// ------------------------------- // -------------------------------
// Invites // Invites - uses lowercase name
// ------------------------------- // -------------------------------
public void invite(FPlayer fplayer) { 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) { 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) { 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); return this.getRelationWish(otherFaction);
} }
public Relation getRelation(FPlayer follower) { public Relation getRelation(FPlayer fplayer) {
return getRelation(follower.getFaction()); return getRelation(fplayer.getFaction());
} }
//----------------------------------------------// //----------------------------------------------//
@ -145,16 +145,16 @@ public class Faction {
//----------------------------------------------// //----------------------------------------------//
public double getPower() { public double getPower() {
double ret = 0; double ret = 0;
for (FPlayer follower : this.getFPlayers()) { for (FPlayer fplayer : this.getFPlayers()) {
ret += follower.getPower(); ret += fplayer.getPower();
} }
return ret; return ret;
} }
public double getPowerMax() { public double getPowerMax() {
double ret = 0; double ret = 0;
for (FPlayer follower : this.getFPlayers()) { for (FPlayer fplayer : this.getFPlayers()) {
ret += follower.getPowerMax(); ret += fplayer.getPowerMax();
} }
return ret; return ret;
} }
@ -176,14 +176,14 @@ public class Faction {
} }
// ------------------------------- // -------------------------------
// Followers // Fplayers
// ------------------------------- // -------------------------------
public ArrayList<FPlayer> getFPlayers() { public ArrayList<FPlayer> getFPlayers() {
ArrayList<FPlayer> ret = new ArrayList<FPlayer>(); ArrayList<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer follower : FPlayer.getAll()) { for (FPlayer fplayer : FPlayer.getAll()) {
if (follower.getFaction() == this) { if (fplayer.getFaction() == this) {
ret.add(follower); ret.add(fplayer);
} }
} }
return ret; return ret;
@ -214,8 +214,8 @@ public class Faction {
public ArrayList<Player> getOnlinePlayers() { public ArrayList<Player> getOnlinePlayers() {
ArrayList<Player> ret = new ArrayList<Player>(); ArrayList<Player> ret = new ArrayList<Player>();
for (Player player: Factions.instance.getServer().getOnlinePlayers()) { for (Player player: Factions.instance.getServer().getOnlinePlayers()) {
FPlayer follower = FPlayer.get(player); FPlayer fplayer = FPlayer.get(player);
if (follower.getFaction() == this) { if (fplayer.getFaction() == this) {
ret.add(player); ret.add(player);
} }
} }
@ -287,8 +287,8 @@ public class Faction {
return this.getRelation(otherFaction).getColor(); return this.getRelation(otherFaction).getColor();
} }
public ChatColor getRelationColor(FPlayer follower) { public ChatColor getRelationColor(FPlayer fplayer) {
return this.getRelation(follower).getColor(); return this.getRelation(fplayer).getColor();
} }
@ -312,6 +312,8 @@ public class Faction {
} }
public static boolean load() { public static boolean load() {
Factions.log("Loading factions from disk");
if ( ! file.exists()) { if ( ! file.exists()) {
Factions.log("No factions to load from disk. Creating new file."); Factions.log("No factions to load from disk. Creating new file.");
save(); save();
@ -319,7 +321,9 @@ public class Faction {
try { try {
Type type = new TypeToken<Map<Integer, Faction>>(){}.getType(); Type type = new TypeToken<Map<Integer, Faction>>(){}.getType();
instances = Factions.gson.fromJson(DiscUtil.read(file), type); Map<Integer, Faction> instancesFromFile = Factions.gson.fromJson(DiscUtil.read(file), type);
instances.clear();
instances.putAll(instancesFromFile);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;

View File

@ -181,7 +181,8 @@ public class Factions extends JavaPlugin {
if (test != null) { if (test != null) {
helpPlugin = ((Help) test); helpPlugin = ((Help) test);
Factions.log("Found and will use plugin "+helpPlugin.getDescription().getFullName()); 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);
} }
} }

View File

@ -1,10 +1,5 @@
package com.bukkit.mcteam.factions.listeners; 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.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockDamageLevel; 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<Integer> specialBlocks = new HashSet<Integer>(Arrays.asList(
new Integer[] {50, 75, 76, 93, 94, 55, 6, 59, 83}
));
@Override @Override
public void onBlockDamage(BlockDamageEvent event) { public void onBlockDamage(BlockDamageEvent event) {
// debug
//event.getPlayer().sendMessage("Block damaged: " + event.getBlock().getTypeId() + " (" + event.getBlock().getType().toString() + ")");
if (event.isCancelled()) { if (event.isCancelled()) {
return; // Alright. lets listen to that. return;
} }
boolean badBlock = event.getDamageLevel() == BlockDamageLevel.STOPPED || specialBlocks.contains(new Integer(event.getBlock().getTypeId())); boolean blockDestroyed = event.getDamageLevel() == BlockDamageLevel.STOPPED || Conf.instaDestroyMaterials.contains(event.getBlock().getType());
if (badBlock && ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "destroy")) {
if (blockDestroyed && ! this.playerCanBuildDestroyBlock(event.getPlayer(), event.getBlock(), "destroy")) {
event.setCancelled(true); event.setCancelled(true);
} }
} }
@ -57,7 +45,7 @@ public class FactionsBlockListener extends BlockListener {
public boolean playerCanBuildDestroyBlock(Player player, Block block, String action) { public boolean playerCanBuildDestroyBlock(Player player, Block block, String action) {
Faction otherFaction = Board.getFactionAt(new FLocation(block)); 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. return true; // This is no faction territory. You may build or break stuff here.
} }

View File

@ -1,11 +1,9 @@
package com.bukkit.mcteam.factions.listeners; package com.bukkit.mcteam.factions.listeners;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -96,7 +94,7 @@ public class FactionsPlayerListener extends PlayerListener{
// Update the lastLoginTime for this fplayer // Update the lastLoginTime for this fplayer
me.setLastLoginTime(System.currentTimeMillis()); 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();
FPlayer.autoLeaveOnInactivityRoutine(); FPlayer.autoLeaveOnInactivityRoutine();
} }
@ -146,7 +144,7 @@ public class FactionsPlayerListener extends PlayerListener{
return; // right-clicked on air, not a block; no worries then 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); event.setCancelled(true);
return; 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) //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<Integer> badItems = new HashSet<Integer>(Arrays.asList( public boolean playerCanUseItemHere(Player player, Block block, Material material) {
new Integer[] {331, 323, 259, 355, 325, 326, 327, 356}
));
public boolean playerCanUseItemHere(Player player, Block block, int itemId) { if ( ! Conf.territoryDenyUseageMaterials.contains(material)) {
if ( ! badItems.contains(new Integer(itemId))) {
return true; // Item isn't one we're preventing. return true; // Item isn't one we're preventing.
} }
Faction otherFaction = Board.getFactionAt(new FLocation(block)); 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. return true; // This is not faction territory. Use whatever you like here.
} }