ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java

212 lines
7.9 KiB
Java
Raw Normal View History

2015-10-18 19:21:32 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityLaserRelay.java") is part of the Actually Additions mod for Minecraft.
2015-10-18 19:21:32 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-10-18 19:21:32 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-10-18 19:21:32 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-10-18 19:21:32 +02:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2016-09-12 16:13:39 +02:00
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import de.ellpeck.actuallyadditions.api.laser.Network;
2017-02-13 19:07:53 +01:00
import de.ellpeck.actuallyadditions.mod.items.InitItems;
import de.ellpeck.actuallyadditions.mod.misc.apiimpl.ConnectionPair;
2017-02-13 19:07:53 +01:00
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import io.netty.util.internal.ConcurrentSet;
2016-12-05 15:09:37 +01:00
import net.minecraft.entity.player.EntityPlayer;
2017-02-13 19:07:53 +01:00
import net.minecraft.item.ItemStack;
2015-10-18 19:21:32 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2017-02-13 17:36:55 +01:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
2016-12-05 15:09:37 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2017-02-13 17:36:55 +01:00
import net.minecraftforge.items.IItemHandler;
2016-05-08 21:09:58 +02:00
import java.util.Set;
2017-02-13 17:36:55 +01:00
public abstract class TileEntityLaserRelay extends TileEntityInventoryBase{
2015-10-18 19:21:32 +02:00
public static final int MAX_DISTANCE = 15;
2017-02-14 20:48:01 +01:00
public static final int MAX_DISTANCE_RANGED = 35;
2016-05-08 21:09:58 +02:00
public final LaserType type;
private Network cachedNetwork;
private int changeAmountAtCaching = -1;
2017-02-13 19:07:53 +01:00
private int lastRange;
2016-09-12 16:13:39 +02:00
private Set<IConnectionPair> tempConnectionStorage;
public TileEntityLaserRelay(String name, LaserType type){
2017-02-13 17:36:55 +01:00
super(1, name);
this.type = type;
}
2015-10-23 16:54:33 +02:00
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
if(type == NBTType.SYNC){
2016-11-26 21:32:27 +01:00
ActuallyAdditionsAPI.connectionHandler.removeRelayFromNetwork(this.pos, this.world);
NBTTagList list = compound.getTagList("Connections", 10);
if(!list.hasNoTags()){
for(int i = 0; i < list.tagCount(); i++){
2016-09-12 16:13:39 +02:00
ConnectionPair pair = new ConnectionPair();
pair.readFromNBT(list.getCompoundTagAt(i));
2016-11-26 21:32:27 +01:00
ActuallyAdditionsAPI.connectionHandler.addConnection(pair.getPositions()[0], pair.getPositions()[1], this.type, this.world, pair.doesSuppressRender());
}
}
}
2015-10-23 16:54:33 +02:00
}
2015-10-18 19:21:32 +02:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
if(type == NBTType.SYNC){
NBTTagList list = new NBTTagList();
2016-11-26 21:32:27 +01:00
ConcurrentSet<IConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.pos, this.world);
if(connections != null && !connections.isEmpty()){
2016-09-12 16:13:39 +02:00
for(IConnectionPair pair : connections){
NBTTagCompound tag = new NBTTagCompound();
pair.writeToNBT(tag);
list.appendTag(tag);
}
2015-10-21 00:22:50 +02:00
}
compound.setTag("Connections", list);
}
2015-10-18 19:21:32 +02:00
}
2017-02-13 19:07:53 +01:00
@Override
public void updateEntity(){
super.updateEntity();
int range = this.getMaxRange();
if(this.lastRange != range){
ConcurrentSet<IConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.pos, this.world);
if(connections != null && !connections.isEmpty()){
for(IConnectionPair pair : connections){
int distanceSq = (int)pair.getPositions()[0].distanceSq(pair.getPositions()[1]);
if(distanceSq > range*range){
ActuallyAdditionsAPI.connectionHandler.removeConnection(this.world, pair.getPositions()[0], pair.getPositions()[1]);
}
}
}
this.lastRange = range;
}
}
/*@Override
2016-02-01 17:49:55 +01:00
public void updateEntity(){
super.updateEntity();
2016-11-26 21:32:27 +01:00
if(this.world.isRemote){
2016-02-01 17:49:55 +01:00
this.renderParticles();
}
}
2015-10-21 00:22:50 +02:00
2016-02-01 17:49:55 +01:00
@SideOnly(Side.CLIENT)
public void renderParticles(){
2016-11-26 21:32:27 +01:00
if(this.world.rand.nextInt(8) == 0){
EntityPlayer player = Minecraft.getMinecraft().player;
if(player != null){
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
WrenchMode mode = WrenchMode.values()[data.theCompound.getInteger("LaserWrenchMode")];
if(mode != WrenchMode.NO_PARTICLES){
ItemStack stack = player.getHeldItemMainhand();
if(mode == WrenchMode.ALWAYS_PARTICLES || (StackUtil.isValid(stack) && stack.getItem() instanceof ItemLaserWrench)){
2016-11-26 21:32:27 +01:00
Network network = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.pos, this.world);
if(network != null){
2016-09-12 16:13:39 +02:00
for(IConnectionPair aPair : network.connections){
if(!aPair.doesSuppressRender() && aPair.contains(this.pos) && this.pos.equals(aPair.getPositions()[0])){
2016-11-26 21:32:27 +01:00
AssetUtil.renderParticlesFromAToB(aPair.getPositions()[0].getX(), aPair.getPositions()[0].getY(), aPair.getPositions()[0].getZ(), aPair.getPositions()[1].getX(), aPair.getPositions()[1].getY(), aPair.getPositions()[1].getZ(), this.world.rand.nextInt(3)+1, 0.8F, this.type == LaserType.ITEM ? COLOR_ITEM : (this.type == LaserType.FLUID ? COLOR_FLUIDS : COLOR), 1F);
}
}
}
2016-02-01 17:49:55 +01:00
}
}
}
2015-10-21 00:22:50 +02:00
}
}*/
2015-10-18 19:21:32 +02:00
public Network getNetwork(){
if(this.cachedNetwork == null || this.cachedNetwork.changeAmount != this.changeAmountAtCaching){
this.cachedNetwork = ActuallyAdditionsAPI.connectionHandler.getNetworkFor(this.pos, this.world);
if(this.cachedNetwork != null){
this.changeAmountAtCaching = this.cachedNetwork.changeAmount;
}
else{
this.changeAmountAtCaching = -1;
}
}
return this.cachedNetwork;
}
2015-12-01 19:48:09 +01:00
@Override
public void invalidate(){
super.invalidate();
//This is because Minecraft randomly invalidates tiles on world join and then validates them again
//We need to compensate for this so that connections don't get broken randomly
2016-11-26 21:32:27 +01:00
this.tempConnectionStorage = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.pos, this.world);
2016-11-26 21:32:27 +01:00
ActuallyAdditionsAPI.connectionHandler.removeRelayFromNetwork(this.pos, this.world);
2015-12-01 19:48:09 +01:00
}
@Override
public void validate(){
if(this.tempConnectionStorage != null){
2016-09-12 16:13:39 +02:00
for(IConnectionPair pair : this.tempConnectionStorage){
2016-11-26 21:32:27 +01:00
ActuallyAdditionsAPI.connectionHandler.addConnection(pair.getPositions()[0], pair.getPositions()[1], pair.getType(), this.world, pair.doesSuppressRender());
}
this.tempConnectionStorage = null;
}
super.validate();
}
@Override
2017-02-13 17:36:55 +01:00
@SideOnly(Side.CLIENT)
public AxisAlignedBB getRenderBoundingBox(){
return INFINITE_EXTENT_AABB;
}
2016-12-05 15:09:37 +01:00
2017-02-13 17:36:55 +01:00
@Override
public boolean shouldSyncSlots(){
return true;
}
@Override
public IItemHandler getItemHandler(EnumFacing facing){
return null;
}
2017-02-13 19:07:53 +01:00
public int getMaxRange(){
ItemStack upgrade = this.slots.getStackInSlot(0);
if(StackUtil.isValid(upgrade) && upgrade.getItem() == InitItems.itemLaserUpgradeRange){
2017-02-14 20:48:01 +01:00
return MAX_DISTANCE_RANGED;
2017-02-13 19:07:53 +01:00
}
else{
return MAX_DISTANCE;
}
}
2016-12-05 15:09:37 +01:00
@SideOnly(Side.CLIENT)
public abstract String getExtraDisplayString();
@SideOnly(Side.CLIENT)
public abstract String getCompassDisplayString();
public abstract void onCompassAction(EntityPlayer player);
2015-10-18 19:21:32 +02:00
}