Jump to content

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


Recommended Posts

Posted

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?

Posted

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/

Posted
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!

Posted

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/

Posted

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.

Posted
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?

Posted

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.

Posted
2 hours ago, Draco18s said:

The...world...?

All entities have a reference to it.

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?

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

Posted (edited)
22 minutes ago, Draco18s said:

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.

Okay, what does it want for the capability augment? The provider?

Edited by OrangeVillager61
Posted

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.

Posted

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.

Posted

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;
			}

 

Posted (edited)
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

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

    • Well, when I log in to the server, sometimes within an hour, sometimes within a minute, the server closes and informs me that there was a Ticking entity error. Below is the crash report
    • Try switching to Windowed or Borderless Window mode in Minecraft. These modes make it easier for the recorder to capture gameplay, as it still has access to the display without the game taking up all of the graphics resources.
    • This forum is for Forge, not NeoForge. Please go to them for support.
    • Forge version: 55.0.0 Minecraft version: 1.21.5 Downloads: As this is the start of a new version, it is recommended that you check the downloads page and use the latest version to receive any bug fixes. Downloads page Intro: Good evening! Today, we have released our initial build of Forge 55.0 for Minecraft 1.21.5. 1.21.5 is the newest member of the 1.21 family of versions, which was released yesterday on March 25, 2025. As a reminder, the first minor (X.0) of a Forge version is a beta. Forge betas are marked as such on the bottom left of the title screen and are candidates for any breaking changes. Additionally, there are a couple of important things to note about this update, which I've made sure to mention in this post as well. Feel free to chat with us about bugs or these implementation changes on GitHub and in our Discord server. As always, we will continue to keep all versions of 1.21 and 1.20 in active support as covered by our tiered support policy. Cheers, happy modding, and good luck porting! Rendering Refactor For those who tuned in to Minecraft Live on March 22, 2025, you may already know that Mojang have announced their intention to bring their new Vibrant Visuals overhaul to Java in the future. They've taken the first steps toward this by refactoring how rendering pipelines and render types are handled internally. This has, in turn, made many of Forge's rendering APIs that have existed for years obsolete, as they (for the most part) can be done directly in vanilla. If there was a rendering API that was provided by Forge which you believe should be re-implemented, we're happy to discuss on GitHub through an issue or a pull request. Deprecation of weapon-like ToolActions In 1.21.5, Minecraft added new data components for defining the characteristics of weapons in data. This includes attack speed, block tags which define efficient blocks, and more. As such, we will begin marking our ToolActions solution for this as deprecated. ToolActions were originally added to address the problem of creating modded tools that needed to perform the same actions as vanilla tools. There are still a few tool actions that will continue to be used, such as the shears tool action for example. There are some existing Forge tool actions that are currently obsolete and have no effect given the way the new data components are implemented. We will continue to work on these deprecations and invite you to chat with us on GitHub or Discord if you have any questions.
    • In summary, a full mod to adjust mining progress in such a specific way does not yet exist in its exact form, but it is possible to find mods that change certain aspects of progression (e.g. "Harder Ores").
  • Topics

×
×
  • Create New...

Important Information

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