mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
* First pass reformatting code to be more readable (common still not done) * Replace google common collect calls with Java calls where possible * Remove a bunch of unnecessary parenthesis leaving clarifying ones * Add some missing @Nonnull annotations * Fix checking wrong type * Remove unnecessary continue/return statements and simplify comparision check * Pass exception instead of directly printing stack trace * Work on cleaning up common, through config so far * Work on cleaning up common, through integration so far * Work on cleaning up common, through network so far * Work on cleaning up common, through tier so far * Remove empty constructors that get created by default anyways * Work on cleaning up common, through transmitters so far * Work on cleaning up common, through utils so far * Finish cleaning up spacing for common
195 lines
5.7 KiB
Java
195 lines
5.7 KiB
Java
package mekanism.common.tile.component;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
import java.util.UUID;
|
|
import mekanism.api.Coord4D;
|
|
import mekanism.api.TileNetworkList;
|
|
import mekanism.common.Mekanism;
|
|
import mekanism.common.PacketHandler;
|
|
import mekanism.common.base.ITileComponent;
|
|
import mekanism.common.config.MekanismConfig;
|
|
import mekanism.common.frequency.Frequency;
|
|
import mekanism.common.frequency.FrequencyManager;
|
|
import mekanism.common.security.ISecurityTile.SecurityMode;
|
|
import mekanism.common.security.SecurityFrequency;
|
|
import mekanism.common.tile.prefab.TileEntityContainerBlock;
|
|
import mekanism.common.util.MekanismUtils;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
public class TileComponentSecurity implements ITileComponent {
|
|
|
|
/**
|
|
* TileEntity implementing this component.
|
|
*/
|
|
public TileEntityContainerBlock tileEntity;
|
|
|
|
private UUID ownerUUID;
|
|
private String clientOwner;
|
|
|
|
private SecurityMode securityMode = SecurityMode.PUBLIC;
|
|
|
|
private SecurityFrequency frequency;
|
|
|
|
public TileComponentSecurity(TileEntityContainerBlock tile) {
|
|
tileEntity = tile;
|
|
tile.components.add(this);
|
|
}
|
|
|
|
public void readFrom(TileComponentSecurity security) {
|
|
ownerUUID = security.ownerUUID;
|
|
securityMode = security.securityMode;
|
|
}
|
|
|
|
public SecurityFrequency getFrequency() {
|
|
return frequency;
|
|
}
|
|
|
|
public void setFrequency(UUID owner) {
|
|
FrequencyManager manager = Mekanism.securityFrequencies;
|
|
manager.deactivate(Coord4D.get(tileEntity));
|
|
|
|
for (Frequency freq : manager.getFrequencies()) {
|
|
if (freq.ownerUUID.equals(owner)) {
|
|
frequency = (SecurityFrequency) freq;
|
|
frequency.activeCoords.add(Coord4D.get(tileEntity));
|
|
return;
|
|
}
|
|
}
|
|
|
|
Frequency freq = new SecurityFrequency(owner).setPublic(true);
|
|
freq.activeCoords.add(Coord4D.get(tileEntity));
|
|
manager.addFrequency(freq);
|
|
frequency = (SecurityFrequency) freq;
|
|
|
|
MekanismUtils.saveChunk(tileEntity);
|
|
tileEntity.markDirty();
|
|
}
|
|
|
|
public UUID getOwnerUUID() {
|
|
return ownerUUID;
|
|
}
|
|
|
|
public void setOwnerUUID(UUID uuid) {
|
|
frequency = null;
|
|
ownerUUID = uuid;
|
|
}
|
|
|
|
public String getClientOwner() {
|
|
return clientOwner;
|
|
}
|
|
|
|
public SecurityMode getMode() {
|
|
if (MekanismConfig.current().general.allowProtection.val()) {
|
|
return securityMode;
|
|
}
|
|
return SecurityMode.PUBLIC;
|
|
}
|
|
|
|
public void setMode(SecurityMode mode) {
|
|
securityMode = mode;
|
|
}
|
|
|
|
public FrequencyManager getManager(Frequency freq) {
|
|
if (ownerUUID == null || freq == null) {
|
|
return null;
|
|
}
|
|
return Mekanism.securityFrequencies;
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
if (!tileEntity.getWorld().isRemote) {
|
|
if (frequency == null && ownerUUID != null) {
|
|
setFrequency(ownerUUID);
|
|
}
|
|
FrequencyManager manager = getManager(frequency);
|
|
|
|
if (manager != null) {
|
|
if (frequency != null && !frequency.valid) {
|
|
frequency = (SecurityFrequency) manager.validateFrequency(ownerUUID, Coord4D.get(tileEntity), frequency);
|
|
}
|
|
if (frequency != null) {
|
|
frequency = (SecurityFrequency) manager.update(Coord4D.get(tileEntity), frequency);
|
|
}
|
|
} else {
|
|
frequency = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void read(NBTTagCompound nbtTags) {
|
|
securityMode = SecurityMode.values()[nbtTags.getInteger("securityMode")];
|
|
if (nbtTags.hasKey("ownerUUID")) {
|
|
ownerUUID = UUID.fromString(nbtTags.getString("ownerUUID"));
|
|
}
|
|
if (nbtTags.hasKey("securityFreq")) {
|
|
frequency = new SecurityFrequency(nbtTags.getCompoundTag("securityFreq"));
|
|
frequency.valid = false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void read(ByteBuf dataStream) {
|
|
securityMode = SecurityMode.values()[dataStream.readInt()];
|
|
|
|
if (dataStream.readBoolean()) {
|
|
ownerUUID = UUID.fromString(PacketHandler.readString(dataStream));
|
|
clientOwner = PacketHandler.readString(dataStream);
|
|
} else {
|
|
ownerUUID = null;
|
|
clientOwner = null;
|
|
}
|
|
|
|
if (dataStream.readBoolean()) {
|
|
frequency = new SecurityFrequency(dataStream);
|
|
} else {
|
|
frequency = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void write(NBTTagCompound nbtTags) {
|
|
nbtTags.setInteger("securityMode", securityMode.ordinal());
|
|
if (ownerUUID != null) {
|
|
nbtTags.setString("ownerUUID", ownerUUID.toString());
|
|
}
|
|
if (frequency != null) {
|
|
NBTTagCompound frequencyTag = new NBTTagCompound();
|
|
frequency.write(frequencyTag);
|
|
nbtTags.setTag("securityFreq", frequencyTag);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void write(TileNetworkList data) {
|
|
data.add(securityMode.ordinal());
|
|
|
|
if (ownerUUID != null) {
|
|
data.add(true);
|
|
data.add(ownerUUID.toString());
|
|
data.add(MekanismUtils.getLastKnownUsername(ownerUUID));
|
|
} else {
|
|
data.add(false);
|
|
}
|
|
|
|
if (frequency != null) {
|
|
data.add(true);
|
|
frequency.write(data);
|
|
} else {
|
|
data.add(false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void invalidate() {
|
|
if (!tileEntity.getWorld().isRemote) {
|
|
if (frequency != null) {
|
|
FrequencyManager manager = getManager(frequency);
|
|
if (manager != null) {
|
|
manager.deactivate(Coord4D.get(tileEntity));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |