Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.9] Assistance required in handling chest opening interactions


baegmon
 Share

Recommended Posts

Currently working on a custom party system but I can't seem to figure out chests.

 

I have a HashMap which tracks all the players and the blocks they have placed (have made sure this also allows Chests):

 

private HashMap<String, ArrayList<Block>> ownedBlocks = new HashMap<String, ArrayList<Block>>();

 

Now, I'm trying to handle an event where a user who did not place the block cannot access the contents of the chests.

 

@SubscribeEvent
public void playerOpenChest(PlayerOpenContainerEvent event){

	EntityPlayer player = event.getEntityPlayer();

	if(player.openContainer instanceof ContainerChest){

		ContainerChest chest = (ContainerChest) player.openContainer;	
		String playerName = player.getName();

		// if HashMap contains player name
		if(ownedBlocks.containsKey(playerName)){
			ArrayList<Block> playerPlacedBlocks = ownedBlocks.get(playerName);

			if(playerPlacedBlocks.contains(chest)){
				System.out.println("I made this chest! I can open this chest!");

			} else {
				System.out.println("This chest is not mine! I shouldn't open other peoples chests!");
			}

		}


	}

}

 

Whenever I try to open my chests, it fires "This chest is not mine! I shouldn't open other peoples chests!"

This is what I have so far, I'm completely lost on how to handle the "if~else" interactions with chests and would love any direction you guys could give me. Thanks!

Link to comment
Share on other sites

Ok, first of all, your data structure (that Map-List thing) is not appropriate at all.

First of, you will want to store this per Chunk and dynamically load and unload based on when chunks load and unload. Use

ChunkEvent.Load

,

ChunkEvent.Unload

,

ChunkDataEvent.Load

and

ChunkDataEvent.Save

.

 

Moreover you cannot just track

Block

instances. The

Block

instance represents all blocks of a certain type (e.g. Blocks.stone represents all stone blocks). Doing this is not the easiest task in the world, since you have to detect any modification to the world. Basically you want to store (possibly) a player for every block position in the world. You will want to use the player's UUID for this.

Link to comment
Share on other sites

Ok, first of all, your data structure (that Map-List thing) is not appropriate at all.

First of, you will want to store this per Chunk and dynamically load and unload based on when chunks load and unload. Use

ChunkEvent.Load

,

ChunkEvent.Unload

,

ChunkDataEvent.Load

and

ChunkDataEvent.Save

.

 

Moreover you cannot just track

Block

instances. The

Block

instance represents all blocks of a certain type (e.g. Blocks.stone represents all stone blocks). Doing this is not the easiest task in the world, since you have to detect any modification to the world. Basically you want to store (possibly) a player for every block position in the world. You will want to use the player's UUID for this.

 

Alright will have to research abit more for how to properly implement chunks and stuff but on to the actual question, how do you tell another player that they can't use a chest through the PlayerOpenContainer event?

Link to comment
Share on other sites

I suggest using the tile entity NBT data. If you're using your own custom chests (which is really the best option), override various methods in the block class instead of using evebts., store the player UUID in the tile entity and override readFromNBT and writeToNBT.

If you're working with vanilla chests, you will have to use events. You may have to store the custom player UUID per-chunk like how diesieben07 said as well.

catch(Exception e)

{

 

}

Yay, Pokémon exception handling, gotta catch 'em all (and then do nothing with 'em).

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
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.

 Share

×
×
  • Create New...

Important Information

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