Fixed Tool objects being shared by all players

This commit is contained in:
bm01 2013-03-04 00:01:53 +01:00
parent 12b548bf46
commit e80f60ccee
3 changed files with 22 additions and 19 deletions

View File

@ -13,6 +13,8 @@ import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.mods.CustomTool;
import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.skills.Tool;
import com.gmail.nossr50.datatypes.skills.ToolType;
import com.gmail.nossr50.datatypes.spout.huds.McMMOHud;
import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent;
import com.gmail.nossr50.party.PartyManager;
@ -77,6 +79,19 @@ public class McMMOPlayer {
profile = new PlayerProfile(playerName, true);
party = PartyManager.getPlayerParty(playerName);
Map<ToolType, Tool> tools = new HashMap<ToolType, Tool>();
// Build Tool objects for the current player, a tool can be used by multiple skills
for (SkillType skillType : SkillType.values()) {
ToolType toolType = skillType.getToolType();
if (tools.containsKey(toolType)) {
continue;
}
tools.put(toolType, new Tool());
}
/*
* I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually),
* but I actually have no idea about the performance impact, if there is any.
@ -88,6 +103,9 @@ public class McMMOPlayer {
// TODO: The null check is needed only because currently some SkillType doesn't have a valid skillManagerClass
if (skillManagerClass != null) {
SkillManager skillManager = skillManagerClass.getConstructor(McMMOPlayer.class).newInstance(this);
skillManager.setTool(tools.get(skillType));
skillManagers.put(skillType, skillManagerClass.getConstructor(McMMOPlayer.class).newInstance(this));
}
}

View File

@ -1,19 +1,11 @@
package com.gmail.nossr50.datatypes.skills;
import java.util.HashMap;
import java.util.Map;
import com.gmail.nossr50.util.Misc;
public class Tool {
private static Map<ToolType, Tool> tools = new HashMap<ToolType, Tool>();
private boolean preparationMode = true;
private long preparationATS;
private Tool(ToolType toolType) {
tools.put(toolType, this);
}
public boolean getPreparationMode() {
return preparationMode;
}
@ -31,14 +23,4 @@ public class Tool {
preparationATS = startTime;
}
public static Tool getTool(ToolType toolType) {
Tool tool = tools.get(toolType);
if (tool == null) {
tool = new Tool(toolType);
}
return tool;
}
}

View File

@ -20,7 +20,6 @@ public abstract class SkillManager {
this.mcMMOPlayer = mcMMOPlayer;
this.activationChance = PerksUtils.handleLuckyPerks(mcMMOPlayer.getPlayer(), skill);
this.skill = skill;
this.tool = Tool.getTool(skill.getToolType());
}
public McMMOPlayer getMcMMOPlayer() {
@ -66,4 +65,8 @@ public abstract class SkillManager {
public Tool getTool() {
return tool;
}
public void setTool(Tool tool) {
this.tool = tool;
}
}