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-06-05 12:15:02 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2016-06-15 17:42:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
|
2016-06-08 21:09:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemLaserWrench;
|
2016-06-15 17:42:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemLaserWrench.WrenchMode;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
|
2016-06-05 16:01:10 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler.ConnectionPair;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
|
2016-06-01 00:39:35 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.Util;
|
2015-10-29 17:36:30 +01:00
|
|
|
import io.netty.util.internal.ConcurrentSet;
|
2016-06-08 21:09:41 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2015-10-18 19:21:32 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-10-29 17:36:30 +01:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
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
|
|
|
|
2016-06-05 16:01:10 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
public abstract class TileEntityLaserRelay extends TileEntityBase{
|
2015-10-18 19:21:32 +02:00
|
|
|
|
2015-11-28 19:02:01 +01:00
|
|
|
public static final int MAX_DISTANCE = 15;
|
2015-12-16 20:40:52 +01:00
|
|
|
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;
|
2015-11-28 19:02:01 +01:00
|
|
|
|
2016-06-05 16:01:10 +02:00
|
|
|
private Set<ConnectionPair> tempConnectionStorage;
|
|
|
|
|
2016-06-11 16:07:06 +02:00
|
|
|
private boolean hasCheckedHandlersAround;
|
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
public TileEntityLaserRelay(String name, boolean isItem){
|
|
|
|
super(name);
|
|
|
|
this.isItem = isItem;
|
2016-05-06 23:23:29 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-06-04 00:26:33 +02: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++){
|
2016-06-05 16:01:10 +02:00
|
|
|
ConnectionPair pair = ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
|
2016-06-05 14:33:53 +02:00
|
|
|
LaserRelayConnectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj);
|
2015-10-26 22:28:49 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-10 22:19:15 +02:00
|
|
|
|
|
|
|
super.receiveSyncCompound(compound);
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2015-10-18 19:21:32 +02:00
|
|
|
@Override
|
2016-05-26 13:11:43 +02:00
|
|
|
public NBTTagCompound getUpdateTag(){
|
|
|
|
NBTTagCompound compound = super.getUpdateTag();
|
2016-06-04 00:26:33 +02:00
|
|
|
NBTTagList list = new NBTTagList();
|
2015-10-18 19:21:32 +02:00
|
|
|
|
2016-06-05 16:01:10 +02:00
|
|
|
ConcurrentSet<ConnectionPair> connections = LaserRelayConnectionHandler.getConnectionsFor(this.pos, this.worldObj);
|
2016-06-04 00:26:33 +02:00
|
|
|
if(connections != null && !connections.isEmpty()){
|
2016-06-05 16:01:10 +02:00
|
|
|
for(ConnectionPair pair : connections){
|
2015-10-29 17:36:30 +01:00
|
|
|
list.appendTag(pair.writeToNBT());
|
2015-10-21 00:22:50 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-04 00:26:33 +02:00
|
|
|
|
|
|
|
compound.setTag("Connections", list);
|
2016-05-10 22:19:15 +02:00
|
|
|
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();
|
|
|
|
}
|
2016-06-11 16:07:06 +02:00
|
|
|
else if(!this.hasCheckedHandlersAround){
|
|
|
|
this.saveAllHandlersAround();
|
|
|
|
this.hasCheckedHandlersAround = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveAllHandlersAround(){
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
2015-10-21 00:22:50 +02:00
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public void renderParticles(){
|
2016-06-05 12:15:02 +02:00
|
|
|
if(Util.RANDOM.nextInt(ConfigBoolValues.LESS_PARTICLES.isEnabled() ? 16 : 8) == 0){
|
2016-06-08 21:09:41 +02:00
|
|
|
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
|
|
|
|
if(player != null){
|
2016-06-15 17:42:55 +02:00
|
|
|
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 || (stack != null && stack.getItem() instanceof ItemLaserWrench)){
|
|
|
|
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(this.pos, this.worldObj);
|
|
|
|
if(network != null){
|
|
|
|
for(ConnectionPair aPair : network.connections){
|
2016-07-04 20:15:41 +02:00
|
|
|
if(aPair.contains(this.pos) && this.pos.equals(aPair.positions[0])){
|
2016-06-15 17:42:55 +02:00
|
|
|
AssetUtil.renderParticlesFromAToB(aPair.positions[0].getX(), aPair.positions[0].getY(), aPair.positions[0].getZ(), aPair.positions[1].getX(), aPair.positions[1].getY(), aPair.positions[1].getZ(), ConfigBoolValues.LESS_PARTICLES.isEnabled() ? 1 : Util.RANDOM.nextInt(3)+1, 0.8F, this.isItem ? COLOR_ITEM : COLOR, 1F);
|
|
|
|
}
|
2016-06-08 21:09:41 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-31 14:42:51 +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-05 16:01:10 +02:00
|
|
|
//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
|
|
|
|
this.tempConnectionStorage = LaserRelayConnectionHandler.getConnectionsFor(this.pos, this.worldObj);
|
|
|
|
|
2016-06-04 14:38:20 +02:00
|
|
|
LaserRelayConnectionHandler.removeRelayFromNetwork(this.pos, this.worldObj);
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2016-06-05 16:01:10 +02:00
|
|
|
@Override
|
|
|
|
public void validate(){
|
|
|
|
if(this.tempConnectionStorage != null){
|
|
|
|
for(ConnectionPair pair : this.tempConnectionStorage){
|
|
|
|
LaserRelayConnectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj);
|
|
|
|
}
|
|
|
|
this.tempConnectionStorage = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
super.validate();
|
|
|
|
}
|
2015-10-18 19:21:32 +02:00
|
|
|
}
|