Jump to content

[1.16.5] Increasing float in capability over time


BlockyPenguin

Recommended Posts

So I have made a capability for chunks, and it has a float field in it. I would like the float to tick up by 0.1 every x amount of ticks (configurable by user). How would I go about doing this?

Thanks!

Edited by BlockyPenguin

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

  • BlockyPenguin changed the title to [1.16.5] Increasing float in capability over time
1 hour ago, diesieben07 said:

Option 1: Use Chunk#getInhabitedTime, it increases every tick, but only if there are actual players nearby (not spectators) or if the chunk is force loaded. This might be enough for your needs.

Option 2: If that does not work for you, use WorldTickEvent, check that you are on the server and then use something similar to ServerChunkProvider#tickChunks to update all loaded chunks.

Thanks, I think I'll use Chunk#getInhabitedTime. Where should I put my code though? Is there an event or something?

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

Wherever you would normally access the float value in your capability, use getInhabitedTime.

So, my capability has a getter for the float, yes, but how would I get the instance of Chunk?

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

So this is my capability:

public class RiftCapability implements IRift {
	
	private boolean isRift;
	private float riftActivityPercent;
	
	public RiftCapability() {
		this.isRift = Rifts.RAND.nextInt(100) < 5;
		this.riftActivityPercent = isRift ? Utils.randomFloatBounds(0.15f) : 0.0f;
	}
	
	public RiftCapability(boolean isRift, float riftActivityPercent) {
		this.isRift = isRift;
		this.riftActivityPercent = riftActivityPercent;
	}

	@Override
	public boolean isRift() {
		return isRift;
	}

	@Override
	public float getRiftActivityPercent() {
		return riftActivityPercent;
	}

	@Override
	public void setRiftActivityPercent(float riftActivityPercent) {
		if(riftActivityPercent < 0 || riftActivityPercent > 1) throw new IndexOutOfBoundsException("Rift Activity Percent cannot be less than 0 or greater than 1!");
		else this.riftActivityPercent = riftActivityPercent;
	}

	@Override
	public void openRift() {
		this.isRift = true;
	}

	@Override
	public void closeRift() {
		this.isRift = false;
	}

	@Override
	public void riftEvent() {
		//TODO: Fire a rift event.
	}
}

 

This is my provider:

public class RiftCapabilityProvider implements ICapabilitySerializable<INBT> {
	
	@CapabilityInject(IRift.class)
	public static final Capability<IRift> RIFT_CAPABILITY = null;
	private LazyOptional<IRift> instance = LazyOptional.of(RIFT_CAPABILITY::getDefaultInstance);
	
	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
		return cap == RIFT_CAPABILITY ? instance.cast() : LazyOptional.empty();
	}
	
	@Override
	public INBT serializeNBT() {
		return RIFT_CAPABILITY.getStorage().writeNBT(
			RIFT_CAPABILITY,
			instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")),
			null
		);
	}
	
	@Override
	public void deserializeNBT(INBT nbt) {
		RIFT_CAPABILITY.getStorage().readNBT(
			RIFT_CAPABILITY,
			instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")),
			null,
			nbt
		);
	}
}

 

And I attach it like so:

public static void attachRiftCapability(AttachCapabilitiesEvent<Chunk> event) {
	World world = event.getObject().getWorld();
	if(!world.isRemote)
		event.addCapability(new ResourceLocation(Rifts.MODID, "rift_capability"), new RiftCapabilityProvider());
}

 

Does this help?

Edited by BlockyPenguin

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

You don't need the float value anymore, you can calculate it from the inhabited time.

Oh, ok... but my question still remains as to how to get the inhabited time.

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

Just now, diesieben07 said:

Why? What for?

Okay, I'm thinking we're not quite on the same page here, so I'll start again:

I've made a capability for chunks, which contains a float value. I would like the float to increase by 0.1 every x amount of time. My thought is that if I get all the loaded chunks, I can iterate over them and increase this value. As to how I would implement a timer without a tick() method, I am at a loss, but one problem at a time.

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

18 minutes ago, diesieben07 said:

I suggested using getInhabitedTime instead of this float value. Minecraft already increments getInhabitedTime for you.

But wouldn't it be incremented at a different rate? I think I forgot to mention as well, that I have the float set to a random value between 0.0 and 0.2 in the capability's default implementation's constructor.

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

Yes, but that is easily adjusted for. Say in your config you say "increment it by 3 every tick", then just take the inhabited time and multiply it by 3 - done.

 

So every chunk starts out with a random value? If you want to avoid ticking still, you can base this random value on the chunk's coordinates (Use something like MathHelper.getSeed, which is what Minecraft uses for the random block rendering offset). Then to get the "final value" just take this "random start value" and add the inhabited time multiplied whatever the config says.

Okay... so how would i get the chunk then? Would it help if i uploaded my code to github?

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

Dude.

You don't need to get the chunk. Minecraft updates the data for you.

You don't need a capability. You don't need to store anything.

Whenever you need the data, you can just calculate it.

Okay, so, let's say that I want to get this data when an item is right clicked, it would be something like this, right?

@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
	ItemStack stack = player.getHeldItem(hand);
	
  	Chunk c = (Chunk)world.getChunk(player.getPosition());
	c.getInhabitedTime();
  	
  	return ActionResult.resultSuccess(stack);
}

But MathHelper.getSeed doesn't seem to exist...

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Why this cast?

Because World#getChunk() returns an IChunk.

 

5 minutes ago, diesieben07 said:

Its getCoordinateRandom if you're not on mojang mappings yet.

Ah, thanks. TBH, I'm a little concerned about changing over to Mojang mappings, I know there are a few legality issues still. What would be the right place to ask about this?

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

The Forge discord.

Okay, thanks :)

2 minutes ago, diesieben07 said:

No... it doesn't? At least not all of its versions. And those that return an IChunk do not always return a Chunk. Blindly casting is almost always a bad idea.

Here is the source code of World#getChunk:

default IChunk getChunk(BlockPos pos) {
  return this.getChunk(pos.getX() >> 4, pos.getZ() >> 4);
}

So what do you suggest I do then?

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

That's IWorldReader#getChunk, not World#getChunk.

That's strange, because I'm definitely calling it on a World object, unless World implements IWorldReader.

2 hours ago, diesieben07 said:

Use instanceof.

So this is what I've got:

@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
	IChunk ichunk = world.getChunk(player.getPosition());
	
  	if(ichunk instanceof Chunk) {
		Chunk chunk = (Chunk)ichunk;
		
		long seed = MathHelper.getCoordinateRandom(player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ());
		seed += chunk.getInhabitedTime() * Config.RIFT_MULTIPLICATION_FACTOR.get();
		player.sendStatusMessage(new StringTextComponent(seed + ""), false);
	}
	
  	return ActionResult.resultSuccess(player.getHeldItem(hand));
}

But how would I turn that into value between 0 and 1? or at least 0 and 100, or some other easily measurable number. Then I've also got to worry about the boolean dictating whether or not a chunk is a rift or not.

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

15 minutes ago, diesieben07 said:

You need to use the chunk position here, not the player position. Otherwise it will be different depending on where you are in the chunk. Also, you probably don't want to actually use getCoordinateRandom, but some other method to get some pseudo-random data.

You could for example use a Random instance and use setSeed() and then call nextFloat.

Okay, so what would I set the seed to? I already have a public static final Random instance in my main class, would that do the job?

Today (22/10/20) I reached 100 posts!

I'm probably more excited than I should be for something so realistically minor...

Link to comment
Share on other sites

3 hours ago, BlockyPenguin said:

unless World implements IWorldReader

*Cough*

It does.

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

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I've had this problem for a long while now, and am just now remembering that Forge has a forum site. I feel dumb that I could've had this problem solved sooner. Though it is a problem I've only experienced as of 1.20(to my memory). Everything prior to that version worked fine up until I installed Forge 1.20. Anyway, I've tried some things including trying previous versions and (at the recommendation of others)taking my resource pack off. Paste.ee link lol Running Minecraft version 1.12 is the version of Forge that works without issues. I've deduced that this is because the overlay that loads the game and mods in future versions of Forge is not present, but this still doesn't solve my problem of how to fix the crashing of said future versions, or why it crashes in the first place. Yes, this happens even without mods. I'm not very good at messing with files, so here I am. Hopefully I did this right after looking at the FAQ.
    • it seems i can put back my mods in my mods folder one by one with their dependencies to get the config file, but when i put 2 or more it doesn't create it
    • Call+27732318372 $100‱-Authentic-Love Spells New York-London-Tokyo-Sydney-Toronto Visit website: https://www.strongspellcaster.us.com. For Rebinding Love Spells New York, Best love spells New York, Genuine love spells New York, Attraction love spells New York, Marriage spells New York, Divorce spells New York, Stop Cheating spells New York, Return Ex-Lover New York, Real Love spells New York, Bring back Ex Lover and Psychic Reader in USA. +27732318372 Are you currently seeing someone? Are you feeling that your lover’s love for is slowly diminishing? Are you looking for something that will instantly strengthen your relationship? Have you tried seeking help from various spell casters but failed because they only gave false hope? Are you hoping to win your lost lover back? Is your current relationship giving you so much misery? Do you want the man/girl of your dreams to find you alluring? Then you are at the right place because here you will find exactly what you have been searching for and that is the Return of Your Lost Love My name is Aadila. During the last 30 years, I have helped many people solve their relationship problems and financial difficulties with powerful Egyptian magic spells. All my clients had different problems, wishes, and goals. All situations involved various people, different spirits, and different souls and as the name goes I am truly a Lovespell Expert. Therefore it is very important that I get to know you, and your problem and gain profound insight into your situation. All my spell work is tailored to your specific needs because this is the only way you will receive your results, and your problems will be solved in a timely manner. You contacted me because you have a problem in your life, which you would like to solve through authentic spiritual means. I am here to assist you. While browsing my site, you will get to know me better, and this site will prepare you to make the right decision. website: https://www.strongspellcaster.us.com. Vodoo is an oral tradition practised by extended families that inherit familial spirits, along with the necessary devotional practices, from their elders. In the cities, local hierarchies of priestesses or priests (manbo and oungan), “children of the spirits” (ounsi), and ritual drummers (ountògi) comprise more formal “societies” or “congregations” (sosyete). In these congregations, knowledge is passed on through a ritual of initiation (kanzo) in which the body becomes the site of spiritual transformation. There is some regional difference in ritual practice across Haiti, and branches of the religion include Rada, Daome, Ibo, Nago, Dereal, Manding, Petwo, and Kongo. There is no centralized hierarchy, no single leader, and no official spokesperson, but various groups sometimes attempt to create such official structures. There are also secret societies, called Bizango or Sanpwèl, that perform a religio-juridical function. I have been having lots of questions from clients about our prices, As a voodoo Priest, it’s 100% forbidden to tax anyone for our services (It's a Taboo). We will only accept FREE WILL DONATIONS AFTER YOU HAVE SEEN RESULTS and not forgetting that every ritual has a list of items to use so all customers PROVIDE JUST THEIR ITEMS NEEDED FOR THEIR WORK Depending on their request!
    • Call +27732318372 $100‱ -Lottery Spells USA: Most Powerful Lottery Spells to Win the Mega Millions in New York. Visit website: https://www.strongspellcaster.us.com I'm William Jones  from the United States. I started playing lottery games 4 years ago and I have never won big. I went online to seek help on how I can win big in my lottery games and I saw some nice reviews about Prof Mama Khulusum who has made different people huge winners in their lottery games with her prayers. I gave it a try and I contacted Prof Mama Khulusum who told me how and what to do before I can become a big lotto winner and I accepted. 
    • $100‱+27732318372 NEW YORK-## LOTTERY SPELLS #STAR# LOTTERY SPELLS THAT NEVER MISS IN USA. visit also my website: https://www.strongspellcaster.us.com HOW TO WIN LOTTERY JACKPOT ASK DR MAMA KHULUSUM IN Fiji Island, Malta, Greek Island cruise, Amelia Island properties, Robin Island, Channel Island By a real lottery spell cater prof mama KHULUSUM +27732318372. Who doesn’t want to win the lottery? Chances are that most of us could do with a little
  • Topics

×
×
  • Create New...

Important Information

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