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

100 lines
3.8 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
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 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
2016-01-23 11:02:35 +01:00
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
2016-06-01 00:39:35 +02:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
import de.ellpeck.actuallyadditions.mod.util.Util;
import io.netty.util.internal.ConcurrentSet;
2015-10-18 19:21:32 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-05-08 21:09:58 +02:00
public abstract class TileEntityLaserRelay extends TileEntityBase{
2015-10-18 19:21:32 +02:00
public static final int MAX_DISTANCE = 15;
private static final float[] COLOR = new float[]{1F, 0F, 0F};
2016-05-08 21:09:58 +02:00
private static final float[] COLOR_ITEM = new float[]{139F/255F, 94F/255F, 1F};
2016-05-19 20:05:12 +02:00
public final boolean isItem;
2016-05-08 21:09:58 +02:00
public TileEntityLaserRelay(String name, boolean isItem){
super(name);
this.isItem = isItem;
}
2015-10-23 16:54:33 +02:00
@Override
2016-02-01 17:49:55 +01:00
public void receiveSyncCompound(NBTTagCompound compound){
2016-06-04 14:38:20 +02:00
LaserRelayConnectionHandler.removeRelayFromNetwork(this.pos, this.worldObj);
2015-10-27 23:09:23 +01:00
NBTTagList list = compound.getTagList("Connections", 10);
if(!list.hasNoTags()){
2016-02-01 17:49:55 +01:00
for(int i = 0; i < list.tagCount(); i++){
LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
2016-06-04 14:38:20 +02:00
LaserRelayConnectionHandler.addConnection(pair.firstRelay, pair.secondRelay, this.worldObj);
}
}
super.receiveSyncCompound(compound);
2015-10-23 16:54:33 +02:00
}
2015-10-18 19:21:32 +02:00
@Override
public NBTTagCompound getUpdateTag(){
NBTTagCompound compound = super.getUpdateTag();
NBTTagList list = new NBTTagList();
2015-10-18 19:21:32 +02:00
2016-06-04 14:38:20 +02:00
ConcurrentSet<LaserRelayConnectionHandler.ConnectionPair> connections = LaserRelayConnectionHandler.getConnectionsFor(this.pos, this.worldObj);
if(connections != null && !connections.isEmpty()){
for(LaserRelayConnectionHandler.ConnectionPair pair : connections){
list.appendTag(pair.writeToNBT());
2015-10-21 00:22:50 +02:00
}
}
compound.setTag("Connections", list);
return compound;
2015-10-18 19:21:32 +02:00
}
@Override
2016-02-01 17:49:55 +01:00
public void updateEntity(){
super.updateEntity();
if(this.worldObj.isRemote){
this.renderParticles();
}
}
2015-10-21 00:22:50 +02:00
2016-02-01 17:49:55 +01:00
@SideOnly(Side.CLIENT)
public void renderParticles(){
if(Util.RANDOM.nextInt(ConfigValues.lessParticles ? 16 : 8) == 0){
2016-02-01 17:49:55 +01:00
BlockPos thisPos = this.pos;
2016-06-04 14:38:20 +02:00
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(thisPos, this.worldObj);
2016-02-01 17:49:55 +01:00
if(network != null){
for(LaserRelayConnectionHandler.ConnectionPair aPair : network.connections){
if(aPair.contains(thisPos) && PosUtil.areSamePos(thisPos, aPair.firstRelay)){
PacketParticle.renderParticlesFromAToB(aPair.firstRelay.getX(), aPair.firstRelay.getY(), aPair.firstRelay.getZ(), aPair.secondRelay.getX(), aPair.secondRelay.getY(), aPair.secondRelay.getZ(), ConfigValues.lessParticles ? 1 : Util.RANDOM.nextInt(3)+1, 0.8F, this.isItem ? COLOR_ITEM : 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
}
2015-12-01 19:48:09 +01:00
@Override
public void invalidate(){
super.invalidate();
2016-06-04 14:38:20 +02:00
LaserRelayConnectionHandler.removeRelayFromNetwork(this.pos, this.worldObj);
2015-12-01 19:48:09 +01:00
}
2015-10-18 19:21:32 +02:00
}