Jump to content

[1.10] Actions when player step on entity


KmKadze

Recommended Posts

Hi everyone! (sorry for my english)

 

I want to create my own Armor Stand(call it "auto armor stand") which is activated when player "step on" it. I used onCollideWithPlayer method, in which I wrote verifying of that the player is in the same block as the "auto armor stand". If it's true, another method swaps armor:

public void onCollideWithPlayer(EntityPlayer player)
    { 
    	if(isActive)
    	{	    	 		
    		int playerX = player.getPosition().getX();
        	int playerY = player.getPosition().getY();
        	int playerZ = player.getPosition().getZ();
        	int thisX = this.getPosition().getX();
        	int thisY = this.getPosition().getY();
        	int thisZ = this.getPosition().getZ();
        	
        	if(playerX == thisX && playerY == thisY && playerZ == thisZ && !player.isSpectator() && !player.isInvisible())
        	{ 	
        		swapItemsWithPlayer(player, EntityEquipmentSlot.MAINHAND);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.OFFHAND);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.HEAD);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.CHEST);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.LEGS);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.FEET);  
        		isActive = false;
        	}
    	}

I used "isActive" variable to make swap worked once, then went to cooldown. The problem is that the method all the same have a time to work twice and in the and armor doesn't swap. What should I do to fix this?

 

p.s. Armor stand and "auto armor stand" is an entity

Link to comment
Share on other sites

You can't store information in the Block class, you must store it elsewhere (TileEntity, Player Capability, etc.)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

You can't store information in the Block class, you must store it elsewhere (TileEntity, Player Capability, etc.)

 

Despite being placed by an

Item

in a block-like manner, the vanilla armor stand is an

Entity

rather than a

Block

. I suspect the OP's is the same.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I can't tell from the code snippet.

There may be also something else they did that I can't see.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Ok so maybe it'll be helpful. This is in mod main class:

@Mod(modid=modInfo.MODID, name=modInfo.MODNAME, version=modInfo.VERSION)
public class LittleFeatures 
{	
@Instance(modInfo.MODID)
public static LittleFeatures instance;	

@EventHandler
public void init(FMLInitializationEvent event)
{		
	EntitiesList.entities();	
}

        public static void registerEntity(Class entityClass, String name, int id)
{		
	EntityRegistry.registerModEntity(entityClass, name, id, instance, 64, 1, true);
}

}

EntitiesList.java:

public class EntitiesList {

public static void entities()
{                
	LittleFeatures.registerEntity(EntityAutoArmorStand.class, "autoArmorStand", 364545);
}
}

EntityAutoArmorStand.java (i copied all of the EntityArmorStand(minecraft's armor stand), changed variables' names and added this code):

public class EntityAutoArmorStand extends EntityLivingBase
{
    private int timer = 0; //for timer
    private boolean isActive = true; 

    public void onLivingUpdate() //cooldown timer, based on ticks
    {   	     
        if(isActive == false && timer < 60)
    	{
    		timer++;
    	}
    	else if(timer >= 60 && isActive == false)
    	{
    		isActive = true;
    		timer = 0;
    	}
    }
    
    public void onCollideWithPlayer(EntityPlayer player)
    { 
    	if(isActive == true)
    	{	    	 		
    		int playerX = player.getPosition().getX();
        	int playerY = player.getPosition().getY();
        	int playerZ = player.getPosition().getZ();
        	int thisX = this.getPosition().getX();
        	int thisY = this.getPosition().getY();
        	int thisZ = this.getPosition().getZ();
        	
        	if(playerX == thisX && playerY == thisY && playerZ == thisZ && !player.isSpectator() && !player.isInvisible())
        	{ 	
        		swapItemsWithPlayer(player, EntityEquipmentSlot.MAINHAND);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.OFFHAND);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.HEAD);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.CHEST);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.LEGS);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.FEET);  
        		isActive = false;
        	}
    	}
    }    
    
    private void swapItemsWithPlayer(EntityPlayer player, EntityEquipmentSlot slot)
    {
   		if(player.getItemStackFromSlot(slot) != null && this.getItemStackFromSlot(slot) == null)
       	{
       		this.setItemStackToSlot(slot, player.getItemStackFromSlot(slot));
       		player.setItemStackToSlot(slot, null);    	
       	}
       	else if(player.getItemStackFromSlot(slot) == null && this.getItemStackFromSlot(slot) != null)
       	{
       		player.setItemStackToSlot(slot, this.getItemStackFromSlot(slot));
       		this.setItemStackToSlot(slot, null); 
       		this.playEquipSound(player.getItemStackFromSlot(slot));
       	}
       	else if(player.getItemStackFromSlot(slot) != null && this.getItemStackFromSlot(slot) != null)
       	{
       		ItemStack playerStack = player.getItemStackFromSlot(slot);
       		ItemStack thisStack = this.getItemStackFromSlot(slot);
       		
       		this.setItemStackToSlot(slot, playerStack);
       		player.setItemStackToSlot(slot, thisStack);        	
       	}     	
    }

Link to comment
Share on other sites

I dont think onCollideWithPlayer is ever called because it is not overriding anything, and you never call it.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I dont think onCollideWithPlayer is ever called because it is not overriding anything, and you never call it.

 

onCollideWithPlayer is called by EntityPlayer whenever the player intersects an entity (it's in EntityPlayer, line 619)

 

    public void onCollideWithPlayer(EntityPlayer player)
    { 
    	if(isActive == true)
    	{	    	 		
    		int playerX = player.getPosition().getX();
        	int playerY = player.getPosition().getY();
        	int playerZ = player.getPosition().getZ();
        	int thisX = this.getPosition().getX();
        	int thisY = this.getPosition().getY();
        	int thisZ = this.getPosition().getZ();
        	
        	if(playerX == thisX && playerY == thisY && playerZ == thisZ && !player.isSpectator() && !player.isInvisible())
        	{ 	
        		swapItemsWithPlayer(player, EntityEquipmentSlot.MAINHAND);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.OFFHAND);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.HEAD);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.CHEST);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.LEGS);
        		swapItemsWithPlayer(player, EntityEquipmentSlot.FEET);  
        		isActive = false;
        	}
    	}
    }    

 

You don't need to manually check if the entity positions are equal and if the player is a spectator, EntityPlayer already does this (in essence). Try removing the position checks and the spectator check.

 

Also, you never reset the timer (timer=0) when the player collides with your entity for the first time, so your code will only run once.

Link to comment
Share on other sites

You don't need to manually check if the entity positions are equal and if the player is a spectator, EntityPlayer already does this (in essence). Try removing the position checks and the spectator check.

Also, you never reset the timer (timer=0) when the player collides with your entity for the first time, so your code will only run once.

Ok, I removed the position and spectator cheking, now it works perfectly but in the one block radius from the "auto armor stand".

Also, you never reset the timer (timer=0) when the player collides with your entity for the first time, so your code will only run once.

Timer resets in onLivingUpdate method.

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.