Jump to content

HashMap seems to not be recording data


Cephrus

Recommended Posts

So in my mod I have a method being called. This method takes the instance of EntityPlayer and also a class (which extends my other class) and adds it to a hashmap.

 

Then, in a method which is executed every tick, it determines whether the instance of the player is in the hashmap, and if it is, gets the class from the hashmap and executed the handlePlayer(EntityPlayer, Minecraft) from it. From there the handlePlayer method will handle certain custom capabilities which the player can do.

 

I have an in-game command which I execute that is supposed to call the method which puts the player and class into the hashmap. I put several println(')'s into several places into the code to determine where the code is faulting. I ultimately determined the code faults on the method that detects the player in the hashmap and executes handlePlayer. I determined the onTickInGame method is in fact working, it's just the getting of the class from the hashmap and the execution of the handlePlayer method which is not.

Link to comment
Share on other sites

Yea I probably did.

 

package tk.cephlab.supernatural;

import java.util.HashMap;
import java.util.logging.Level;

import org.lwjgl.input.Keyboard;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;

/**
* Custom Player Handler Class
* 
* This class is used for registering any custom handles for the player who
* currently is supernatural.
* 
* TL;DR: Pretty much it adds the features of a supernatural to the player when
* they are supernatural.
* 
* @author XCephrusX
*
*/
public class SupernaturalPlayerHandler
{
public HashMap<SupernaturalPlayerHandler, String> superMap = new HashMap<SupernaturalPlayerHandler, String>();
public HashMap<EntityPlayer, SupernaturalPlayerHandler> playerToSupernatural = new HashMap<EntityPlayer, SupernaturalPlayerHandler>();
public static SupernaturalPlayerHandler playerHandler;

public static final SupernaturalPlayerHandlerVampire supernaturalVampire = new SupernaturalPlayerHandlerVampire();

public SupernaturalPlayerHandler(String name)
{
	this.superMap.put(this, name);
	playerHandler = this;
}

/**
 * DO NOT USE THIS CONSTRUCTOR! IT IS ONLY USED FOR INITIALIZATION!
 */
public SupernaturalPlayerHandler()
{
	playerHandler = this;
}

public static SupernaturalPlayerHandler instance()
{
	if(playerHandler != null)
	{
		return playerHandler;
	}
	else return null;
}

/**
 * 
 * Converts specified player into supernatural handler specified.
 * Requires EntityPlayer class, and SupernaturalPlayerHandler.
 * 
 * @param player
 * @param playerHandler
 */
public void convertPlayerToSupernatural(EntityPlayer player, SupernaturalPlayerHandler playerHandler)
{
	player.addChatMessage("Conversion invoked");
	if(!this.playerToSupernatural.containsKey(player))
	{
		this.playerToSupernatural.put(player, playerHandler);
		player.addChatMessage("Player should be converted.");
	}
	else
	{
		System.out.println(Level.FINER + "Player is already supernatural, cannot add new supernatural tag to this player.");
	}
}

public boolean getIsPlayerSupernatural(EntityPlayer player)
{
	return this.playerToSupernatural.containsKey(player);
}

public boolean getIsPlayerSupernatural(EntityPlayer player, SupernaturalPlayerHandler handler)
{
	if(this.playerToSupernatural.containsKey(player))
	{
		if(this.playerToSupernatural.get(player) == handler)
		{
			return true;
		}
	}
	return false;
}

public void onTick(Minecraft minecraft)
{
	EntityPlayer player = minecraft.thePlayer;

	if(this.playerToSupernatural.get(player) !=null)
	{
		this.playerToSupernatural.get(player).handlePlayer(player, minecraft);
	}
}

public void handlePlayer(EntityPlayer player, Minecraft minecraft)
{
	if(Keyboard.isKeyDown(Keyboard.KEY_ADD) && this.getIsPlayerSupernatural(player))
	{
		player.addChatMessage(player.username + " is a " + this.superMap.get(this.playerToSupernatural.get(player)) + ".");
	}
	else if(Keyboard.isKeyDown(Keyboard.KEY_ADD) && !this.getIsPlayerSupernatural(player))
	{
		player.addChatMessage(player.username + "is a human.");
	}
}

public static void initializePlayerHandler()
{
	SupernaturalPlayerHandler init = new SupernaturalPlayerHandler();
}
}

Link to comment
Share on other sites

Man, you couldn't use a Map<EntityPlayer,String> directly ?

You are holding SupernaturalPlayerHandler objects all over the place...

 

By the way, containsKey(obj) is known to behave differently depending on equals(obj) and hashcode implementations in the object.

Considering you are dealing with player objects, using get and handling null cases would be easier.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.