Skip to content

Commit

Permalink
ъ
Browse files Browse the repository at this point in the history
  • Loading branch information
Progames723 committed Oct 23, 2024
1 parent 9b79c23 commit b7c9179
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This library is mainly made for my mods, buy you can use it in your mod!
## How to use in your mods
Go to my [maven](https://github.com/Progames723/maven) repo

### first ever mod to use JNI
### first ever library to use JNI
im going insane

# TODO
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/dev/progames723/hmmm/GMPWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
public class GMPWrapper {
/**
* DO NOT MODIFY DURING RUNTIME
* <p>
* IF MODIFIED FROM {@code false} TO {@code true} EXPECT AN IRRECOVERABLE ERROR
*/
private static boolean isSupported = true;//assume true until false
private boolean isCleared = false;
Expand Down
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());
}
}

0 comments on commit b7c9179

Please sign in to comment.