equivalent-exchange-3/src/main/java/com/pahimar/ee3/tileentity/TileEntityEE.java

137 lines
3.8 KiB
Java
Raw Normal View History

2014-04-02 04:47:08 +02:00
package com.pahimar.ee3.tileentity;
2023-01-03 17:47:36 +01:00
import java.util.UUID;
2014-04-29 03:24:31 +02:00
import com.pahimar.ee3.network.PacketHandler;
2014-04-30 03:46:59 +02:00
import com.pahimar.ee3.network.message.MessageTileEntityEE;
2014-04-02 04:47:08 +02:00
import com.pahimar.ee3.reference.Names;
import net.minecraft.entity.player.EntityPlayer;
2014-04-02 04:47:08 +02:00
import net.minecraft.nbt.NBTTagCompound;
2014-04-29 03:24:31 +02:00
import net.minecraft.network.Packet;
2014-04-02 04:47:08 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.UsernameCache;
2014-04-02 04:47:08 +02:00
import net.minecraftforge.common.util.ForgeDirection;
2023-01-03 17:47:36 +01:00
public class TileEntityEE extends TileEntity {
2014-04-02 04:47:08 +02:00
protected ForgeDirection orientation;
protected byte state;
protected String customName;
protected UUID ownerUUID;
2014-04-02 04:47:08 +02:00
2023-01-03 17:47:36 +01:00
public TileEntityEE() {
2014-04-02 04:47:08 +02:00
orientation = ForgeDirection.SOUTH;
state = 0;
customName = "";
ownerUUID = null;
2014-04-02 04:47:08 +02:00
}
2023-01-03 17:47:36 +01:00
public ForgeDirection getOrientation() {
2014-04-02 04:47:08 +02:00
return orientation;
}
2023-01-03 17:47:36 +01:00
public void setOrientation(ForgeDirection orientation) {
2014-04-30 03:46:59 +02:00
this.orientation = orientation;
2014-04-02 04:47:08 +02:00
}
2023-01-03 17:47:36 +01:00
public void setOrientation(int orientation) {
2014-04-30 03:46:59 +02:00
this.orientation = ForgeDirection.getOrientation(orientation);
2014-04-02 04:47:08 +02:00
}
2023-01-03 17:47:36 +01:00
public short getState() {
2014-04-02 04:47:08 +02:00
return state;
}
2023-01-03 17:47:36 +01:00
public void setState(byte state) {
2014-04-02 04:47:08 +02:00
this.state = state;
}
2023-01-03 17:47:36 +01:00
public String getCustomName() {
2014-04-02 04:47:08 +02:00
return customName;
}
2023-01-03 17:47:36 +01:00
public void setCustomName(String customName) {
2014-04-02 04:47:08 +02:00
this.customName = customName;
}
2023-01-03 17:47:36 +01:00
public UUID getOwnerUUID() {
return ownerUUID;
}
2023-01-03 17:47:36 +01:00
public String getOwnerName() {
if (ownerUUID != null) {
return UsernameCache.getLastKnownUsername(ownerUUID);
}
return "Unknown";
}
2023-01-03 17:47:36 +01:00
public void setOwner(EntityPlayer entityPlayer) {
this.ownerUUID = entityPlayer.getPersistentID();
2014-04-02 04:47:08 +02:00
}
2023-01-03 17:47:36 +01:00
public void setOwnerUUID(UUID ownerUUID) {
this.ownerUUID = ownerUUID;
2014-04-02 04:47:08 +02:00
}
@Override
2023-01-03 17:47:36 +01:00
public void readFromNBT(NBTTagCompound nbtTagCompound) {
2014-04-02 04:47:08 +02:00
super.readFromNBT(nbtTagCompound);
2023-01-03 17:47:36 +01:00
if (nbtTagCompound.hasKey(Names.NBT.DIRECTION)) {
this.orientation
= ForgeDirection.getOrientation(nbtTagCompound.getByte(Names.NBT.DIRECTION
));
2014-04-02 04:47:08 +02:00
}
2023-01-03 17:47:36 +01:00
if (nbtTagCompound.hasKey(Names.NBT.STATE)) {
2014-04-02 04:47:08 +02:00
this.state = nbtTagCompound.getByte(Names.NBT.STATE);
}
2023-01-03 17:47:36 +01:00
if (nbtTagCompound.hasKey(Names.NBT.CUSTOM_NAME)) {
2014-04-02 04:47:08 +02:00
this.customName = nbtTagCompound.getString(Names.NBT.CUSTOM_NAME);
}
2023-01-03 17:47:36 +01:00
if (nbtTagCompound.hasKey(Names.NBT.OWNER_UUID_MOST_SIG)
&& nbtTagCompound.hasKey(Names.NBT.OWNER_UUID_LEAST_SIG)) {
this.ownerUUID = new UUID(
nbtTagCompound.getLong(Names.NBT.OWNER_UUID_MOST_SIG),
nbtTagCompound.getLong(Names.NBT.OWNER_UUID_LEAST_SIG)
);
2014-04-02 04:47:08 +02:00
}
}
@Override
2023-01-03 17:47:36 +01:00
public void writeToNBT(NBTTagCompound nbtTagCompound) {
2014-04-02 04:47:08 +02:00
super.writeToNBT(nbtTagCompound);
nbtTagCompound.setByte(Names.NBT.DIRECTION, (byte) orientation.ordinal());
nbtTagCompound.setByte(Names.NBT.STATE, state);
2023-01-03 17:47:36 +01:00
if (this.hasCustomName()) {
2014-04-02 04:47:08 +02:00
nbtTagCompound.setString(Names.NBT.CUSTOM_NAME, customName);
}
2023-01-03 17:47:36 +01:00
if (this.hasOwner()) {
nbtTagCompound.setLong(
Names.NBT.OWNER_UUID_MOST_SIG, ownerUUID.getMostSignificantBits()
);
nbtTagCompound.setLong(
Names.NBT.OWNER_UUID_LEAST_SIG, ownerUUID.getLeastSignificantBits()
);
2014-04-02 04:47:08 +02:00
}
}
2014-04-29 03:24:31 +02:00
2023-01-03 17:47:36 +01:00
public boolean hasCustomName() {
return customName != null && customName.length() > 0;
}
2023-01-03 17:47:36 +01:00
public boolean hasOwner() {
return ownerUUID != null;
}
2014-04-29 03:24:31 +02:00
@Override
2023-01-03 17:47:36 +01:00
public Packet getDescriptionPacket() {
2014-04-30 03:46:59 +02:00
return PacketHandler.INSTANCE.getPacketFrom(new MessageTileEntityEE(this));
2014-04-29 03:24:31 +02:00
}
2014-04-02 04:47:08 +02:00
}