-
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.
- Loading branch information
1 parent
9b79c23
commit b7c9179
Showing
3 changed files
with
69 additions
and
1 deletion.
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
66 changes: 66 additions & 0 deletions
66
common/src/main/java/dev/progames723/hmmm/persistence/PersistentEntityVariable.java
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,66 @@ | ||
package dev.progames723.hmmm.persistence; | ||
|
||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.ListTag; | ||
import net.minecraft.nbt.Tag; | ||
import net.minecraft.world.entity.Entity; | ||
|
||
import java.util.Random; | ||
|
||
public class PersistentEntityVariable<T extends Tag> { | ||
private final T baseVariable; | ||
private final Entity entity; | ||
private final String name; | ||
private static final String HmmmPersistent = "HmmmPersistent"; | ||
|
||
private PersistentEntityVariable(Entity entity, T baseVariable) { | ||
this.entity = entity; | ||
this.baseVariable = baseVariable; | ||
this.name = "PersistentValue_%s".formatted(new Random().nextLong(100000000L)); | ||
attachToEntity(); | ||
} | ||
|
||
private PersistentEntityVariable(String name, Entity entity, T baseVariable) { | ||
this.entity = entity; | ||
this.baseVariable = baseVariable; | ||
this.name = name; | ||
attachToEntity(); | ||
} | ||
|
||
public Entity getEntity() { | ||
return entity; | ||
} | ||
|
||
public CompoundTag getAsCompound() { | ||
CompoundTag tag = new CompoundTag(); | ||
tag.put(name, baseVariable); | ||
return tag; | ||
} | ||
|
||
private void attachToEntity() { | ||
CompoundTag tag = entity.saveWithoutId(new CompoundTag()); | ||
tag.putString("id", entity.getEncodeId()); | ||
tag.put(HmmmPersistent, getAsCompound()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private T getVariableInEntity() { | ||
return (T) entity.saveWithoutId(new CompoundTag()).getCompound(HmmmPersistent).get(name); | ||
} | ||
|
||
private void setVariableInEntity(T variable) { | ||
entity.saveWithoutId(new CompoundTag()).getCompound(HmmmPersistent).put(name, variable); | ||
} | ||
|
||
public static <T extends Tag> PersistentEntityVariable<T> create(Entity entity, T variable) { | ||
return new PersistentEntityVariable<>(entity, variable); | ||
} | ||
|
||
public static <T extends Tag> PersistentEntityVariable<T> create(String name, Entity entity, T variable) { | ||
return new PersistentEntityVariable<>(name, entity, variable); | ||
} | ||
|
||
static class test { | ||
static PersistentEntityVariable variable = PersistentEntityVariable.create(null, new ListTag()); | ||
} | ||
} |