<commit_before>package mods.ocminecart.common.tileentity;

import li.cil.oc.api.Network;
import li.cil.oc.api.network.Analyzable;
import li.cil.oc.api.network.Connector;
import li.cil.oc.api.network.Environment;
import li.cil.oc.api.network.Message;
import li.cil.oc.api.network.Node;
import li.cil.oc.api.network.SidedEnvironment;
import li.cil.oc.api.network.Visibility;
import mods.ocminecart.OCMinecart;
import mods.ocminecart.common.util.IPlugable;
import mods.ocminecart.common.util.Plug;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.movable.IMovableTile;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@Optional.Interface(iface="appeng.api.movable.IMovableTile", modid="appliedenergistics2", striprefs=true)
public class NetworkRailBaseTile extends TileEntity implements ISidedInventory, SidedEnvironment, IPlugable, Analyzable, IMovableTile{
	
	
	private Plug rail;	//Environment for the Cart
	private Plug side;	//Environment for Cables, Computers, ...
	
	private boolean firstupdate = true; //First call of updateEntity()
	
	private ItemStack camoItem = null;
	private ItemStack camoItemOld = null;

	private IIcon camoTop = null;
	
	/*
	 * 0 = Connect: Network,Power
	 * 1 = Connect: Network
	 * 2 = Connect: Power
	 */
	private int Mode = 0;
	private boolean moving=false;
	
	public NetworkRailBaseTile(){
		if(FMLCommonHandler.instance().getEffectiveSide().isServer()){
			rail = new Plug(this);
			side = new Plug(this);
			
			rail.setNode(Network.newNode(rail, Visibility.Network).withConnector().create());
			side.setNode(Network.newNode(side,Visibility.Network).withConnector(500D).create());
		}
		this.markDirty();
	}
	
	/*---------NBT/Sync--------*/
	public void readFromNBT(NBTTagCompound nbt){
		super.readFromNBT(nbt);
		if(nbt.hasKey("conMode")) Mode = nbt.getInteger("conMode");
		
		camoItem=ItemStack.loadItemStackFromNBT((NBTTagCompound) nbt.getTag("CamoItem"));
		
		if(FMLCommonHandler.instance().getEffectiveSide().isServer()){
			 if(nbt.hasKey("Plug_1"))side.load((NBTTagCompound) nbt.getTag("Plug_1"));
			 if(nbt.hasKey("Plug_2"))rail.load((NBTTagCompound) nbt.getTag("Plug_2"));
		}
		
		this.markDirty();
	}
		
	public void writeToNBT(NBTTagCompound nbt){
		super.writeToNBT(nbt);
		nbt.setInteger("conMode", Mode);
		
		if(!this.worldObj.isRemote){
			NBTTagCompound plug1 = new NBTTagCompound();
			NBTTagCompound plug2 = new NBTTagCompound();
			
			side.save(plug1);
			nbt.setTag("Plug_1",plug1);
			
			rail.save(plug2);
			nbt.setTag("Plug_2", plug2);
		}
		
		NBTTagCompound item = new NBTTagCompound();
		if(camoItem!=null)camoItem.writeToNBT(item);
		nbt.setTag("CamoItem",item);
	}
	
	   @Override
	   public Packet getDescriptionPacket()
	   {
	       NBTTagCompound syncData = new NBTTagCompound();
	       this.writeSyncableDataToNBT(syncData);
	       return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, syncData);
	   }
	   
	   @Override
	   public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
	   {
	       readSyncableDataFromNBT(pkt.func_148857_g());
	   }
	
	private void writeSyncableDataToNBT(NBTTagCompound syncData){
		NBTTagCompound item = new NBTTagCompound();
		if(camoItem!=null)camoItem.writeToNBT(item);
		syncData.setTag("CamoItem",item);
	}
	
	private void readSyncableDataFromNBT(NBTTagCompound syncData) {
		camoItem=ItemStack.loadItemStackFromNBT((NBTTagCompound) syncData.getTag("CamoItem"));
		if(camoItem !=camoItemOld) updateCamo();
	}
	
	private void forcesync(){
		this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
		this.markDirty();
	}
	
	public void updateEntity(){
		super.updateEntity();
		if (!this.worldObj.isRemote && !this.moving) {
			if(firstupdate){
				Network.joinOrCreateNetwork(this);
				Network.joinNewNetwork(this.rail.node());
				firstupdate=false;
			}
			
			if(Mode==0 || Mode ==2){
				Connector con1=(Connector)side.node();
				Connector con2=(Connector)rail.node();
				if(con2.globalBuffer()<con2.globalBufferSize()){
					double need = con2.globalBufferSize() - con2.globalBuffer();
					double provide = 0.0;
					
					if(need > 100) need = 100;
					
					provide = need + con1.changeBuffer(-need);
					con2.changeBuffer(provide);
				}<commit_msg>Added configoption to set network rail energy transfer speed<commit_after>package mods.ocminecart.common.tileentity;

import li.cil.oc.api.Network;
import li.cil.oc.api.network.Analyzable;
import li.cil.oc.api.network.Connector;
import li.cil.oc.api.network.Environment;
import li.cil.oc.api.network.Message;
import li.cil.oc.api.network.Node;
import li.cil.oc.api.network.SidedEnvironment;
import li.cil.oc.api.network.Visibility;
import mods.ocminecart.OCMinecart;
import mods.ocminecart.Settings;
import mods.ocminecart.common.util.IPlugable;
import mods.ocminecart.common.util.Plug;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.movable.IMovableTile;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@Optional.Interface(iface="appeng.api.movable.IMovableTile", modid="appliedenergistics2", striprefs=true)
public class NetworkRailBaseTile extends TileEntity implements ISidedInventory, SidedEnvironment, IPlugable, Analyzable, IMovableTile{
	
	
	private Plug rail;	//Environment for the Cart
	private Plug side;	//Environment for Cables, Computers, ...
	
	private boolean firstupdate = true; //First call of updateEntity()
	
	private ItemStack camoItem = null;
	private ItemStack camoItemOld = null;

	private IIcon camoTop = null;
	
	/*
	 * 0 = Connect: Network,Power
	 * 1 = Connect: Network
	 * 2 = Connect: Power
	 */
	private int Mode = 0;
	private boolean moving=false;
	
	public NetworkRailBaseTile(){
		if(FMLCommonHandler.instance().getEffectiveSide().isServer()){
			rail = new Plug(this);
			side = new Plug(this);
			
			rail.setNode(Network.newNode(rail, Visibility.Network).withConnector().create());
			side.setNode(Network.newNode(side,Visibility.Network).withConnector(500D).create());
		}
		this.markDirty();
	}
	
	/*---------NBT/Sync--------*/
	public void readFromNBT(NBTTagCompound nbt){
		super.readFromNBT(nbt);
		if(nbt.hasKey("conMode")) Mode = nbt.getInteger("conMode");
		
		camoItem=ItemStack.loadItemStackFromNBT((NBTTagCompound) nbt.getTag("CamoItem"));
		
		if(FMLCommonHandler.instance().getEffectiveSide().isServer()){
			 if(nbt.hasKey("Plug_1"))side.load((NBTTagCompound) nbt.getTag("Plug_1"));
			 if(nbt.hasKey("Plug_2"))rail.load((NBTTagCompound) nbt.getTag("Plug_2"));
		}
		
		this.markDirty();
	}
		
	public void writeToNBT(NBTTagCompound nbt){
		super.writeToNBT(nbt);
		nbt.setInteger("conMode", Mode);
		
		if(!this.worldObj.isRemote){
			NBTTagCompound plug1 = new NBTTagCompound();
			NBTTagCompound plug2 = new NBTTagCompound();
			
			side.save(plug1);
			nbt.setTag("Plug_1",plug1);
			
			rail.save(plug2);
			nbt.setTag("Plug_2", plug2);
		}
		
		NBTTagCompound item = new NBTTagCompound();
		if(camoItem!=null)camoItem.writeToNBT(item);
		nbt.setTag("CamoItem",item);
	}
	
	   @Override
	   public Packet getDescriptionPacket()
	   {
	       NBTTagCompound syncData = new NBTTagCompound();
	       this.writeSyncableDataToNBT(syncData);
	       return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, syncData);
	   }
	   
	   @Override
	   public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
	   {
	       readSyncableDataFromNBT(pkt.func_148857_g());
	   }
	
	private void writeSyncableDataToNBT(NBTTagCompound syncData){
		NBTTagCompound item = new NBTTagCompound();
		if(camoItem!=null)camoItem.writeToNBT(item);
		syncData.setTag("CamoItem",item);
	}
	
	private void readSyncableDataFromNBT(NBTTagCompound syncData) {
		camoItem=ItemStack.loadItemStackFromNBT((NBTTagCompound) syncData.getTag("CamoItem"));
		if(camoItem !=camoItemOld) updateCamo();
	}
	
	private void forcesync(){
		this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
		this.markDirty();
	}
	
	public void updateEntity(){
		super.updateEntity();
		if (!this.worldObj.isRemote && !this.moving) {
			if(firstupdate){
				Network.joinOrCreateNetwork(this);
				Network.joinNewNetwork(this.rail.node());
				firstupdate=false;
			}
			
			if(Mode==0 || Mode ==2){
				Connector con1=(Connector)side.node();
				Connector con2=(Connector)rail.node();
				if(con2.globalBuffer()<con2.globalBufferSize()){
					double need = con2.globalBufferSize() - con2.globalBuffer();
					double provide = 0.0;
					
					if(need > Settings.NetRailPowerTransfer) need = Settings.NetRailPowerTransfer;
					
					provide = need + con1.changeBuffer(-need);
					con2.changeBuffer(provide);
				}
{'fraction_non_alphanumeric': 0.13042162598345775, 'fraction_numerical': 0.00968327617510591, 'mean_word_length': 4.734528629265472, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7393528', 'n_tokens_mistral': 3785, 'n_tokens_neox': 3379, 'n_words': 540}