Jump to content

[1.12] How to have a ticking variable for a mob without overriding the mob?


OrangeVillager61

Recommended Posts

Hello!

 

I am getting butchers to harvest meat from animals and I need said animals to be unharvestable to villagers for 24000 ticks. My issue is that I want the butchers to harvest from other mobs in that time so I can't just disallow the butchers from harvesting mobs for that period. How do I this without overriding said mob and creating a new entity since I want compatibility with other mods?

Link to comment
Share on other sites

You can create a world data extension with a HashMap or similar collection where you map the entities to the time since last harvested, and update using the server tick event or world tick event. You would only need to add to the map when a successful harvest attempt happens (you don't have to have all the cows in the world in the map for example), and you could delete entries when the timer counts to zero or the entity dies. So it could be a fairly small and efficient map. 

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

8 minutes ago, jabelar said:

You can create a world data extension with a HashMap or similar collection where you map the entities to the time since last harvested, and update using the server tick event or world tick event. You would only need to add to the map when a successful harvest attempt happens (you don't have to have all the cows in the world in the map for example), and you could delete entries when the timer counts to zero or the entity dies. So it could be a fairly small and efficient map. 

Hmm, that sounds interesting and very useful. Unfortunately, I do not know what a HashMap is? Could you explain what a HashMap is and the basics how to do one?

 

Thanks for the help!

Link to comment
Share on other sites

HashMap is a common Java collection. It is just an implementation of the Map interface. A map just means there is a key and a value so you can look them up. So in this case you would add each entity as a "key" and the initial wait value as the "value". Then each tick you would iterate (loop) through the list of keys and for each on you would read the value and then set the value to one less (count down) and check if it hit zero (in which case you would remove the entry from the map).

 

Whenever a butcher tries to harvest an entity you would just check if the entity exists as a key in the map, if it does it means it is not yet harvestable.

 

Just search Google for Java HashMap examples.

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Whut.

How about applying a Capability to the animals that stores the last world-time they were harvested?

The butchers would then query that, do a calculation to see if that value is more than 24000 ticks ago, and then perform an action based on the result.

  • Like 1

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

9 minutes ago, Draco18s said:

Whut.

How about applying a Capability to the animals that stores the last world-time they were harvested?

The butchers would then query that, do a calculation to see if that value is more than 24000 ticks ago, and then perform an action based on the result.

Where would I get the world-time?

Link to comment
Share on other sites

The...world...?

All entities have a reference to it.

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

23 minutes ago, OrangeVillager61 said:

Alright, I have set up a basic capability, do I have to do anything for it to work client side or is that not necessary since the AI is doing it all. Also, how do I access this capability from my AI file?

If it's all AI, no, you don't need to do anything for the client.

As for the capability, use GetCapability(...) on the entity in question.

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

They're longs. Do subtraction.

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

Read the javadoc

 

Yes

  • Like 2

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

I seem to be having an issue with the capability. I can't find said issue though.

 

public class HarvestTimeProvider implements ICapabilitySerializable<NBTBase>{
	
	@CapabilityInject(IHarvestTime.class)
	public static final Capability<IHarvestTime> CAPABILITY_HARVEST_ANIMAL_TIME = null;
	
	private IHarvestTime instance = CAPABILITY_HARVEST_ANIMAL_TIME.getDefaultInstance();

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing)
	{
		return capability == CAPABILITY_HARVEST_ANIMAL_TIME;
	}
	 
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing)
	{
		return capability == CAPABILITY_HARVEST_ANIMAL_TIME ? CAPABILITY_HARVEST_ANIMAL_TIME.<T> cast(this.instance) : null;
	}
	 
	@Override
	public NBTBase serializeNBT()
	{
		return CAPABILITY_HARVEST_ANIMAL_TIME.getStorage().writeNBT(CAPABILITY_HARVEST_ANIMAL_TIME, this.instance, null);
	}
	
	@Override
	public void deserializeNBT(NBTBase nbt)
	{
		CAPABILITY_HARVEST_ANIMAL_TIME.getStorage().readNBT(CAPABILITY_HARVEST_ANIMAL_TIME, this.instance, null, nbt);
	}
}
public class HarvestTimeUser implements IHarvestTime{
	
	private long time = 0;
	
	@Override
	public void setTime(long l)
	{
		this.time = l;
	}

	@Override
	public long get_time() {
		return time;
	}

}
public interface IHarvestTime {

	void setTime(long l);
	
	long get_time();
}
public class IvCapabilities {

	public static void regsiterCapabilties()
	{
		CapabilityManager.INSTANCE.register(IHarvestTime.class, new IHarvestTimeStorage(), HarvestTimeUser.class);
	}
	
	public static class IHarvestTimeStorage implements IStorage<IHarvestTime>
	{

		@Override
		 public NBTBase writeNBT(Capability<IHarvestTime> capability, IHarvestTime instance, EnumFacing side)
		{
		return new NBTTagLong (instance.get_time());
		}
		
		@Override
		public void readNBT(Capability<IHarvestTime> capability, IHarvestTime instance, EnumFacing side, NBTBase nbt)
		{
		instance.setTime(((NBTPrimitive) nbt).getLong());
		}
	}
}
public class AttachCapabilties {
	
	public static final ResourceLocation HARVEST_TIME_CAP = new ResourceLocation(Reference.MOD_ID, "Harvest_Time");
			
	@SubscribeEvent
	public void AttachCapToEntity (AttachCapabilitiesEvent<Entity> event)
	{
		if (event.getObject() instanceof EntityAnimal)
		{
			event.addCapability(HARVEST_TIME_CAP, new HarvestTimeProvider());
		}
	}
}

When I'm using it:

if (this.villagerObj.world.getWorldTime() - entity.getCapability(HarvestTimeProvider.CAPABILITY_HARVEST_ANIMAL_TIME, null).get_time() < 24000 || entity.getCapability(HarvestTimeProvider.CAPABILITY_HARVEST_ANIMAL_TIME, null).get_time() == 0)
			{
        		System.out.println("Entity has been harvested");
				return false;
			}

 

Link to comment
Share on other sites

On 8/18/2017 at 4:53 PM, Draco18s said:

Whut.

How about applying a Capability to the animals that stores the last world-time they were harvested?

The butchers would then query that, do a calculation to see if that value is more than 24000 ticks ago, and then perform an action based on the result.

Above post

Forgot to quote this there.

Edited by OrangeVillager61
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.