Fixed panels not updating when a connection via panel connectors is broken or placed

This commit is contained in:
malte0811 2017-07-03 20:38:31 +02:00
parent 6a1081f47c
commit f8f2de930e
7 changed files with 68 additions and 13 deletions

View file

@ -23,6 +23,7 @@ import malte0811.industrialWires.IndustrialWires;
import malte0811.industrialWires.blocks.BlockIWBase;
import malte0811.industrialWires.blocks.IMetaEnum;
import malte0811.industrialWires.controlpanel.PanelComponent;
import malte0811.industrialWires.controlpanel.PanelUtils;
import malte0811.industrialWires.controlpanel.PropertyComponents;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
@ -48,6 +49,7 @@ import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import javax.annotation.Nonnull;
import java.util.List;
public class BlockPanel extends BlockIWBase implements IMetaEnum {
public static final PropertyEnum<BlockTypes_Panel> type = PropertyEnum.create("type", BlockTypes_Panel.class);
@ -218,4 +220,33 @@ public class BlockPanel extends BlockIWBase implements IMetaEnum {
}
}
}
@Override
public void breakBlock(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
super.breakBlock(worldIn, pos, state);
//break connections
List<BlockPos> panels = PanelUtils.discoverPanelParts(worldIn, pos, 11 * 11 * 11);
for (BlockPos p : panels) {
if (!p.equals(pos)) {
TileEntity panelPart = worldIn.getTileEntity(p);
if (panelPart instanceof TileEntityPanel) {
((TileEntityPanel) panelPart).removeAllRSCons();
}
}
}
}
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
super.onBlockAdded(worldIn, pos, state);
List<BlockPos> panels = PanelUtils.discoverPanelParts(worldIn, pos, 11 * 11 * 11);
for (BlockPos p : panels) {
if (!p.equals(pos)) {
TileEntity panelPart = worldIn.getTileEntity(p);
if (panelPart instanceof TileEntityPanel) {
((TileEntityPanel) panelPart).firstTick = true;
}
}
}
}
}

View file

@ -33,4 +33,8 @@ public enum BlockTypes_Panel implements IStringSerializable {
public String getName() {
return toString().toLowerCase(Locale.ENGLISH);
}
public boolean isPanelConnector() {
return this != CREATOR && this != UNFINISHED;
}
}

View file

@ -54,7 +54,7 @@ import java.util.Set;
public class TileEntityPanel extends TileEntityIWBase implements IDirectionalTile, IBlockBoundsIW, IPlayerInteraction, ITickable, IEBlockInterfaces.ITileDrop {
private PropertyComponents.PanelRenderProperties components = new PropertyComponents.PanelRenderProperties();
private boolean firstTick = true;
public boolean firstTick = true;
// non-rendered properties
private Set<TileEntityRSPanelConn> rsPorts = new HashSet<>();
@ -86,7 +86,7 @@ public class TileEntityPanel extends TileEntityIWBase implements IDirectionalTil
}
if (!world.isRemote) {
if (firstTick) {
List<BlockPos> parts = PanelUtils.discoverPanelParts(world, pos);
List<BlockPos> parts = PanelUtils.discoverPanelParts(world, pos, 100);
for (BlockPos bp : parts) {
TileEntity te = world.getTileEntity(bp);
if (te instanceof TileEntityRSPanelConn && !rsPorts.contains(te)) {
@ -299,9 +299,15 @@ public class TileEntityPanel extends TileEntityIWBase implements IDirectionalTil
for (PanelComponent pc : components) {
pc.invalidate(this);
}
removeAllRSCons();
}
public void removeAllRSCons() {
for (TileEntityRSPanelConn rs : rsPorts) {
rs.unregisterPanel(this, true);
}
rsPorts.clear();
firstTick = true;
}
@Override
@ -310,8 +316,6 @@ public class TileEntityPanel extends TileEntityIWBase implements IDirectionalTil
for (PanelComponent pc : components) {
pc.invalidate(this);
}
for (TileEntityRSPanelConn rs : rsPorts) {
rs.unregisterPanel(this, true);
}
removeAllRSCons();
}
}

View file

@ -108,7 +108,7 @@ public class TileEntityPanelCreator extends TileEntityIWBase implements INetGUI,
}
break;
case CREATE_PANEL:
if (ItemStack.areItemsEqual(PanelUtils.getPanelBase(), inv)) {
if (ItemStack.areItemsEqual(PanelUtils.getPanelBase(), inv) && !components.isEmpty()) {
float height = PanelUtils.getHeight(inv);
float angle = PanelUtils.getAngle(inv);
boolean valid = true;

View file

@ -77,7 +77,7 @@ public class TileEntityRSPanelConn extends TileEntityImmersiveConnectable implem
loaded = true;
// completely reload the network
network.removeFromNetwork(null);
List<BlockPos> parts = PanelUtils.discoverPanelParts(world, pos);
List<BlockPos> parts = PanelUtils.discoverPanelParts(world, pos, 100);
for (BlockPos bp : parts) {
TileEntity te = world.getTileEntity(bp);
if (te instanceof TileEntityPanel) {
@ -166,6 +166,7 @@ public class TileEntityRSPanelConn extends TileEntityImmersiveConnectable implem
Consumer<byte[]> listener = pc.getRSInputHandler(id, panel);
if (listener != null) {
changeListeners.add(listener);
listener.accept(out);
}
pc.registerRSOutput(id, rsOut);
}
@ -174,18 +175,27 @@ public class TileEntityRSPanelConn extends TileEntityImmersiveConnectable implem
}
public void unregisterPanel(TileEntityPanel panel, boolean remove) {
out = new byte[16];
PropertyComponents.PanelRenderProperties p = panel.getComponents();
for (PanelComponent pc : p) {
Consumer<byte[]> listener = pc.getRSInputHandler(id, panel);
if (listener != null) {
listener.accept(out);
changeListeners.remove(listener);
}
pc.unregisterRSOutput(id, rsOut);
outputs.remove(new PCWrapper(pc));
}
panel.unregisterRS(this);
if (remove) {
connectedPanels.remove(panel);
}
for (TileEntityPanel te : connectedPanels) {
for (PanelComponent pc : te.getComponents()) {
pc.registerRSOutput(id, rsOut);
}
}
network.updateValues();
}
@Override
@ -287,7 +297,7 @@ public class TileEntityRSPanelConn extends TileEntityImmersiveConnectable implem
@Override
public void onChange(NBTTagCompound nbt, EntityPlayer p) {
if (nbt.hasKey("rsId")) {
List<BlockPos> parts = PanelUtils.discoverPanelParts(world, pos);
List<BlockPos> parts = PanelUtils.discoverPanelParts(world, pos, 100);
List<TileEntityPanel> tes = new ArrayList<>(parts.size());
for (BlockPos bp : parts) {
TileEntity te = world.getTileEntity(bp);

View file

@ -19,6 +19,7 @@
package malte0811.industrialWires.containers;
import blusunrize.immersiveengineering.api.ApiUtils;
import malte0811.industrialWires.blocks.controlpanel.BlockTypes_Panel;
import malte0811.industrialWires.blocks.controlpanel.TileEntityPanelCreator;
import malte0811.industrialWires.controlpanel.PanelUtils;
import net.minecraft.entity.player.EntityPlayer;
@ -52,7 +53,9 @@ public class ContainerPanelCreator extends Container {
@Override
public boolean isItemValid(ItemStack stack) {
return ItemStack.areItemStacksEqual(ApiUtils.copyStackWithAmount(stack, 1), PanelUtils.getPanelBase()) || stack.getItem() == PanelUtils.PANEL_ITEM;
if (ItemStack.areItemStacksEqual(ApiUtils.copyStackWithAmount(stack, 1), PanelUtils.getPanelBase()))
return true;
return stack.getItem() == PanelUtils.PANEL_ITEM && stack.getMetadata() == BlockTypes_Panel.TOP.ordinal();
}
});

View file

@ -377,18 +377,21 @@ public final class PanelUtils {
return panelBase;
}
public static List<BlockPos> discoverPanelParts(World w, BlockPos here) {
public static List<BlockPos> discoverPanelParts(World w, BlockPos here, int maxCount) {
BiPredicate<BlockPos, Integer> isValid = (pos, count) -> {
if (here.distanceSq(pos) > 25 || count > 100 || !w.isBlockLoaded(pos)) {
if (pos.equals(here)) {
return true;
}
if (here.distanceSq(pos) > 25 || count > maxCount || !w.isBlockLoaded(pos)) {
return false;
}
IBlockState state = w.getBlockState(pos);
return state.getBlock() == IndustrialWires.panel && state.getValue(BlockPanel.type) != BlockTypes_Panel.CREATOR;
return state.getBlock() == IndustrialWires.panel && state.getValue(BlockPanel.type).isPanelConnector();
};
List<BlockPos> all = discoverLocal(w, here, isValid);
List<BlockPos> ret = new ArrayList<>();
for (BlockPos pos : all) {
if (w.getBlockState(pos).getValue(BlockPanel.type) != BlockTypes_Panel.DUMMY) {
if (w.getBlockState(pos).getBlock() == IndustrialWires.panel && w.getBlockState(pos).getValue(BlockPanel.type) != BlockTypes_Panel.DUMMY) {
ret.add(pos);
}
}