1.0.1.0 Move more common code from OTEMod into this library
This commit is contained in:
parent
fa3961d85b
commit
51043006b5
8 changed files with 360 additions and 2 deletions
|
@ -4,14 +4,39 @@ import org.slf4j.Logger;
|
|||
|
||||
import com.mojang.logging.LogUtils;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.server.ServerStartedEvent;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
|
||||
@Mod(LibZontreck.MOD_ID)
|
||||
public class LibZontreck {
|
||||
public static final Logger LOGGER = LogUtils.getLogger();
|
||||
public static final String MOD_ID = "libzontreck";
|
||||
public static MinecraftServer THE_SERVER;
|
||||
|
||||
public LibZontreck(){
|
||||
|
||||
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
// Register the setup method for modloading
|
||||
bus.addListener(this::setup);
|
||||
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
private void setup(final FMLCommonSetupEvent event)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@SubscribeEvent
|
||||
public void onServerStarted(final ServerStartedEvent event)
|
||||
{
|
||||
THE_SERVER = event.getServer();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package dev.zontreck.libzontreck.exceptions;
|
||||
|
||||
public class InvalidDeserialization extends Exception
|
||||
{
|
||||
|
||||
public InvalidDeserialization(String error){
|
||||
super(error);
|
||||
}
|
||||
public InvalidDeserialization(){
|
||||
super("Incorrect information was provided to the deserializer");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package dev.zontreck.libzontreck.vectors;
|
||||
|
||||
/*
|
||||
* This is a non-serializable instanced Vector that is meant to slam positions down as a integer
|
||||
*/
|
||||
public class NonAbsVector3
|
||||
{
|
||||
public long x;
|
||||
public long y;
|
||||
public long z;
|
||||
|
||||
public NonAbsVector3(Vector3 origin)
|
||||
{
|
||||
x = Math.round(origin.x);
|
||||
y = Math.round(origin.y);
|
||||
z = Math.round(origin.z);
|
||||
}
|
||||
}
|
68
src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java
Normal file
68
src/main/java/dev/zontreck/libzontreck/vectors/Vector2.java
Normal file
|
@ -0,0 +1,68 @@
|
|||
package dev.zontreck.libzontreck.vectors;
|
||||
|
||||
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
|
||||
import net.minecraft.world.phys.Vec2;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
public class Vector2
|
||||
{
|
||||
public float x;
|
||||
public float y;
|
||||
|
||||
public Vec2 asMinecraftVector(){
|
||||
return new Vec2(x, y);
|
||||
}
|
||||
|
||||
public Vector2()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Vector2(float x, float y)
|
||||
{
|
||||
this.x=x;
|
||||
this.y=y;
|
||||
}
|
||||
|
||||
public Vector2(Vec2 pos)
|
||||
{
|
||||
x=pos.x;
|
||||
y=pos.y;
|
||||
}
|
||||
|
||||
public Vector2(String pos) throws InvalidDeserialization
|
||||
{
|
||||
// This will be serialized most likely from the ToString method
|
||||
// Parse
|
||||
if(pos.startsWith("<"))
|
||||
{
|
||||
pos=pos.substring(1, pos.length()-1); // Rip off the ending bracket too
|
||||
String[] positions = pos.split(", ");
|
||||
if(positions.length!=2)
|
||||
{
|
||||
positions = pos.split(",");
|
||||
}
|
||||
|
||||
if(positions.length!=2)
|
||||
{
|
||||
throw new InvalidDeserialization("Positions must be in the same format provided by ToString() (ex. <1,1> or <1, 1>");
|
||||
}
|
||||
|
||||
this.x = Float.parseFloat(positions[0]);
|
||||
this.y = Float.parseFloat(positions[1]);
|
||||
// We are done now
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 Clone()
|
||||
{
|
||||
Vector2 n = new Vector2(x, y);
|
||||
return n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "<"+String.valueOf(x)+", "+String.valueOf(y) + ">";
|
||||
}
|
||||
}
|
140
src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java
Normal file
140
src/main/java/dev/zontreck/libzontreck/vectors/Vector3.java
Normal file
|
@ -0,0 +1,140 @@
|
|||
package dev.zontreck.libzontreck.vectors;
|
||||
|
||||
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
|
||||
public class Vector3
|
||||
{
|
||||
public double x;
|
||||
public double y;
|
||||
public double z;
|
||||
|
||||
public Vec3 asMinecraftVector(){
|
||||
return new Vec3(x, y, z);
|
||||
}
|
||||
|
||||
public BlockPos asBlockPos()
|
||||
{
|
||||
return new BlockPos(asMinecraftVector());
|
||||
}
|
||||
|
||||
public Vector3()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Vector3(double x, double y, double z)
|
||||
{
|
||||
this.x=x;
|
||||
this.y=y;
|
||||
this.z=z;
|
||||
}
|
||||
|
||||
public Vector3(Vec3 pos)
|
||||
{
|
||||
x=pos.x;
|
||||
y=pos.y;
|
||||
z=pos.z;
|
||||
}
|
||||
|
||||
public Vector3(BlockPos pos)
|
||||
{
|
||||
x=pos.getX();
|
||||
y=pos.getY();
|
||||
z=pos.getZ();
|
||||
}
|
||||
|
||||
public Vector3(String pos) throws InvalidDeserialization
|
||||
{
|
||||
// This will be serialized most likely from the ToString method
|
||||
// Parse
|
||||
if(pos.startsWith("<"))
|
||||
{
|
||||
pos=pos.substring(1, pos.length()-1); // Rip off the ending bracket too
|
||||
String[] positions = pos.split(", ");
|
||||
if(positions.length!=3)
|
||||
{
|
||||
positions = pos.split(",");
|
||||
}
|
||||
|
||||
if(positions.length!=3)
|
||||
{
|
||||
throw new InvalidDeserialization("Positions must be in the same format provided by ToString() (ex. <1,1,1> or <1, 1, 1>");
|
||||
}
|
||||
|
||||
this.x = Double.parseDouble(positions[0]);
|
||||
this.y = Double.parseDouble(positions[1]);
|
||||
this.z = Double.parseDouble(positions[2]);
|
||||
// We are done now
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 subtract(Vector3 other)
|
||||
{
|
||||
return new Vector3(x-other.x, y-other.y, z-other.z);
|
||||
}
|
||||
public Vector3 add(Vector3 other)
|
||||
{
|
||||
return new Vector3(x+other.x, y+other.y, z +other.z);
|
||||
}
|
||||
|
||||
public double distance(Vector3 other)
|
||||
{
|
||||
Vector3 sub = subtract(other);
|
||||
return Math.sqrt((sub.x * sub.x + sub.y * sub.y + sub.z * sub.z));
|
||||
}
|
||||
|
||||
public Vector3 moveUp()
|
||||
{
|
||||
Vector3 up = Clone();
|
||||
up.y+=1;
|
||||
return up;
|
||||
}
|
||||
public Vector3 moveDown()
|
||||
{
|
||||
Vector3 up = Clone();
|
||||
up.y-=1;
|
||||
return up;
|
||||
}
|
||||
|
||||
|
||||
public Vector3 Clone()
|
||||
{
|
||||
Vector3 n = new Vector3(x, y, z);
|
||||
return n;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "<"+String.valueOf(x)+", "+String.valueOf(y)+", "+String.valueOf(z)+">";
|
||||
}
|
||||
|
||||
public NonAbsVector3 rounded()
|
||||
{
|
||||
NonAbsVector3 cl = new NonAbsVector3(this);
|
||||
return cl;
|
||||
}
|
||||
|
||||
public CompoundTag serialize()
|
||||
{
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.putDouble("x", x);
|
||||
tag.putDouble("y", y);
|
||||
tag.putDouble("z", z);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public Vector3(CompoundTag tag) {
|
||||
this.deserialize(tag);
|
||||
}
|
||||
public void deserialize(CompoundTag tag)
|
||||
{
|
||||
x=tag.getDouble("x");
|
||||
y=tag.getDouble("y");
|
||||
z=tag.getDouble("z");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
package dev.zontreck.libzontreck.vectors;
|
||||
|
||||
import dev.zontreck.libzontreck.LibZontreck;
|
||||
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
|
||||
public class WorldPosition
|
||||
{
|
||||
|
||||
public Vector3 Position;
|
||||
public String Dimension;
|
||||
|
||||
public WorldPosition(CompoundTag tag, boolean pretty) throws InvalidDeserialization
|
||||
{
|
||||
if(pretty){
|
||||
|
||||
Position = new Vector3(tag.getString("Position"));
|
||||
Dimension = tag.getString("Dimension");
|
||||
}else {
|
||||
Position = new Vector3(tag.getCompound("pos"));
|
||||
Dimension = tag.getString("Dimension");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public WorldPosition(Vector3 pos, String dim)
|
||||
{
|
||||
Position=pos;
|
||||
Dimension=dim;
|
||||
}
|
||||
|
||||
public WorldPosition(Vector3 pos, ServerLevel lvl)
|
||||
{
|
||||
Position=pos;
|
||||
Dimension = lvl.dimension().location().getNamespace() + ":"+lvl.dimension().location().getPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return NbtUtils.structureToSnbt(serialize());
|
||||
}
|
||||
|
||||
public CompoundTag serializePretty()
|
||||
{
|
||||
CompoundTag tag = new CompoundTag();
|
||||
|
||||
tag.putString("Position", Position.toString());
|
||||
tag.putString("Dimension", Dimension);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public CompoundTag serialize()
|
||||
{
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.put("pos", Position.serialize());
|
||||
tag.putString("Dimension", Dimension);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ServerLevel getActualDimension()
|
||||
{
|
||||
|
||||
String dim = Dimension;
|
||||
String[] dims = dim.split(":");
|
||||
|
||||
ResourceLocation rl = new ResourceLocation(dims[0], dims[1]);
|
||||
ServerLevel dimL = null;
|
||||
for (ServerLevel lServerLevel : LibZontreck.THE_SERVER.getAllLevels()) {
|
||||
ResourceLocation XL = lServerLevel.dimension().location();
|
||||
|
||||
if(XL.getNamespace().equals(rl.getNamespace())){
|
||||
if(XL.getPath().equals(rl.getPath())){
|
||||
dimL = lServerLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(dimL == null)
|
||||
{
|
||||
LibZontreck.LOGGER.error("DIMENSION COULD NOT BE FOUND : "+Dimension);
|
||||
return null;
|
||||
}
|
||||
|
||||
return dimL;
|
||||
}
|
||||
}
|
Reference in a new issue