CarryOn/Common/src/main/java/tschipp/carryon/common/carry/CarryOnData.java
2022-10-11 22:25:56 +02:00

118 lines
3.0 KiB
Java

package tschipp.carryon.common.carry;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import javax.annotation.Nullable;
public class CarryOnData {
private CarryType type;
private CompoundTag nbt;
public CarryOnData(CompoundTag data)
{
if(data.contains("type"))
this.type = CarryType.valueOf(data.getString("type"));
else
this.type = CarryType.INVALID;
this.nbt = data;
}
public CompoundTag getNbt()
{
nbt.putString("type", type.toString());
return nbt;
}
public void setBlock(BlockState state, @Nullable BlockEntity tile)
{
this.type = CarryType.BLOCK;
if(state.hasProperty(BlockStateProperties.WATERLOGGED))
state = state.setValue(BlockStateProperties.WATERLOGGED, false);
CompoundTag stateData = NbtUtils.writeBlockState(state);
nbt.put("block", stateData);
if(tile != null)
{
CompoundTag tileData = tile.saveWithId();
nbt.put("tile", tileData);
}
}
public BlockState getBlock()
{
if(this.type != CarryType.BLOCK)
throw new IllegalStateException("Called getBlock on data that contained " + this.type);
return NbtUtils.readBlockState(nbt.getCompound("block"));
}
@Nullable
public BlockEntity getBlockEntity(BlockPos pos)
{
if(this.type != CarryType.BLOCK)
throw new IllegalStateException("Called getBlockEntity on data that contained " + this.type);
if(!nbt.contains("tile"))
return null;
return BlockEntity.loadStatic(pos, this.getBlock(), nbt.getCompound("tile"));
}
public void setEntity(Entity entity)
{
this.type = CarryType.ENTITY;
CompoundTag entityData = new CompoundTag();
entity.save(entityData);
nbt.put("entity", entityData);
}
public Entity getEntity(Level level)
{
if(this.type != CarryType.ENTITY)
throw new IllegalStateException("Called getEntity on data that contained " + this.type);
return EntityType.create(nbt.getCompound("entity"), level).orElseThrow();
}
public boolean isCarrying()
{
return this.type != CarryType.INVALID;
}
public boolean isCarrying(CarryType type)
{
return this.type == type;
}
public void clear()
{
this.type = CarryType.INVALID;
this.nbt = new CompoundTag();
}
public int getTick()
{
if(!this.nbt.contains("tick"))
return -1;
return this.nbt.getInt("tick");
}
public enum CarryType {
BLOCK,
ENTITY,
INVALID
}
}