Single component panels work, fixed some bugs with normal ones

This commit is contained in:
malte0811 2018-07-31 17:34:18 +02:00
parent bd26b2b358
commit b5826d226b
8 changed files with 141 additions and 97 deletions

View File

@ -63,9 +63,11 @@ public abstract class TileEntityIWBase extends TileEntity {
}
public void triggerRenderUpdate() {
IBlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
world.addBlockEvent(pos, state.getBlock(), 255, 0);
if (world!=null) {
IBlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
world.addBlockEvent(pos, state.getBlock(), 255, 0);
}
}
@Override

View File

@ -21,6 +21,8 @@ import malte0811.industrialWires.blocks.BlockIWBase;
import malte0811.industrialWires.blocks.IMetaEnum;
import malte0811.industrialWires.controlpanel.PanelComponent;
import malte0811.industrialWires.controlpanel.PropertyComponents;
import malte0811.industrialWires.util.MiscUtils;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
@ -86,6 +88,8 @@ public class BlockPanel extends BlockIWBase implements IMetaEnum {
return new TileEntityUnfinishedPanel();
case SINGLE_COMP:
return new TileEntityComponentPanel();
case DUMMY:
return new TileEntityGeneralCP();
default:
return null;
}
@ -235,7 +239,7 @@ public class BlockPanel extends BlockIWBase implements IMetaEnum {
return state.getValue(type)==BlockTypes_Panel.SINGLE_COMP;
}
/*@Override
@Override
public int getStrongPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
if (blockState.getValue(type)==BlockTypes_Panel.SINGLE_COMP) {
TileEntity te = blockAccess.getTileEntity(pos);
@ -256,5 +260,16 @@ public class BlockPanel extends BlockIWBase implements IMetaEnum {
}
}
return 0;
}*/
}
@Override
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos) {
super.neighborChanged(state, worldIn, pos, blockIn, fromPos);
if (!worldIn.isRemote) {
TileEntityComponentPanel panel = MiscUtils.getExistingTE(worldIn, pos, TileEntityComponentPanel.class);
if (panel != null) {
panel.updateRSInput();
}
}
}
}

View File

@ -15,58 +15,57 @@
package malte0811.industrialWires.blocks.controlpanel;
import malte0811.industrialWires.controlpanel.ControlPanelNetwork;
import malte0811.industrialWires.controlpanel.PanelComponent;
import malte0811.industrialWires.controlpanel.PropertyComponents;
import malte0811.industrialWires.items.ItemPanelComponent;
import net.minecraft.block.BlockRedstoneWire;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import javax.annotation.Nonnull;
import static malte0811.industrialWires.util.MiscUtils.apply;
public class TileEntityComponentPanel extends TileEntityPanel {
/*private int rsOut = 0; todo
private Consumer<byte[]> rsIn;
private byte rsOut = 0;
public TileEntityComponentPanel() {
components = new PropertyComponents.AABBPanelProperties();
panelNetwork = new SingleCompNetwork();
}
@Override
public void update() {
for (PanelComponent pc : components) {
pc.update();
}
public void onLoad() {
super.onLoad();
if (!world.isRemote) {
if (firstTick&&components.size()>0) {
PanelComponent pc = components.get(0);
pc.registerRSOutput(-1, (channel, value, pcTmp)->{
rsOut = value;
if (!isInvalid()) {
markBlockForUpdate(pos);
markBlockForUpdate(pos.offset(components.getTop(), -1));
}
});
rsIn = pc.getRSInputHandler(-1, this);
updateRS();
firstTick = false;
}
updateRSInput();
}
}
public void updateRS() {
if (rsIn != null) {
int value = world.isBlockIndirectlyGettingPowered(pos);
if (value == 0) {
for (EnumFacing f : EnumFacing.HORIZONTALS) {
IBlockState state = world.getBlockState(pos.offset(f));
if (state.getBlock() == Blocks.REDSTONE_WIRE && state.getValue(BlockRedstoneWire.POWER) > value)
value = state.getValue(BlockRedstoneWire.POWER);
}
public void updateRSInput() {
int value = world.isBlockIndirectlyGettingPowered(pos);
if (value == 0) {
for (EnumFacing f : EnumFacing.HORIZONTALS) {
IBlockState state = world.getBlockState(pos.offset(f));
if (state.getBlock() == Blocks.REDSTONE_WIRE && state.getValue(BlockRedstoneWire.POWER) > value)
value = state.getValue(BlockRedstoneWire.POWER);
}
byte[] tmp = new byte[16];
for (int i = 0; i < tmp.length; i++) {
tmp[i] = (byte) value;
}
rsIn.accept(tmp);
}
((SingleCompNetwork)panelNetwork).setGlobalInput((byte) value);
}
public void markBlockForUpdate(BlockPos pos)
{
IBlockState state = world.getBlockState(getBlockPos());
world.notifyBlockUpdate(pos,state,state,3);
world.notifyNeighborsOfStateChange(pos, state.getBlock(), true);
if (world!=null) {
IBlockState state = world.getBlockState(getBlockPos());
world.notifyBlockUpdate(pos, state, state, 3);
world.notifyNeighborsOfStateChange(pos, state.getBlock(), true);
}
}
@Override
@ -78,31 +77,52 @@ public class TileEntityComponentPanel extends TileEntityPanel {
return defAABB;
}
@Override
public void registerRS(TileEntityRSPanelConn te) {
//NO-OP
}
@Override
public void unregisterRS(TileEntityRSPanelConn te) {
//NO-OP
}
@Override
public boolean interactsWithRSWires() {
return false;
}
public int getRSOutput() {
return rsOut;
}
@Nonnull
@Override
public ItemStack getTileDrop(@Nonnull EntityPlayer player, @Nonnull IBlockState state) {
public ItemStack getTileDrop(EntityPlayer player, @Nonnull IBlockState state) {
if (components.size()<1) {
return ItemStack.EMPTY;
}
return ItemPanelComponent.stackFromComponent(components.get(0));
}
*/}
@Override
public boolean canJoinNetwork() {
return false;
}
public void setComponent(PanelComponent comp) {
components.clear();
components.add(comp);
comp.setPanel(this);
comp.setNetwork(panelNetwork);
}
private class SingleCompNetwork extends ControlPanelNetwork {
@Override
public void setOutputs(IOwner owner, RSChannelState... out) {
super.setOutputs(owner, out);
byte oldOut = rsOut;
rsOut = 0;
for (OutputValue s:activeOutputs.values()) {
rsOut = (byte) Math.max(rsOut, s.getTargetState().getStrength());
}
if (oldOut!=rsOut) {
markBlockForUpdate(pos);
}
}
public void setGlobalInput(byte value) {
for (RSChannel channel: listeners.keySet()) {
RSChannelState state = new RSChannelState(channel, value);
for (ChangeListener l:listeners.get(channel)) {
l.onChange(state);
}
}
}
}
}

View File

@ -19,12 +19,13 @@ import malte0811.industrialWires.blocks.TileEntityIWBase;
import malte0811.industrialWires.controlpanel.ControlPanelNetwork;
import malte0811.industrialWires.controlpanel.ControlPanelNetwork.IOwner;
import malte0811.industrialWires.util.MiscUtils;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import javax.annotation.Nonnull;
public abstract class TileEntityGeneralCP extends TileEntityIWBase implements IOwner {
public class TileEntityGeneralCP extends TileEntityIWBase implements IOwner {
@Nonnull
protected ControlPanelNetwork panelNetwork = new ControlPanelNetwork();
@ -37,16 +38,18 @@ public abstract class TileEntityGeneralCP extends TileEntityIWBase implements IO
super.onLoad();
if (!world.isRemote) {
boolean isFinalNet = false;
for (EnumFacing side : EnumFacing.VALUES) {
BlockPos posSide = pos.offset(side);
TileEntityGeneralCP neighbour = MiscUtils.getExistingTE(world, posSide, TileEntityGeneralCP.class);
if (neighbour != null) {
if (!isFinalNet) {
panelNetwork = neighbour.panelNetwork;
panelNetwork.addMember(this);
isFinalNet = true;
} else {
neighbour.panelNetwork.replaceWith(panelNetwork, world);
if (canJoinNetwork()) {
for (EnumFacing side : EnumFacing.VALUES) {
BlockPos posSide = pos.offset(side);
TileEntityGeneralCP neighbour = MiscUtils.getExistingTE(world, posSide, TileEntityGeneralCP.class);
if (neighbour != null && neighbour.canJoinNetwork()) {
if (!isFinalNet) {
panelNetwork = neighbour.panelNetwork;
panelNetwork.addMember(this);
isFinalNet = true;
} else {
neighbour.panelNetwork.replaceWith(panelNetwork, world);
}
}
}
}
@ -62,6 +65,12 @@ public abstract class TileEntityGeneralCP extends TileEntityIWBase implements IO
panelNetwork.removeMember(pos, world);
}
@Override
public void writeNBT(NBTTagCompound out, boolean updatePacket) {}
@Override
public void readNBT(NBTTagCompound in, boolean updatePacket) {}
@Override
public void invalidate() {
super.invalidate();
@ -72,4 +81,8 @@ public abstract class TileEntityGeneralCP extends TileEntityIWBase implements IO
public BlockPos getBlockPos() {
return pos;
}
public boolean canJoinNetwork() {
return true;
}
}

View File

@ -46,9 +46,9 @@ import javax.annotation.Nullable;
import static malte0811.industrialWires.util.MiscUtils.apply;
public class TileEntityPanel extends TileEntityGeneralCP implements IDirectionalTile, IBlockBoundsIW, IPlayerInteraction, ITickable, IEBlockInterfaces.ITileDrop {
public class TileEntityPanel extends TileEntityGeneralCP implements IDirectionalTile, IBlockBoundsIW, IPlayerInteraction,
ITickable, IEBlockInterfaces.ITileDrop {
protected PropertyComponents.PanelRenderProperties components = new PropertyComponents.PanelRenderProperties();
public boolean firstTick = true;
{
int[] colors = {
@ -132,9 +132,11 @@ public class TileEntityPanel extends TileEntityGeneralCP implements IDirectional
NBTTagList l = nbt.getTagList("components", 10);
PanelUtils.readListFromNBT(l, components);
panelNetwork.removeIOFor(this);
for (PanelComponent pc:components) {
for (PanelComponent pc : components) {
pc.setPanel(this);
pc.setNetwork(panelNetwork);
if (world == null || !world.isRemote) {
pc.setNetwork(panelNetwork);
}
}
components.setHeight(nbt.getFloat("height"));
components.setAngle(nbt.getFloat("angle"));

View File

@ -32,11 +32,11 @@ import java.util.function.Consumer;
@SuppressWarnings({"unused", "WeakerAccess"})
public class ControlPanelNetwork {
private Map<RSChannel, List<ChangeListener>> listeners = new HashMap<>();
private Map<RSChannel, List<OutputValue>> allOutputs = new HashMap<>();
private Map<RSChannel, OutputValue> activeOutputs = new HashMap<>();
private Map<RSChannel, OutputValue> secondActiveOutputs = new HashMap<>();
private Set<BlockPos> members = new HashSet<>();
protected Map<RSChannel, List<ChangeListener>> listeners = new HashMap<>();
protected Map<RSChannel, List<OutputValue>> allOutputs = new HashMap<>();
protected Map<RSChannel, OutputValue> activeOutputs = new HashMap<>();
protected Map<RSChannel, OutputValue> secondActiveOutputs = new HashMap<>();
protected Set<BlockPos> members = new HashSet<>();
public void addListener(IOwner owner, Consumer<RSChannelState> listener, RSChannel... channels) {
ChangeListener l = new ChangeListener(owner, listener);
@ -232,7 +232,7 @@ public class ControlPanelNetwork {
}
}
private static class ChangeListener extends Owned {
protected static class ChangeListener extends Owned {
private final Consumer<RSChannelState> listener;
private ChangeListener(IOwner owner, Consumer<RSChannelState> listener) {
@ -245,7 +245,7 @@ public class ControlPanelNetwork {
}
}
private static class OutputValue extends Owned {
protected static class OutputValue extends Owned {
private final RSChannelState targetState;
private OutputValue(@Nullable IOwner owner, RSChannelState targetState) {

View File

@ -136,7 +136,7 @@ public abstract class PanelComponent implements IOwner {
@Override
public BlockPos getBlockPos() {
return panel.getBlockPos();
return panel.getPos();
}
public void writeToNBT(NBTTagCompound nbt, boolean toItem) {

View File

@ -17,14 +17,14 @@ package malte0811.industrialWires.items;
import blusunrize.immersiveengineering.api.ApiUtils;
import blusunrize.immersiveengineering.client.ClientProxy;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces;
import malte0811.industrialWires.IndustrialWires;
import malte0811.industrialWires.blocks.controlpanel.BlockTypes_Panel;
import malte0811.industrialWires.blocks.controlpanel.TileEntityPanel;
import malte0811.industrialWires.blocks.controlpanel.TileEntityComponentPanel;
import malte0811.industrialWires.controlpanel.IConfigurableComponent;
import malte0811.industrialWires.controlpanel.PanelComponent;
import malte0811.industrialWires.controlpanel.PanelUtils;
import malte0811.industrialWires.controlpanel.SevenSegDisplay;
import malte0811.industrialWires.util.MiscUtils;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
@ -33,13 +33,11 @@ import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@ -187,7 +185,7 @@ public class ItemPanelComponent extends Item implements INetGUIItem {
ItemStack itemstack = player.getHeldItem(hand);
if (!itemstack.isEmpty() && player.canPlayerEdit(pos, facing, itemstack) && worldIn.mayPlace(IndustrialWires.panel, pos, false, facing, (Entity) null)) {
if (!itemstack.isEmpty() && player.canPlayerEdit(pos, facing, itemstack) && worldIn.mayPlace(IndustrialWires.panel, pos, false, facing, null)) {
placeBlockAt(itemstack, player, worldIn, pos, facing, hitX, hitY, hitZ);
SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, player);
worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
@ -202,17 +200,11 @@ public class ItemPanelComponent extends Item implements INetGUIItem {
private void placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) {
IBlockState state = IndustrialWires.panel.getStateFromMeta(BlockTypes_Panel.SINGLE_COMP.ordinal());
world.setBlockState(pos, state);
TileEntity te = world.getTileEntity(pos);
if (te instanceof IEBlockInterfaces.IDirectionalTile) {
EnumFacing dir = ((IEBlockInterfaces.IDirectionalTile) te).getFacingForPlacement(player, pos, side, hitX, hitY, hitZ);
((IEBlockInterfaces.IDirectionalTile) te).setFacing(dir);
}
if (te instanceof IEBlockInterfaces.ITileDrop) {
((IEBlockInterfaces.ITileDrop) te).readOnPlacement(player, stack);
}
if (te instanceof TileEntityPanel) {
((TileEntityPanel) te).getComponents().clear();
((TileEntityPanel) te).getComponents().add(componentFromStack(stack));
TileEntityComponentPanel te = MiscUtils.getExistingTE(world, pos, TileEntityComponentPanel.class);
if (te!=null) {
EnumFacing dir = te.getFacingForPlacement(player, pos, side, hitX, hitY, hitZ);
te.setFacing(dir);
te.setComponent(componentFromStack(stack));
}
}