Restored more P2P
This commit is contained in:
@@ -22,6 +22,7 @@ import java.util.function.Function;
|
||||
|
||||
import appeng.fluids.parts.*;
|
||||
import appeng.parts.automation.*;
|
||||
import appeng.parts.p2p.*;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import appeng.api.definitions.IItemDefinition;
|
||||
@@ -137,26 +138,21 @@ public final class ApiParts implements IParts {
|
||||
ConversionMonitorPart::new);
|
||||
this.iface = createPart("cable_interface", InterfacePart.class, InterfacePart::new);
|
||||
this.fluidIface = createPart("cable_fluid_interface", FluidInterfacePart.class, FluidInterfacePart::new);
|
||||
// FIXME FABRIC this.p2PTunnelME = createPart("me_p2p_tunnel", MEP2PTunnelPart.class, MEP2PTunnelPart::new);
|
||||
// FIXME FABRIC this.p2PTunnelRedstone = createPart("redstone_p2p_tunnel", RedstoneP2PTunnelPart.class,
|
||||
// FIXME FABRIC RedstoneP2PTunnelPart::new);
|
||||
// FIXME FABRIC this.p2PTunnelItems = createPart("item_p2p_tunnel", ItemP2PTunnelPart.class, ItemP2PTunnelPart::new);
|
||||
// FIXME FABRIC this.p2PTunnelFluids = createPart("fluid_p2p_tunnel", FluidP2PTunnelPart.class, FluidP2PTunnelPart::new);
|
||||
this.p2PTunnelME = createPart("me_p2p_tunnel", MEP2PTunnelPart.class, MEP2PTunnelPart::new);
|
||||
this.p2PTunnelRedstone = createPart("redstone_p2p_tunnel", RedstoneP2PTunnelPart.class,
|
||||
RedstoneP2PTunnelPart::new);
|
||||
this.p2PTunnelItems = createPart("item_p2p_tunnel", ItemP2PTunnelPart.class, ItemP2PTunnelPart::new);
|
||||
this.p2PTunnelFluids = createPart("fluid_p2p_tunnel", FluidP2PTunnelPart.class, FluidP2PTunnelPart::new);
|
||||
// FIXME FABRIC this.p2PTunnelEU = null; // FIXME createPart( "ic2_p2p_tunnel", PartType.P2P_TUNNEL_IC2,
|
||||
// FIXME FABRIC // PartP2PIC2Power.class, PartP2PIC2Power::new);
|
||||
// FIXME FABRIC this.p2PTunnelFE = createPart("fe_p2p_tunnel", FEP2PTunnelPart.class, FEP2PTunnelPart::new);
|
||||
// FIXME FABRIC this.p2PTunnelLight = createPart("light_p2p_tunnel", LightP2PTunnelPart.class, LightP2PTunnelPart::new);
|
||||
this.p2PTunnelLight = createPart("light_p2p_tunnel", LightP2PTunnelPart.class, LightP2PTunnelPart::new);
|
||||
this.interfaceTerminal = createPart("interface_terminal", InterfaceTerminalPart.class,
|
||||
InterfaceTerminalPart::new);
|
||||
this.fluidTerminal = createPart("fluid_terminal", FluidTerminalPart.class, FluidTerminalPart::new);
|
||||
|
||||
this.p2PTunnelME = null;
|
||||
this.p2PTunnelRedstone = null;
|
||||
this.p2PTunnelItems = null;
|
||||
this.p2PTunnelFluids = null;
|
||||
this.p2PTunnelEU = null;
|
||||
this.p2PTunnelFE = null;
|
||||
this.p2PTunnelLight = null;
|
||||
|
||||
this.registry = null;
|
||||
this.partModels = null;
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.parts.p2p;
|
||||
|
||||
import alexiil.mc.lib.attributes.AttributeList;
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidAttributes;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidInsertable;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidVolumeUtil;
|
||||
import alexiil.mc.lib.attributes.fluid.amount.FluidAmount;
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidKey;
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidVolume;
|
||||
import appeng.api.parts.IPartModel;
|
||||
import appeng.items.parts.PartModels;
|
||||
import appeng.me.GridAccessException;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.BlockView;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class FluidP2PTunnelPart extends P2PTunnelPart<FluidP2PTunnelPart> implements FluidInsertable {
|
||||
|
||||
private static final P2PModels MODELS = new P2PModels("part/p2p/p2p_tunnel_fluids");
|
||||
|
||||
private static final ThreadLocal<Deque<FluidP2PTunnelPart>> DEPTH = new ThreadLocal<>();;
|
||||
|
||||
private FluidInsertable cachedTank;
|
||||
|
||||
public FluidP2PTunnelPart(final ItemStack is) {
|
||||
super(is);
|
||||
}
|
||||
|
||||
@PartModels
|
||||
public static List<IPartModel> getModels() {
|
||||
return MODELS.getModels();
|
||||
}
|
||||
|
||||
public float getPowerDrainPerTick() {
|
||||
return 2.0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTunnelNetworkChange() {
|
||||
this.cachedTank = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onneighborUpdate(BlockView w, BlockPos pos, BlockPos neighbor) {
|
||||
this.cachedTank = null;
|
||||
|
||||
if (this.isOutput()) {
|
||||
final FluidP2PTunnelPart in = this.getInput();
|
||||
if (in != null) {
|
||||
in.onTunnelNetworkChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAllAttributes(AttributeList<?> to) {
|
||||
to.offer(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartModel getStaticModels() {
|
||||
return MODELS.getModel(this.isPowered(), this.isActive());
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidVolume attemptInsertion(FluidVolume fluid, Simulation simulation) {
|
||||
|
||||
final Deque<FluidP2PTunnelPart> stack = this.getDepth();
|
||||
|
||||
for (final FluidP2PTunnelPart t : stack) {
|
||||
if (t == this) {
|
||||
return fluid;
|
||||
}
|
||||
}
|
||||
|
||||
stack.push(this);
|
||||
|
||||
final List<FluidP2PTunnelPart> list = this.getOutputs(fluid.getFluidKey());
|
||||
FluidAmount requestTotal = FluidAmount.ZERO;
|
||||
|
||||
Iterator<FluidP2PTunnelPart> i = list.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
final FluidP2PTunnelPart l = i.next();
|
||||
final FluidInsertable tank = l.getTarget();
|
||||
FluidAmount inserted;
|
||||
if (tank != null) {
|
||||
inserted = fluid.amount().sub(tank.attemptInsertion(fluid.copy(), Simulation.SIMULATE).amount());
|
||||
} else {
|
||||
inserted = FluidAmount.ZERO;
|
||||
}
|
||||
|
||||
if (inserted.isZero()) {
|
||||
i.remove();
|
||||
} else {
|
||||
requestTotal = requestTotal.add(inserted);
|
||||
}
|
||||
}
|
||||
|
||||
if (requestTotal.isZero()) {
|
||||
if (stack.pop() != this) {
|
||||
throw new IllegalStateException("Invalid Recursion detected.");
|
||||
}
|
||||
|
||||
return fluid;
|
||||
}
|
||||
|
||||
if (simulation != Simulation.ACTION) {
|
||||
if (stack.pop() != this) {
|
||||
throw new IllegalStateException("Invalid Recursion detected.");
|
||||
}
|
||||
|
||||
return fluid.copy().split(fluid.amount().min(requestTotal));
|
||||
}
|
||||
|
||||
FluidAmount remaining = fluid.amount();
|
||||
|
||||
i = list.iterator();
|
||||
|
||||
while (i.hasNext() && !remaining.isZero()) {
|
||||
final FluidP2PTunnelPart l = i.next();
|
||||
|
||||
FluidVolume insert = fluid.withAmount(
|
||||
fluid.amount().div(list.size())
|
||||
);
|
||||
if (insert.amount().isGreaterThan(remaining)) {
|
||||
insert = insert.withAmount(remaining);
|
||||
}
|
||||
|
||||
FluidInsertable tank = l.getTarget();
|
||||
if (tank != null) {
|
||||
remaining = remaining.sub(insert.amount().sub(tank.attemptInsertion(insert, Simulation.ACTION).amount()));
|
||||
}
|
||||
}
|
||||
|
||||
if (stack.pop() != this) {
|
||||
throw new IllegalStateException("Invalid Recursion detected.");
|
||||
}
|
||||
|
||||
return remaining.isZero() ? FluidVolumeUtil.EMPTY : fluid.withAmount(remaining);
|
||||
}
|
||||
|
||||
private Deque<FluidP2PTunnelPart> getDepth() {
|
||||
Deque<FluidP2PTunnelPart> s = DEPTH.get();
|
||||
|
||||
if (s == null) {
|
||||
DEPTH.set(s = new ArrayDeque<>());
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
private List<FluidP2PTunnelPart> getOutputs(final FluidKey input) {
|
||||
final List<FluidP2PTunnelPart> outs = new ArrayList<>();
|
||||
|
||||
try {
|
||||
for (final FluidP2PTunnelPart l : this.getOutputs()) {
|
||||
final FluidInsertable handler = l.getTarget();
|
||||
|
||||
if (handler != null) {
|
||||
outs.add(l);
|
||||
}
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
|
||||
return outs;
|
||||
}
|
||||
|
||||
private FluidInsertable getTarget() {
|
||||
if (!this.getProxy().isActive()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.cachedTank != null) {
|
||||
return this.cachedTank;
|
||||
}
|
||||
|
||||
return this.cachedTank = FluidAttributes.INSERTABLE.getFirstOrNullFromNeighbour(this.getTile(), this.getSide().getFacing());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.parts.p2p;
|
||||
|
||||
import alexiil.mc.lib.attributes.AttributeList;
|
||||
import alexiil.mc.lib.attributes.CacheInfo;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.ItemAttributes;
|
||||
import alexiil.mc.lib.attributes.item.impl.CombinedFixedItemInv;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.parts.IPartModel;
|
||||
import appeng.core.settings.TickRates;
|
||||
import appeng.items.parts.PartModels;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.cache.helpers.TunnelCollection;
|
||||
import appeng.util.Platform;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.world.BlockView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ItemP2PTunnelPart extends P2PTunnelPart<ItemP2PTunnelPart> implements IGridTickable {
|
||||
private static final float POWER_DRAIN = 2.0f;
|
||||
private static final P2PModels MODELS = new P2PModels("part/p2p/p2p_tunnel_items");
|
||||
private boolean partVisited = false;
|
||||
|
||||
@PartModels
|
||||
public static List<IPartModel> getModels() {
|
||||
return MODELS.getModels();
|
||||
}
|
||||
|
||||
private int oldSize = 0;
|
||||
private boolean requested;
|
||||
private FixedItemInv cachedInv;
|
||||
private List<FixedItemInv> cachedInvs = new ArrayList<>();
|
||||
|
||||
public ItemP2PTunnelPart(final ItemStack is) {
|
||||
super(is);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onneighborUpdate(BlockView w, BlockPos pos, BlockPos neighbor) {
|
||||
this.cachedInv = null;
|
||||
this.cachedInvs.clear();
|
||||
final ItemP2PTunnelPart input = this.getInput();
|
||||
if (input != null && this.isOutput()) {
|
||||
input.onTunnelNetworkChange();
|
||||
}
|
||||
}
|
||||
|
||||
private FixedItemInv getDestination() {
|
||||
this.requested = true;
|
||||
|
||||
if (this.cachedInv != null) {
|
||||
return this.cachedInv;
|
||||
}
|
||||
|
||||
final List<FixedItemInv> outs = new ArrayList<FixedItemInv>();
|
||||
final TunnelCollection<ItemP2PTunnelPart> itemTunnels;
|
||||
|
||||
try {
|
||||
itemTunnels = this.getOutputs();
|
||||
} catch (final GridAccessException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (final ItemP2PTunnelPart t : itemTunnels) {
|
||||
final FixedItemInv inv = t.getOutputInv();
|
||||
if (inv != null && inv != this) {
|
||||
if (Platform.getRandomInt() % 2 == 0) {
|
||||
outs.add(inv);
|
||||
} else {
|
||||
outs.add(0, inv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.cachedInvs = outs;
|
||||
return this.cachedInv = new CombinedFixedItemInv<>(outs);
|
||||
}
|
||||
|
||||
private FixedItemInv getOutputInv() {
|
||||
FixedItemInv ret = null;
|
||||
if (!this.partVisited) {
|
||||
this.partVisited = true;
|
||||
if (this.getProxy().isActive()) {
|
||||
final Direction facing = this.getSide().getFacing();
|
||||
ret = ItemAttributes.FIXED_INV.getFirstOrNullFromNeighbour(
|
||||
this.getTile(),
|
||||
facing
|
||||
);
|
||||
}
|
||||
this.partVisited = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
return new TickingRequest(TickRates.ItemTunnel.getMin(), TickRates.ItemTunnel.getMax(), false, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, final int ticksSinceLastCall) {
|
||||
final boolean wasReq = this.requested;
|
||||
|
||||
if (this.requested && !this.cachedInvs.isEmpty()) {
|
||||
// Don't modify the old list
|
||||
this.cachedInvs = new ArrayList<>(this.cachedInvs);
|
||||
FixedItemInv lastInv = this.cachedInvs.remove(this.cachedInvs.size() - 1);
|
||||
this.cachedInvs.add(0, lastInv);
|
||||
this.cachedInv = new CombinedFixedItemInv<>(this.cachedInvs);
|
||||
}
|
||||
|
||||
this.requested = false;
|
||||
return wasReq ? TickRateModulation.FASTER : TickRateModulation.SLOWER;
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void changeStateA(final MENetworkBootingStatusChange bs) {
|
||||
if (!this.isOutput()) {
|
||||
this.cachedInv = null;
|
||||
final int olderSize = this.oldSize;
|
||||
this.oldSize = this.getDestination().getSlotCount();
|
||||
if (olderSize != this.oldSize) {
|
||||
this.getHost().notifyNeighbors();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void changeStateB(final MENetworkChannelsChanged bs) {
|
||||
if (!this.isOutput()) {
|
||||
this.cachedInv = null;
|
||||
final int olderSize = this.oldSize;
|
||||
this.oldSize = this.getDestination().getSlotCount();
|
||||
if (olderSize != this.oldSize) {
|
||||
this.getHost().notifyNeighbors();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void changeStateC(final MENetworkPowerStatusChange bs) {
|
||||
if (!this.isOutput()) {
|
||||
this.cachedInv = null;
|
||||
final int olderSize = this.oldSize;
|
||||
this.oldSize = this.getDestination().getSlotCount();
|
||||
if (olderSize != this.oldSize) {
|
||||
this.getHost().notifyNeighbors();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTunnelNetworkChange() {
|
||||
if (!this.isOutput()) {
|
||||
this.cachedInv = null;
|
||||
final int olderSize = this.oldSize;
|
||||
this.oldSize = this.getDestination().getSlotCount();
|
||||
if (olderSize != this.oldSize) {
|
||||
this.getHost().notifyNeighbors();
|
||||
}
|
||||
} else {
|
||||
final ItemP2PTunnelPart input = this.getInput();
|
||||
if (input != null) {
|
||||
input.getHost().notifyNeighbors();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAllAttributes(AttributeList<?> to) {
|
||||
to.offer(getDestination(), CacheInfo.NOT_CACHABLE);
|
||||
}
|
||||
|
||||
public float getPowerDrainPerTick() {
|
||||
return POWER_DRAIN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartModel getStaticModels() {
|
||||
return MODELS.getModel(this.isPowered(), this.isActive());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.parts.p2p;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.BlockView;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.parts.IPartModel;
|
||||
import appeng.core.settings.TickRates;
|
||||
import appeng.items.parts.PartModels;
|
||||
import appeng.me.GridAccessException;
|
||||
|
||||
public class LightP2PTunnelPart extends P2PTunnelPart<LightP2PTunnelPart> implements IGridTickable {
|
||||
|
||||
private static final P2PModels MODELS = new P2PModels("part/p2p/p2p_tunnel_light");
|
||||
|
||||
@PartModels
|
||||
public static List<IPartModel> getModels() {
|
||||
return MODELS.getModels();
|
||||
}
|
||||
|
||||
private int lastValue = 0;
|
||||
private int opacity = -1;
|
||||
|
||||
public LightP2PTunnelPart(final ItemStack is) {
|
||||
super(is);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chanRender(final MENetworkChannelsChanged c) {
|
||||
this.onTunnelNetworkChange();
|
||||
super.chanRender(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void powerRender(final MENetworkPowerStatusChange c) {
|
||||
this.onTunnelNetworkChange();
|
||||
super.powerRender(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeInt(this.isOutput() ? this.lastValue : 0);
|
||||
data.writeInt(this.opacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
super.readFromStream(data);
|
||||
final int oldValue = this.lastValue;
|
||||
final int oldOpacity = this.opacity;
|
||||
|
||||
this.lastValue = data.readInt();
|
||||
this.opacity = data.readInt();
|
||||
|
||||
this.setOutput(this.lastValue > 0);
|
||||
return this.lastValue != oldValue || oldOpacity != this.opacity;
|
||||
}
|
||||
|
||||
private boolean doWork() {
|
||||
if (this.isOutput()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final BlockEntity te = this.getTile();
|
||||
final World w = te.getWorld();
|
||||
|
||||
final int newLevel = w.getLightLevel(te.getPos().offset(this.getSide().getFacing()));
|
||||
|
||||
if (this.lastValue != newLevel && this.getProxy().isActive()) {
|
||||
this.lastValue = newLevel;
|
||||
try {
|
||||
for (final LightP2PTunnelPart out : this.getOutputs()) {
|
||||
out.setLightLevel(this.lastValue);
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onneighborUpdate(BlockView w, BlockPos pos, BlockPos neighbor) {
|
||||
if (this.isOutput() && pos.offset(this.getSide().getFacing()).equals(neighbor)) {
|
||||
this.opacity = -1;
|
||||
this.getHost().markForUpdate();
|
||||
} else {
|
||||
this.doWork();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLightLevel() {
|
||||
if (this.isOutput() && this.isPowered()) {
|
||||
return this.blockLight(this.lastValue);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void setLightLevel(final int out) {
|
||||
this.lastValue = out;
|
||||
this.getHost().markForUpdate();
|
||||
}
|
||||
|
||||
private int blockLight(final int emit) {
|
||||
if (this.opacity < 0) {
|
||||
final BlockEntity te = this.getTile();
|
||||
this.opacity = 255 - te.getWorld().getLightLevel(te.getPos().offset(this.getSide().getFacing()));
|
||||
}
|
||||
|
||||
return (int) (emit * (this.opacity / 255.0f));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(final CompoundTag tag) {
|
||||
super.readFromNBT(tag);
|
||||
this.lastValue = tag.getInt("lastValue");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(final CompoundTag tag) {
|
||||
super.writeToNBT(tag);
|
||||
tag.putInt("lastValue", this.lastValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTunnelConfigChange() {
|
||||
this.onTunnelNetworkChange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTunnelNetworkChange() {
|
||||
if (this.isOutput()) {
|
||||
final LightP2PTunnelPart src = this.getInput();
|
||||
if (src != null && src.getProxy().isActive()) {
|
||||
this.setLightLevel(src.lastValue);
|
||||
} else {
|
||||
this.getHost().markForUpdate();
|
||||
}
|
||||
} else {
|
||||
this.doWork();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
return new TickingRequest(TickRates.LightTunnel.getMin(), TickRates.LightTunnel.getMax(), false, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, final int ticksSinceLastCall) {
|
||||
return this.doWork() ? TickRateModulation.URGENT : TickRateModulation.SLOWER;
|
||||
}
|
||||
|
||||
public float getPowerDrainPerTick() {
|
||||
return 0.5f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartModel getStaticModels() {
|
||||
return MODELS.getModel(this.isPowered(), this.isActive());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.parts.p2p;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.RedstoneWireBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.BlockView;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.parts.IPartModel;
|
||||
import appeng.items.parts.PartModels;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class RedstoneP2PTunnelPart extends P2PTunnelPart<RedstoneP2PTunnelPart> {
|
||||
|
||||
private static final P2PModels MODELS = new P2PModels("part/p2p/p2p_tunnel_redstone");
|
||||
|
||||
@PartModels
|
||||
public static List<IPartModel> getModels() {
|
||||
return MODELS.getModels();
|
||||
}
|
||||
|
||||
private int power;
|
||||
private boolean recursive = false;
|
||||
|
||||
public RedstoneP2PTunnelPart(final ItemStack is) {
|
||||
super(is);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void changeStateA(final MENetworkBootingStatusChange bs) {
|
||||
this.setNetworkReady();
|
||||
}
|
||||
|
||||
private void setNetworkReady() {
|
||||
if (this.isOutput()) {
|
||||
final RedstoneP2PTunnelPart in = this.getInput();
|
||||
if (in != null) {
|
||||
this.putInput(in.power);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void putInput(final Object o) {
|
||||
if (this.recursive) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.recursive = true;
|
||||
if (this.isOutput() && this.getProxy().isActive()) {
|
||||
final int newPower = (Integer) o;
|
||||
if (this.power != newPower) {
|
||||
this.power = newPower;
|
||||
this.notifyNeighbors();
|
||||
}
|
||||
}
|
||||
this.recursive = false;
|
||||
}
|
||||
|
||||
private void notifyNeighbors() {
|
||||
final World world = this.getTile().getWorld();
|
||||
|
||||
Platform.notifyBlocksOfNeighbors(world, this.getTile().getPos());
|
||||
|
||||
// and this cause sometimes it can go thought walls.
|
||||
for (final Direction face : Direction.values()) {
|
||||
Platform.notifyBlocksOfNeighbors(world, this.getTile().getPos().offset(face));
|
||||
}
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void changeStateB(final MENetworkChannelsChanged bs) {
|
||||
this.setNetworkReady();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void changeStateC(final MENetworkPowerStatusChange bs) {
|
||||
this.setNetworkReady();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(final CompoundTag tag) {
|
||||
super.readFromNBT(tag);
|
||||
this.power = tag.getInt("power");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(final CompoundTag tag) {
|
||||
super.writeToNBT(tag);
|
||||
tag.putInt("power", this.power);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTunnelNetworkChange() {
|
||||
this.setNetworkReady();
|
||||
}
|
||||
|
||||
public float getPowerDrainPerTick() {
|
||||
return 0.5f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onneighborUpdate(BlockView w, BlockPos pos, BlockPos neighbor) {
|
||||
if (!this.isOutput()) {
|
||||
final BlockPos target = this.getTile().getPos().offset(this.getSide().getFacing());
|
||||
|
||||
final BlockState state = this.getTile().getWorld().getBlockState(target);
|
||||
final Block b = state.getBlock();
|
||||
if (b != null && !this.isOutput()) {
|
||||
Direction srcSide = this.getSide().getFacing();
|
||||
if (b instanceof RedstoneWireBlock) {
|
||||
srcSide = Direction.UP;
|
||||
}
|
||||
|
||||
this.power = b.getWeakRedstonePower(state, this.getTile().getWorld(), target, srcSide);
|
||||
this.power = Math.max(this.power, b.getWeakRedstonePower(state, this.getTile().getWorld(), target, srcSide));
|
||||
this.sendToOutput(this.power);
|
||||
} else {
|
||||
this.sendToOutput(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnectRedstone() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int isProvidingStrongPower() {
|
||||
return this.isOutput() ? this.power : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int isProvidingWeakPower() {
|
||||
return this.isOutput() ? this.power : 0;
|
||||
}
|
||||
|
||||
private void sendToOutput(final int power) {
|
||||
try {
|
||||
for (final RedstoneP2PTunnelPart rs : this.getOutputs()) {
|
||||
rs.putInput(power);
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPartModel getStaticModels() {
|
||||
return MODELS.getModel(this.isPowered(), this.isActive());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user