forked from GregTechCEu/GregTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* i forgor release()
- Loading branch information
Showing
14 changed files
with
233 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package gregtech.api.util; | ||
|
||
import net.minecraft.world.World; | ||
|
||
import java.util.Optional; | ||
|
||
public class TickingObjectHolder<T> { | ||
|
||
private T object; | ||
private long lastUpdateTime; | ||
private final long defaultLifeTime; | ||
private long lifeTime; | ||
|
||
public TickingObjectHolder(T t, long lifeTime) { | ||
this.object = t; | ||
this.defaultLifeTime = lifeTime; | ||
this.lifeTime = lifeTime; | ||
} | ||
|
||
private void checkState(World world) { | ||
if(lifeTime <= 0) return; | ||
long worldTime = world.getTotalWorldTime(); | ||
if(lastUpdateTime != worldTime) { | ||
lifeTime -= (worldTime - lastUpdateTime); | ||
if(lifeTime <= 0) | ||
object = null; | ||
lastUpdateTime = worldTime; | ||
} | ||
} | ||
|
||
public Optional<T> get(World world) { | ||
checkState(world); | ||
return object == null ? Optional.empty() : Optional.of(object); | ||
} | ||
|
||
public T getNullable(World world) { | ||
checkState(world); | ||
return object; | ||
} | ||
|
||
public long getRemainingTime(World world) { | ||
checkState(world); | ||
return Math.max(0, lifeTime); | ||
} | ||
|
||
public void reset(T object, long lifeTime) { | ||
this.object = object; | ||
this.lifeTime = lifeTime; | ||
} | ||
|
||
public void reset(T object) { | ||
this.object = object; | ||
this.lifeTime = defaultLifeTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.