Move stuff over from OTEMod

This commit is contained in:
Aria 2023-02-27 15:16:46 -07:00
parent fb71db187d
commit f27c76f334
40 changed files with 2036 additions and 5 deletions

View file

@ -0,0 +1,6 @@
package dev.zontreck.otemod.implementation.warps;
public class NoSuchWarpException extends Exception
{
}

View file

@ -0,0 +1,41 @@
package dev.zontreck.otemod.implementation.warps;
import java.util.UUID;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import dev.zontreck.otemod.database.TeleportDestination;
import net.minecraft.nbt.CompoundTag;
public class Warp {
public UUID owner;
public String WarpName;
public boolean RTP;
public boolean isPublic;
public TeleportDestination destination;
public Warp(UUID owner, String name, boolean rtp, boolean publicWarp, TeleportDestination destination)
{
this.owner=owner;
WarpName=name;
RTP=rtp;
isPublic=publicWarp;
this.destination=destination;
}
public static Warp deserialize(CompoundTag tag) throws InvalidDeserialization
{
return new Warp(tag.getUUID("owner"), tag.getString("name"), tag.getBoolean("rtp"), tag.getBoolean("public"), new TeleportDestination(tag.getCompound("destination")));
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
tag.putUUID("owner", owner);
tag.putString("name", WarpName);
tag.putBoolean("rtp", RTP);
tag.putBoolean("public", isPublic);
tag.put("destination", destination.serialize());
return tag;
}
}

View file

@ -0,0 +1,92 @@
package dev.zontreck.otemod.implementation.warps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.implementation.events.WarpCreatedEvent;
import dev.zontreck.otemod.implementation.events.WarpDeletedEvent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
public class Warps
{
private Map<String,Warp> warps = new HashMap<>();
private Warps(){
}
public Warp getNamedWarp(String name) throws NoSuchWarpException
{
if(warps.containsKey(name))
return warps.get(name);
else throw new NoSuchWarpException();
}
public void add(Warp w)
{
warps.put(w.WarpName,w);
WarpsProvider.updateFile();
WarpCreatedEvent e = new WarpCreatedEvent(w);
OTEMod.bus.post(e);
}
public Map<String, Warp> get()
{
return new HashMap<String, Warp>(warps);
}
public void delete(Warp w)
{
warps.remove(w.WarpName);
WarpsProvider.updateFile();
WarpDeletedEvent e = new WarpDeletedEvent(w);
OTEMod.bus.post(e);
}
public static Warps getNew()
{
return new Warps();
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
ListTag lst = new ListTag();
for(Map.Entry<String, Warp> entry : warps.entrySet())
{
lst.add(entry.getValue().serialize());
}
tag.put("warps", lst);
return tag;
}
public static Warps deserialize(CompoundTag tag)
{
Warps w = new Warps();
ListTag lst = tag.getList("warps", Tag.TAG_COMPOUND);
for (Tag tag2 : lst) {
Warp warp;
try {
warp = Warp.deserialize((CompoundTag)tag2);
w.warps.put(warp.WarpName, warp);
} catch (InvalidDeserialization e) {
e.printStackTrace();
}
}
return w;
}
}

View file

@ -0,0 +1,58 @@
package dev.zontreck.otemod.implementation.warps;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import dev.zontreck.otemod.database.FileTreeDatastore;
import net.minecraft.nbt.NbtIo;
public class WarpsProvider extends FileTreeDatastore
{
public static final Path BASE = of("warps");
public static final Path WARPS_DATA = BASE.resolve("warps.nbt");
public static final Warps WARPS_INSTANCE;
static{
if(!BASE.toFile().exists()){
try {
Files.createDirectory(BASE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
WARPS_INSTANCE = getOrCreate();
}
/**
* Creates a new warps instance, or returns a fully deserialized instance
* @return
*/
private static Warps getOrCreate()
{
Warps instance = null;
if(WARPS_DATA.toFile().exists())
{
try{
instance= Warps.deserialize(NbtIo.read(WARPS_DATA.toFile()));
}catch(Exception e){
instance=Warps.getNew();
}
}else {
instance=Warps.getNew();
}
return instance;
}
public static void updateFile()
{
try {
NbtIo.write(WARPS_INSTANCE.serialize(), WARPS_DATA.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}