Jump to content

[1.7.10] How would you set up a dynamic link between entities and a player


Recommended Posts

Posted

Hi everyone,

 

So I want to set up a dynamic link between a series of entities and the player so that the player has each spawned entities position data (that updates dynamically as the entity moves) and all of the entities properties also. I have a player that has the NBT data for a party of entities. This player can use the data that is stores in his IEEP to spawn these entities at will and can despawn them/update the NBT that is saved to the IEEP also. I now want to establish a link dynamic link between the player and the entity that is spawned in the world so that the player has stuff like the position and all properties of the entity at all times during which the entity is spawned in the world. How would you go about doing this? any ideas?

 

Also as a side question, should I update to 1.8 or is it a pain, I understand that rendering changed like crazy and from what ive been reading a lot is a pain to fix but is it worth it?

 

As always any input is greatly appreciated

Posted

This is my working party class where I store my entity data for the party members that the player has acquired. I want to somehow establish a dynamic link between the entities that the player spawns into the world and the player himself so that the player knows the position of each entity and their stats etc etc... I was wondering how would be a good way to go about doing this. Any ideas?

 

FYI atm I use a pretty bad method to check whether or not each entity is spawned in the world using arrays (and im trying to come up with a better method for determining if a party member is spawned in the world or not but for now the one I have seems to work). I use an int array that sets the value of the array slot to my party NBT[] slot # when the mob is spawned and resets it to -1 when the mob is not spawned, I then get the mobs NBT from the value of the slots spawnedSelectedSlot[]), but if I create a dynamic link between the entities and the NBT data that is saved here then I wont have to use the inefficient system i dont think).

 


public class PlayerParty implements IExtendedEntityProperties {

protected EntityPlayer entityPlayer;
protected EntityPlayer ownerName;
	public static final int NUM_SLOTS = 6;
	public int currentSelectedSlot = 0; //default slot is the first slot
	public int[] spawnedSelectedSlot = new int[6];

public boolean partyFull = false; // Used in EventTryToAddMobToParty in the  tryToAddMobToParty it allows for one to get a spawnegg item if party is full that contains the mob
private static final String EXTENDED_ENTITY_PROPERTIES_TAGNAME = "PlayerParty";


protected NBTTagCompound[] customMobPartyNbt = new NBTTagCompound[NUM_SLOTS];

public PlayerParty(EntityPlayer player) {
	this.entityPlayer = player;
	spawnedSelectedSlot[0] = -1; //A value of -1 should mean that the mob in slot 0 is not spawned
	spawnedSelectedSlot[1] = -1; 
	spawnedSelectedSlot[2] = -1; 
	spawnedSelectedSlot[3] = -1; 
	spawnedSelectedSlot[4] = -1; 
	spawnedSelectedSlot[5] = -1; 
}

//sets the customMob in the specified slot. Stores only the NBT data, which is enough to recreate the customMob later.
     // doesnt seem to be explicitly called but without it customMob dont go into the party
public void setCustomMob(int slot, EntityCustomMob customMob, NBTTagCompound caughtTag){
	NBTTagCompound compound = new NBTTagCompound();
	customMob.writeToNBT(compound);
	customMobPartyNbt[slot] = compound; 


		}

public void setCustomMobInSlot(int slot, NBTTagCompound customMob, int mobID, String ownerName){
	customMobPartyNbt[slot] = customMob; //the NBTTagCompound that is being passed here from tryToAddMobToParty() method inside EventTryToAddMobToParty class doesnt have a key ie (customMob.hasKey("caughtCustomMob") = false) here dunno y but we need it to be true	
	customMobPartyNbt[slot].setInteger("MobID", mobID);
	System.out.println("ownerName  "+ ownerName);
	customMobPartyNbt[slot].setString("ownerName", ownerName);
	}
   
public void setSelectedSlot(int slot){
   currentSelectedSlot = slot; 
    }

public int getSelectedSlot(){
	return currentSelectedSlot;
}
public int getSpawnedSlot(){
	for(int i = 0; i < NUM_SLOTS; i++){
		if(this.spawnedSelectedSlot[i] !=-1)
			return i;
	}
	return -1;

}

//returns true if the player has a free party slot
public boolean hasFreeSlot(){
	return (this.getNextFreeSlot() >= 0);
}

//gets the first free slot in the player's party. The NBT tag compound that slot position should
//be null.
public int getNextFreeSlot(){
	for(int i = 0; i < NUM_SLOTS; i++){
		if(this.customMobPartyNbt[i] == null)
			return i;
	}
	return -1;
}

public int getNextFullSlot(){
	for(int i = 0; i < NUM_SLOTS; i++){
		if(this.customMobPartyNbt[i] != null)
			return i;
	}
	return -1;
}

public NBTTagCompound getCustomMobNBTFromSlot(int slot){
return customMobPartyNbt[slot];
}


public void swapCustomMobn(int slotOne, int slotTwo, boolean wasPressed){
	if(slotOne != -1 && slotTwo!=-1){
	NBTTagCompound tempSlot = customMobPartyNbt[slotOne];
	customMobPartyNbt[slotOne] = customMobPartyNbt[slotTwo];
	customMobPartyNbt[slotTwo] = tempSlot;
	}
	}

/**
 * Used to register these extended properties for the player during EntityConstructing event
 */
public static final void register(EntityPlayer player) {
	player.registerExtendedProperties(EXTENDED_ENTITY_PROPERTIES_TAGNAME, new PlayerParty(player));
}

/**
 * Copies additional player data from the given BattleMobExtendedPlayerHelper instance
 * Avoids NBT disk I/O overhead when cloning a player after respawn
 */
public void copy(PlayerParty properties) {
	customMobPartyNbt = properties.customMobPartyNbt.clone();
}

/**
 * Returns BattleMobExtendedPlayerHelper properties for player
 */
public static final PlayerParty get(EntityPlayer player) {
	return (PlayerParty) player.getExtendedProperties(EXTENDED_ENTITY_PROPERTIES_TAGNAME);
}

@Override
public void saveNBTData(NBTTagCompound compound) {
	NBTTagCompound properties = new NBTTagCompound();
	for(int i=0; i < NUM_SLOTS; i++){

		if(customMobPartyNbt[i] != null){
			properties.setTag("Slot"+ i, customMobPartyNbt[i]);
				} 
			}
	compound.setTag(EXTENDED_ENTITY_PROPERTIES_TAGNAME, properties);



}

@Override
public void loadNBTData(NBTTagCompound compound) {

	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXTENDED_ENTITY_PROPERTIES_TAGNAME);
	if(properties != null){
		for(int i=0; i < NUM_SLOTS; i++){
			if(properties.hasKey("Slot"+i)){
				customMobPartyNbt[i] = properties.getCompoundTag("Slot"+i);
						}
		}
	}
}

public void updatePlayerPartyServerToClient(){
	PacketOverlord.sendTo(new PacketSyncPlayerParty((EntityPlayer) entityPlayer),(EntityPlayerMP) entityPlayer);
}


@Override
public void init(Entity entity, World world) {

}


}

Posted

I got this Idea from digging around to use the PlayerEvent.StartTracking event to establish the link between the entity and the player as long as both are tracking each other. Im not sure if this is the best approach but I think it should work, if I do the appropriate checks and get the exact entity I want then somehow send packets to update each about the others position and what not.

@SubscribeEvent(priority=EventPriority.NORMAL)
public void onTracking(PlayerEvent.StartTracking event) {
	//Establish the link here
}
}

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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