Rewrite cable logic to use FE.
Fixes #1077 Fixes #1071 Fixes #1067 Fixes #1070 Fixes #1059 Fixes #1044 Fixes #1041 Fixes #1025 Fixes #1006 Fixes #990 Fixes #987 Fixes #962 Fixes #955 Fixes #897
This commit is contained in:
parent
541d3096a0
commit
aee7ca9f14
5 changed files with 86 additions and 596 deletions
|
@ -29,14 +29,10 @@ import net.minecraft.init.MobEffects;
|
||||||
import net.minecraft.inventory.EntityEquipmentSlot;
|
import net.minecraft.inventory.EntityEquipmentSlot;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
|
||||||
import net.minecraftforge.event.world.WorldEvent;
|
|
||||||
import net.minecraftforge.fml.common.eventhandler.EventPriority;
|
import net.minecraftforge.fml.common.eventhandler.EventPriority;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||||
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||||
import techreborn.init.ModItems;
|
import techreborn.init.ModItems;
|
||||||
import techreborn.power.PowerDestroyEvent;
|
|
||||||
import techreborn.power.PowerTickEvent;
|
|
||||||
|
|
||||||
public class TRTickHandler {
|
public class TRTickHandler {
|
||||||
|
|
||||||
|
@ -56,32 +52,4 @@ public class TRTickHandler {
|
||||||
previouslyWearing = chestslot;
|
previouslyWearing = chestslot;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ticksCounted = 0;
|
|
||||||
long tickTime = 0;
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public void worldTick(TickEvent.WorldTickEvent e) {
|
|
||||||
if (e.world.isRemote) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
long start = System.nanoTime();
|
|
||||||
e.world.theProfiler.startSection("TechRebornPowerNet");
|
|
||||||
MinecraftForge.EVENT_BUS.post(new PowerTickEvent(e.world));
|
|
||||||
e.world.theProfiler.endSection();
|
|
||||||
long elapsed = System.nanoTime() - start;
|
|
||||||
tickTime += elapsed;
|
|
||||||
ticksCounted++;
|
|
||||||
if (ticksCounted == 20 * 5) {
|
|
||||||
//Enable this this line to get some infomation when debuging.
|
|
||||||
//System.out.println("Average powernet tick time over last 5 seconds: " + (tickTime / ticksCounted) + "ns");
|
|
||||||
ticksCounted = 0;
|
|
||||||
tickTime = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public void unload(WorldEvent.Unload event){
|
|
||||||
MinecraftForge.EVENT_BUS.post(new PowerDestroyEvent(event.getWorld()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,11 +32,13 @@ import net.minecraft.block.properties.PropertyEnum;
|
||||||
import net.minecraft.block.state.BlockStateContainer;
|
import net.minecraft.block.state.BlockStateContainer;
|
||||||
import net.minecraft.block.state.IBlockState;
|
import net.minecraft.block.state.IBlockState;
|
||||||
import net.minecraft.entity.Entity;
|
import net.minecraft.entity.Entity;
|
||||||
import net.minecraft.entity.EntityLivingBase;
|
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.*;
|
import net.minecraft.util.BlockRenderLayer;
|
||||||
|
import net.minecraft.util.EnumFacing;
|
||||||
|
import net.minecraft.util.ITickable;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraft.util.math.AxisAlignedBB;
|
import net.minecraft.util.math.AxisAlignedBB;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.text.TextFormatting;
|
import net.minecraft.util.text.TextFormatting;
|
||||||
|
@ -45,7 +47,9 @@ import net.minecraftforge.common.property.ExtendedBlockState;
|
||||||
import net.minecraftforge.common.property.IExtendedBlockState;
|
import net.minecraftforge.common.property.IExtendedBlockState;
|
||||||
import net.minecraftforge.common.property.IUnlistedProperty;
|
import net.minecraftforge.common.property.IUnlistedProperty;
|
||||||
import net.minecraftforge.common.property.Properties;
|
import net.minecraftforge.common.property.Properties;
|
||||||
import reborncore.api.power.IEnergyInterfaceTile;
|
import net.minecraftforge.energy.CapabilityEnergy;
|
||||||
|
import net.minecraftforge.energy.IEnergyStorage;
|
||||||
|
import reborncore.common.RebornCoreConfig;
|
||||||
import reborncore.common.misc.Functions;
|
import reborncore.common.misc.Functions;
|
||||||
import reborncore.common.misc.vecmath.Vecs3dCube;
|
import reborncore.common.misc.vecmath.Vecs3dCube;
|
||||||
import reborncore.common.util.WorldUtils;
|
import reborncore.common.util.WorldUtils;
|
||||||
|
@ -53,12 +57,8 @@ import reborncore.mcmultipart.MCMultiPartMod;
|
||||||
import reborncore.mcmultipart.microblock.IMicroblock;
|
import reborncore.mcmultipart.microblock.IMicroblock;
|
||||||
import reborncore.mcmultipart.multipart.*;
|
import reborncore.mcmultipart.multipart.*;
|
||||||
import reborncore.mcmultipart.raytrace.PartMOP;
|
import reborncore.mcmultipart.raytrace.PartMOP;
|
||||||
import techreborn.config.ConfigTechReborn;
|
|
||||||
import techreborn.init.ModSounds;
|
|
||||||
import techreborn.parts.TechRebornParts;
|
import techreborn.parts.TechRebornParts;
|
||||||
import techreborn.parts.walia.IPartWaliaProvider;
|
import techreborn.parts.walia.IPartWaliaProvider;
|
||||||
import techreborn.power.TRPowerNet;
|
|
||||||
import techreborn.utils.damageSources.ElectrialShockSource;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ import java.util.*;
|
||||||
* Created by modmuss50 on 02/03/2016.
|
* Created by modmuss50 on 02/03/2016.
|
||||||
*/
|
*/
|
||||||
public abstract class CableMultipart extends Multipart
|
public abstract class CableMultipart extends Multipart
|
||||||
implements INormallyOccludingPart, ISlottedPart, ITickable, ICableType, IPartWaliaProvider {
|
implements INormallyOccludingPart, ISlottedPart, ITickable, ICableType, IPartWaliaProvider, IEnergyStorage {
|
||||||
|
|
||||||
public static final IUnlistedProperty<Boolean> UP = Properties.toUnlisted(PropertyBool.create("up"));
|
public static final IUnlistedProperty<Boolean> UP = Properties.toUnlisted(PropertyBool.create("up"));
|
||||||
public static final IUnlistedProperty<Boolean> DOWN = Properties.toUnlisted(PropertyBool.create("down"));
|
public static final IUnlistedProperty<Boolean> DOWN = Properties.toUnlisted(PropertyBool.create("down"));
|
||||||
|
@ -81,8 +81,7 @@ public abstract class CableMultipart extends Multipart
|
||||||
public Map<EnumFacing, BlockPos> connectedSides;
|
public Map<EnumFacing, BlockPos> connectedSides;
|
||||||
public int ticks = 0;
|
public int ticks = 0;
|
||||||
public ItemStack stack;
|
public ItemStack stack;
|
||||||
public TRPowerNet mergeWith = null;
|
public int power = 0;
|
||||||
private TRPowerNet network;
|
|
||||||
|
|
||||||
public CableMultipart() {
|
public CableMultipart() {
|
||||||
connectedSides = new HashMap<>();
|
connectedSides = new HashMap<>();
|
||||||
|
@ -168,7 +167,6 @@ public abstract class CableMultipart extends Multipart
|
||||||
@Override
|
@Override
|
||||||
public void onRemoved() {
|
public void onRemoved() {
|
||||||
super.onRemoved();
|
super.onRemoved();
|
||||||
removeFromNetwork();
|
|
||||||
for (EnumFacing dir : EnumFacing.VALUES) {
|
for (EnumFacing dir : EnumFacing.VALUES) {
|
||||||
CableMultipart multipart = getPartFromWorld(getWorld(), getPos().offset(dir), dir);
|
CableMultipart multipart = getPartFromWorld(getWorld(), getPos().offset(dir), dir);
|
||||||
if (multipart != null) {
|
if (multipart != null) {
|
||||||
|
@ -177,12 +175,6 @@ public abstract class CableMultipart extends Multipart
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onUnloaded() {
|
|
||||||
super.onUnloaded();
|
|
||||||
removeFromNetwork();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addOcclusionBoxes(List<AxisAlignedBB> list) {
|
public void addOcclusionBoxes(List<AxisAlignedBB> list) {
|
||||||
for (EnumFacing dir : EnumFacing.VALUES) {
|
for (EnumFacing dir : EnumFacing.VALUES) {
|
||||||
|
@ -209,10 +201,7 @@ public abstract class CableMultipart extends Multipart
|
||||||
part.checkConnectedSides();
|
part.checkConnectedSides();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (network == null) {
|
|
||||||
findAndJoinNetwork(getWorld(), getPos());
|
|
||||||
}
|
|
||||||
TRPowerNet.buildEndpoint(network);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -220,26 +209,6 @@ public abstract class CableMultipart extends Multipart
|
||||||
checkConnectedSides();
|
checkConnectedSides();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tryNetworkMerge(EnumFacing dir){
|
|
||||||
if (dir != null) {
|
|
||||||
if (internalShouldConnectTo(dir)) {
|
|
||||||
CableMultipart cableMultipart = getPartFromWorld(getWorld(), getPos().offset(dir), dir);
|
|
||||||
if (cableMultipart != null && cableMultipart.internalShouldConnectTo(dir.getOpposite())) {
|
|
||||||
if(network == null && cableMultipart.network != null){
|
|
||||||
network = cableMultipart.network;
|
|
||||||
network.addElement(this);
|
|
||||||
} else if (network != null && cableMultipart.network != null && network != cableMultipart.network){
|
|
||||||
network = cableMultipart.network.merge(network);
|
|
||||||
network.addElement(this);
|
|
||||||
} else {
|
|
||||||
findAndJoinNetwork(getWorld(), getPos());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public boolean shouldConnectTo(EnumFacing dir) {
|
public boolean shouldConnectTo(EnumFacing dir) {
|
||||||
if (dir != null) {
|
if (dir != null) {
|
||||||
if (internalShouldConnectTo(dir)) {
|
if (internalShouldConnectTo(dir)) {
|
||||||
|
@ -249,8 +218,7 @@ public abstract class CableMultipart extends Multipart
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TileEntity tile = getNeighbourTile(dir);
|
TileEntity tile = getNeighbourTile(dir);
|
||||||
|
if (tile != null && tile.hasCapability(CapabilityEnergy.ENERGY, dir)) {
|
||||||
if (tile instanceof IEnergyInterfaceTile) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -290,7 +258,6 @@ public abstract class CableMultipart extends Multipart
|
||||||
TileEntity te = getNeighbourTile(dir);
|
TileEntity te = getNeighbourTile(dir);
|
||||||
if (shouldConnectTo(dir)) {
|
if (shouldConnectTo(dir)) {
|
||||||
connectedSides.put(dir, te.getPos());
|
connectedSides.put(dir, te.getPos());
|
||||||
tryNetworkMerge(dir);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,18 +274,38 @@ public abstract class CableMultipart extends Multipart
|
||||||
if (getWorld().getTotalWorldTime() % 80 == 0) {
|
if (getWorld().getTotalWorldTime() % 80 == 0) {
|
||||||
checkConnectedSides();
|
checkConnectedSides();
|
||||||
}
|
}
|
||||||
if(ticks == 20){ // Rebuilds the whole network just after the part has been added, we allow for some time to let the world load in.
|
tickPower();
|
||||||
resetNetwork();
|
|
||||||
findAndJoinNetwork(getWorld(), getPos());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (network == null) {
|
}
|
||||||
this.findAndJoinNetwork(getWorld(), getPos());
|
|
||||||
} else {
|
public void tickPower(){
|
||||||
if (mergeWith != null) {
|
for (EnumFacing face : EnumFacing.VALUES) {
|
||||||
network = getNetwork().merge(mergeWith);
|
if (connectedSides.containsKey(face)) {
|
||||||
network.addElement(this);
|
BlockPos offPos = getPos().offset(face);
|
||||||
mergeWith = null;
|
TileEntity tile = getWorld().getTileEntity(offPos);
|
||||||
|
if(tile == null){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (tile.hasCapability(CapabilityEnergy.ENERGY, face.getOpposite())) {
|
||||||
|
IEnergyStorage energy = tile.getCapability(CapabilityEnergy.ENERGY, face.getOpposite());
|
||||||
|
if (energy.canReceive()) {
|
||||||
|
int move = energy.receiveEnergy(Math.min(getCableType().transferRate * RebornCoreConfig.euPerFU, power), false);
|
||||||
|
if (move != 0) {
|
||||||
|
power -= move;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CableMultipart pipe = getPartFromWorld(getWorld(), getPos().offset(face), face);
|
||||||
|
if (pipe != null) {
|
||||||
|
int averPower = (power + pipe.power) / 2;
|
||||||
|
pipe.power = averPower;
|
||||||
|
if(averPower % 2 != 0){
|
||||||
|
averPower ++;
|
||||||
|
}
|
||||||
|
power = averPower;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -355,32 +342,6 @@ public abstract class CableMultipart extends Multipart
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onEntityCollided(Entity entity) {
|
|
||||||
if (getCableType().canKill && entity instanceof EntityLivingBase) {
|
|
||||||
if (network != null) {
|
|
||||||
if (network.getEnergy() != 0) {
|
|
||||||
if (ConfigTechReborn.UninsulatedElectocutionDamage) {
|
|
||||||
if (getCableType() == EnumCableType.HV) {
|
|
||||||
entity.setFire(1);
|
|
||||||
}
|
|
||||||
network.setEnergy(-1);
|
|
||||||
entity.attackEntityFrom(new ElectrialShockSource(), 1F);
|
|
||||||
}
|
|
||||||
if (ConfigTechReborn.UninsulatedElectocutionSound) {
|
|
||||||
getWorld().playSound(null, entity.posX, entity.posY,
|
|
||||||
entity.posZ, ModSounds.CABLE_SHOCK,
|
|
||||||
SoundCategory.BLOCKS, 0.6F, 1F);
|
|
||||||
}
|
|
||||||
if (ConfigTechReborn.UninsulatedElectocutionParticle) {
|
|
||||||
getWorld().spawnParticle(EnumParticleTypes.CRIT, entity.posX, entity.posY, entity.posZ, 0,
|
|
||||||
0, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEntityStanding(Entity entity) {
|
public void onEntityStanding(Entity entity) {
|
||||||
|
@ -392,66 +353,6 @@ public abstract class CableMultipart extends Multipart
|
||||||
return new ItemStack(TechRebornParts.cables, 1, getCableType().ordinal());
|
return new ItemStack(TechRebornParts.cables, 1, getCableType().ordinal());
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void findAndJoinNetwork(World world, BlockPos pos) {
|
|
||||||
for (EnumFacing dir : EnumFacing.VALUES) {
|
|
||||||
CableMultipart cableMultipart = getPartFromWorld(getWorld(), getPos().offset(dir), dir);
|
|
||||||
if (cableMultipart != null && cableMultipart.getCableType() == getCableType()) {
|
|
||||||
TRPowerNet net = cableMultipart.getNetwork();
|
|
||||||
if (net != null) {
|
|
||||||
if(network == null){
|
|
||||||
network = net;
|
|
||||||
network.addElement(this);
|
|
||||||
} else {
|
|
||||||
network = net.merge(network);
|
|
||||||
network.addElement(this);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (network == null) {
|
|
||||||
network = new TRPowerNet(getCableType(), world);
|
|
||||||
network.addElement(this);
|
|
||||||
}
|
|
||||||
for (EnumFacing dir : EnumFacing.VALUES) {
|
|
||||||
TileEntity te = getNeighbourTile(dir);
|
|
||||||
if (te instanceof IEnergyInterfaceTile) {
|
|
||||||
network.addConnection((IEnergyInterfaceTile) te, dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final TRPowerNet getNetwork() {
|
|
||||||
return network;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void setNetwork(TRPowerNet n) {
|
|
||||||
if (n == null) {
|
|
||||||
} else {
|
|
||||||
network = n;
|
|
||||||
network.addElement(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void removeFromNetwork() {
|
|
||||||
if (network == null) {
|
|
||||||
} else
|
|
||||||
network.removeElement(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void rebuildNetwork() {
|
|
||||||
this.removeFromNetwork();
|
|
||||||
this.resetNetwork();
|
|
||||||
this.findAndJoinNetwork(getWorld(), getPos());
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void resetNetwork() {
|
|
||||||
if (network != null) {
|
|
||||||
network.removeElement(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
network = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInfo(List<String> info) {
|
public void addInfo(List<String> info) {
|
||||||
|
@ -471,4 +372,48 @@ public abstract class CableMultipart extends Multipart
|
||||||
public boolean canRenderInLayer(BlockRenderLayer layer) {
|
public boolean canRenderInLayer(BlockRenderLayer layer) {
|
||||||
return layer == BlockRenderLayer.CUTOUT;
|
return layer == BlockRenderLayer.CUTOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int receiveEnergy(int maxReceive, boolean simulate) {
|
||||||
|
if (!canReceive()){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int energyReceived = Math.min(getMaxEnergyStored() - power, Math.min(getCableType().transferRate * RebornCoreConfig.euPerFU, maxReceive));
|
||||||
|
if (!simulate)
|
||||||
|
power += energyReceived;
|
||||||
|
return energyReceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int extractEnergy(int maxExtract, boolean simulate) {
|
||||||
|
if (!canExtract()){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int energyExtracted = Math.min(power, Math.min(getCableType().transferRate * RebornCoreConfig.euPerFU, maxExtract));
|
||||||
|
if (!simulate)
|
||||||
|
power -= energyExtracted;
|
||||||
|
return energyExtracted;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getEnergyStored() {
|
||||||
|
return power;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxEnergyStored() {
|
||||||
|
return getCableType().transferRate * RebornCoreConfig.euPerFU * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canExtract() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canReceive() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
|
||||||
*
|
|
||||||
* Copyright (c) 2017 TechReborn
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package techreborn.power;
|
|
||||||
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
|
||||||
|
|
||||||
public class PowerDestroyEvent extends Event {
|
|
||||||
World world;
|
|
||||||
public PowerDestroyEvent(World world) {
|
|
||||||
this.world = world;
|
|
||||||
}
|
|
||||||
|
|
||||||
public World getWorld() {
|
|
||||||
return world;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
|
||||||
*
|
|
||||||
* Copyright (c) 2017 TechReborn
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package techreborn.power;
|
|
||||||
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
|
||||||
|
|
||||||
public class PowerTickEvent extends Event {
|
|
||||||
World world;
|
|
||||||
public PowerTickEvent(World world) {
|
|
||||||
this.world = world;
|
|
||||||
}
|
|
||||||
|
|
||||||
public World getWorld() {
|
|
||||||
return world;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,345 +0,0 @@
|
||||||
/*
|
|
||||||
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
|
||||||
*
|
|
||||||
* Copyright (c) 2017 TechReborn
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package techreborn.power;
|
|
||||||
|
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraft.util.EnumFacing;
|
|
||||||
import net.minecraft.util.EnumParticleTypes;
|
|
||||||
import net.minecraft.util.math.BlockPos;
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
import net.minecraft.world.WorldServer;
|
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
import reborncore.api.power.IEnergyInterfaceTile;
|
|
||||||
import techreborn.parts.powerCables.CableMultipart;
|
|
||||||
import techreborn.parts.powerCables.EnumCableType;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class TRPowerNet {
|
|
||||||
public ArrayList<EnergyHandler> endpoints = new ArrayList();
|
|
||||||
int tick = 0;
|
|
||||||
EnumCableType cableType;
|
|
||||||
private ArrayList<CableMultipart> cables = new ArrayList();
|
|
||||||
private int energy = 0;
|
|
||||||
private World world;
|
|
||||||
|
|
||||||
|
|
||||||
public TRPowerNet(EnumCableType cableType, World world) {
|
|
||||||
this.cableType = cableType;
|
|
||||||
MinecraftForge.EVENT_BUS.register(this);
|
|
||||||
this.world = world;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void buildEndpoint(TRPowerNet net) {
|
|
||||||
ArrayList<CableMultipart> parts = new ArrayList<>();
|
|
||||||
ArrayList<CableMultipart> partsToMerge = new ArrayList<>();
|
|
||||||
parts.addAll(net.cables);
|
|
||||||
for (CableMultipart cable : parts) {
|
|
||||||
for (EnumFacing facing : EnumFacing.VALUES) {
|
|
||||||
BlockPos pos = cable.getPos().offset(facing);
|
|
||||||
TileEntity tile = cable.getWorld().getTileEntity(pos);
|
|
||||||
if (tile instanceof IEnergyInterfaceTile) {
|
|
||||||
IEnergyInterfaceTile eit = (IEnergyInterfaceTile) tile;
|
|
||||||
net.addConnection(eit, facing);
|
|
||||||
}
|
|
||||||
CableMultipart cableMultipart = CableMultipart.getPartFromWorld(cable.getWorld(), pos, null);
|
|
||||||
if (cableMultipart != null) {
|
|
||||||
if (cableMultipart.getNetwork() != net) {
|
|
||||||
partsToMerge.add(cableMultipart);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (CableMultipart cableMultipart : partsToMerge) {
|
|
||||||
cableMultipart.mergeWith = net;
|
|
||||||
}
|
|
||||||
net.checkAndRemoveOldEndpoints();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getIOLimit() {
|
|
||||||
return cableType.transferRate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public synchronized void tick(PowerTickEvent evt) {
|
|
||||||
|
|
||||||
if (tick < 20) {
|
|
||||||
tick++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (tick % 80 == 0) {
|
|
||||||
for (Iterator<CableMultipart> it = cables.iterator(); it.hasNext(); ) {
|
|
||||||
CableMultipart cableMultipart = it.next();
|
|
||||||
if (cableMultipart.getWorld() == null || cableMultipart.getPos() == null) {
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
CableMultipart mp = cableMultipart.getPartFromWorld(cableMultipart.getWorld(), cableMultipart.getPos(),
|
|
||||||
null);
|
|
||||||
if (mp == null) {
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buildEndpoint(this);
|
|
||||||
}
|
|
||||||
if (!cables.isEmpty()) {
|
|
||||||
ArrayList<EnergyHandler> collectibles = new ArrayList();
|
|
||||||
ArrayList<EnergyHandler> insertibles = new ArrayList();
|
|
||||||
for (EnergyHandler ei : endpoints) {
|
|
||||||
if (ei.isCollectible()) {
|
|
||||||
collectibles.add(ei);
|
|
||||||
}
|
|
||||||
if (ei.isInsertible()) {
|
|
||||||
insertibles.add(ei);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (energy < cableType.transferRate * cables.size()) {
|
|
||||||
for (EnergyHandler handler : collectibles) {
|
|
||||||
energy += handler.collectEnergy(cableType.transferRate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (EnergyHandler handler : insertibles) {
|
|
||||||
energy -= handler.addEnergy(Math.min(energy, cableType.transferRate));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
MinecraftForge.EVENT_BUS.unregister(this);
|
|
||||||
}
|
|
||||||
tick++;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public void destory(PowerDestroyEvent event){
|
|
||||||
if(event.getWorld() == world){
|
|
||||||
world = null;
|
|
||||||
MinecraftForge.EVENT_BUS.unregister(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addElement(CableMultipart te) {
|
|
||||||
synchronized (cables){
|
|
||||||
if (!cables.contains(te)) {
|
|
||||||
cables.add(te);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeElement(CableMultipart te) {
|
|
||||||
synchronized (cables){
|
|
||||||
cables.remove(te);
|
|
||||||
}
|
|
||||||
this.rebuild();
|
|
||||||
this.checkAndRemoveOldEndpoints();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void checkAndRemoveOldEndpoints() {
|
|
||||||
List<EnergyHandler> deadHandlers = new ArrayList<>();
|
|
||||||
for (EnergyHandler energyHandler : endpoints) {
|
|
||||||
TileEntity tile = (TileEntity) energyHandler.tile;
|
|
||||||
if (tile.getWorld().getTileEntity(tile.getPos()) == null) {
|
|
||||||
deadHandlers.add(energyHandler);
|
|
||||||
} else {
|
|
||||||
boolean hasNet = false;
|
|
||||||
for (EnumFacing facing : EnumFacing.VALUES) {
|
|
||||||
BlockPos pos = tile.getPos().offset(facing);
|
|
||||||
CableMultipart multipart = CableMultipart.getPartFromWorld(tile.getWorld(), pos, facing);
|
|
||||||
if (multipart != null && multipart.getNetwork() == this) {
|
|
||||||
hasNet = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hasNet) {
|
|
||||||
deadHandlers.add(energyHandler);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
for (Iterator<EnergyHandler> it = endpoints.iterator(); it.hasNext(); ) {
|
|
||||||
EnergyHandler energyHandler = it.next();
|
|
||||||
if (deadHandlers.contains(energyHandler)) {
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rebuild() {
|
|
||||||
for (int i = 0; i < cables.size(); i++) {
|
|
||||||
CableMultipart te = cables.get(i);
|
|
||||||
te.setNetwork(null);
|
|
||||||
te.findAndJoinNetwork(te.getWorld(), te.getPos());
|
|
||||||
}
|
|
||||||
this.clear(true);
|
|
||||||
MinecraftForge.EVENT_BUS.unregister(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getEnergy() {
|
|
||||||
return energy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnergy(int energy) {
|
|
||||||
energy += energy;
|
|
||||||
if (energy < 0) {
|
|
||||||
energy = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addConnection(IEnergyInterfaceTile ih, EnumFacing dir) {
|
|
||||||
if (ih instanceof CableMultipart)
|
|
||||||
return;
|
|
||||||
EnergyHandler has = this.getHandleFrom(ih);
|
|
||||||
if (has == null) {
|
|
||||||
endpoints.add(new EnergyHandler(ih, cableType, dir));
|
|
||||||
} else {
|
|
||||||
has.side = dir;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TRPowerNet merge(TRPowerNet n) {
|
|
||||||
if (n != this) {
|
|
||||||
ArrayList<CableMultipart> li = new ArrayList();
|
|
||||||
for (int i = 0; i < n.cables.size(); i++) {
|
|
||||||
CableMultipart wire = n.cables.get(i);
|
|
||||||
li.add(wire);
|
|
||||||
}
|
|
||||||
for (EnergyHandler ei : n.endpoints) {
|
|
||||||
endpoints.add(ei);
|
|
||||||
}
|
|
||||||
n.clear(false);
|
|
||||||
for (int i = 0; i < li.size(); i++) {
|
|
||||||
CableMultipart wire = li.get(i);
|
|
||||||
wire.setNetwork(this);
|
|
||||||
}
|
|
||||||
checkAndRemoveOldEndpoints();
|
|
||||||
MinecraftForge.EVENT_BUS.unregister(n);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private EnergyHandler getHandleFrom(IEnergyInterfaceTile tile) {
|
|
||||||
for (EnergyHandler ei : endpoints) {
|
|
||||||
if (ei.tile == tile)
|
|
||||||
return ei;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clear(boolean clearTiles) {
|
|
||||||
if (clearTiles) {
|
|
||||||
for (int i = 0; i < cables.size(); i++) {
|
|
||||||
cables.get(i).resetNetwork();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cables.clear();
|
|
||||||
for (Iterator<TRPowerNet.EnergyHandler> it = endpoints.iterator(); it.hasNext(); ) {
|
|
||||||
it.next();
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
energy = 0;
|
|
||||||
|
|
||||||
MinecraftForge.EVENT_BUS.unregister(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int addEnergy(int maxAdd, boolean simulate) {
|
|
||||||
if (energy >= this.getIOLimit())
|
|
||||||
return 0;
|
|
||||||
maxAdd = Math.min(this.getIOLimit(), maxAdd);
|
|
||||||
if (!simulate)
|
|
||||||
energy += maxAdd;
|
|
||||||
return maxAdd;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class EnergyHandler {
|
|
||||||
private final IEnergyInterfaceTile tile;
|
|
||||||
private final EnumCableType type;
|
|
||||||
private EnumFacing side;
|
|
||||||
|
|
||||||
private EnergyHandler(IEnergyInterfaceTile ih, EnumCableType type, EnumFacing dir) {
|
|
||||||
tile = ih;
|
|
||||||
this.type = type;
|
|
||||||
this.side = dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isInsertible() {
|
|
||||||
return this.getTotalInsertible() > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isCollectible() {
|
|
||||||
return this.getTotalCollectible() > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean contains(IEnergyInterfaceTile tile) {
|
|
||||||
return tile == this.tile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int collectEnergy(int max) {
|
|
||||||
int total = 0;
|
|
||||||
if (tile.canProvideEnergy(side.getOpposite())) {
|
|
||||||
int collect = (int) Math.min(max, Math.min(tile.getMaxOutput(), tile.getEnergy()));
|
|
||||||
total = (int) tile.useEnergy(collect, false);
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int addEnergy(int max) {
|
|
||||||
int total = 0;
|
|
||||||
if (tile.canAcceptEnergy(side.getOpposite()) && max > 0) {
|
|
||||||
if (type.tier.ordinal() > tile.getTier().ordinal() && max > tile.getMaxInput()) {
|
|
||||||
if (tile instanceof TileEntity && ((TileEntity) tile).getWorld() instanceof WorldServer) {
|
|
||||||
WorldServer worldServer = (WorldServer) ((TileEntity) tile).getWorld();
|
|
||||||
if(worldServer.rand.nextInt(5) == 0){
|
|
||||||
double x = (double)((TileEntity) tile).getPos().getX() + worldServer.rand.nextDouble() + (side.getFrontOffsetX() / 2);
|
|
||||||
double y = (double)((TileEntity) tile).getPos().getY() + worldServer.rand.nextDouble() + 1;
|
|
||||||
double z = (double)((TileEntity) tile).getPos().getZ() + worldServer.rand.nextDouble()+ (side.getFrontOffsetZ() / 2);
|
|
||||||
worldServer.spawnParticle(EnumParticleTypes.SMOKE_LARGE, x, y, z,1, 0.0D, 0.0D, 0.0D, 0D);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int add = max - total;
|
|
||||||
total += tile.addEnergy(add, false);
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTotalCollectible() {
|
|
||||||
if (tile.canProvideEnergy(side.getOpposite()) && tile.getEnergy() != 0) {
|
|
||||||
return (int) Math.min(tile.getMaxOutput(), tile.getEnergy());
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTotalInsertible() {
|
|
||||||
int total = 0;
|
|
||||||
if (tile.canAcceptEnergy(side.getOpposite()) && tile.getMaxPower() - tile.getEnergy() != 0) {
|
|
||||||
total += tile.addEnergy(type.transferRate, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue