2017-03-24 17:40:05 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
|
|
|
* Copyright (C) 2016-2017 malte0811
|
|
|
|
*
|
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Industrial Wires 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package malte0811.industrialWires.blocks.controlpanel;
|
|
|
|
|
|
|
|
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IDirectionalTile;
|
|
|
|
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IPlayerInteraction;
|
|
|
|
import blusunrize.immersiveengineering.common.util.IELogger;
|
2017-03-28 18:15:41 +02:00
|
|
|
import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4;
|
2017-03-24 17:40:05 +01:00
|
|
|
import malte0811.industrialWires.blocks.IBlockBoundsIW;
|
|
|
|
import malte0811.industrialWires.blocks.TileEntityIWBase;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2017-03-28 18:15:41 +02:00
|
|
|
import net.minecraft.nbt.NBTTagLong;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2017-03-24 17:40:05 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.util.ITickable;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2017-03-28 18:15:41 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.RayTraceResult;
|
2017-03-24 17:40:05 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2017-03-28 18:15:41 +02:00
|
|
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2017-03-24 17:40:05 +01:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2017-03-28 18:15:41 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2017-03-24 17:40:05 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class TileEntityPanel extends TileEntityIWBase implements IDirectionalTile, IBlockBoundsIW, IPlayerInteraction, ITickable {
|
2017-03-28 18:15:41 +02:00
|
|
|
PropertyComponents.PanelRenderProperties components = new PropertyComponents.PanelRenderProperties();
|
|
|
|
// non-rendered properties
|
|
|
|
//relative positions!
|
|
|
|
private List<BlockPos> rsWireConns = new ArrayList<>();
|
2017-03-24 17:40:05 +01:00
|
|
|
{
|
|
|
|
Random r = new Random();
|
2017-03-28 18:15:41 +02:00
|
|
|
PanelComponent b = new LightedButton(0xff<<(8*r.nextInt(3)), false, false, 0, 0);
|
2017-03-24 17:40:05 +01:00
|
|
|
b.setX(3/16F);
|
2017-03-28 18:15:41 +02:00
|
|
|
b.setY(.75F);
|
|
|
|
b.setPanelHeight(components.height);
|
2017-03-24 17:40:05 +01:00
|
|
|
components.add(b);
|
2017-03-28 18:15:41 +02:00
|
|
|
b = new LightedButton(0xff<<(8*r.nextInt(3)), false, true, 0, 1);
|
2017-03-24 17:40:05 +01:00
|
|
|
b.setX(8/16F);
|
2017-03-28 18:15:41 +02:00
|
|
|
b.setY(.75F);
|
|
|
|
b.setPanelHeight(components.height);
|
2017-03-24 17:40:05 +01:00
|
|
|
components.add(b);
|
2017-03-28 18:15:41 +02:00
|
|
|
b = new LightedButton(0xff<<(8*r.nextInt(3)), false, true, 0, 2);
|
2017-03-24 17:40:05 +01:00
|
|
|
b.setX(13/16F);
|
2017-03-28 18:15:41 +02:00
|
|
|
b.setY(.75F);
|
|
|
|
b.setPanelHeight(components.height);
|
|
|
|
components.add(b);
|
|
|
|
b = new Label("TESTtextIII");
|
|
|
|
b.setX(3/16F);
|
|
|
|
b.setY(.25F);
|
|
|
|
b.setPanelHeight(components.height);
|
2017-03-24 17:40:05 +01:00
|
|
|
components.add(b);
|
2017-03-28 18:15:41 +02:00
|
|
|
|
|
|
|
rsWireConns.add(new BlockPos(0, -1, 0));//one RS output 1 block below the panel
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
for (PanelComponent pc:components) {
|
|
|
|
pc.update(this);
|
|
|
|
}
|
2017-03-28 18:15:41 +02:00
|
|
|
if (!worldObj.isRemote) {
|
|
|
|
for (int i = 0; i < rsWireConns.size(); i++) {
|
|
|
|
TileEntityRSPanelConn rs = getRSConn(i);
|
|
|
|
if (rs != null)
|
|
|
|
rs.flushRS();
|
|
|
|
}
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeNBT(NBTTagCompound out, boolean updatePacket) {
|
|
|
|
NBTTagList comps = new NBTTagList();
|
|
|
|
for (PanelComponent p:components) {
|
|
|
|
NBTTagCompound nbt = new NBTTagCompound();
|
|
|
|
p.writeToNBT(nbt);
|
|
|
|
comps.appendTag(nbt);
|
|
|
|
}
|
|
|
|
out.setTag("components", comps);
|
2017-03-28 18:15:41 +02:00
|
|
|
out.setInteger("facing", components.facing.getHorizontalIndex());
|
|
|
|
out.setFloat("height", components.height);
|
|
|
|
NBTTagList rsConns = new NBTTagList();
|
|
|
|
for (BlockPos pos:rsWireConns) {
|
|
|
|
rsConns.appendTag(new NBTTagLong(pos.toLong()));
|
|
|
|
}
|
|
|
|
out.setTag("rsConns", rsConns);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readNBT(NBTTagCompound in, boolean updatePacket) {
|
|
|
|
NBTTagList l = in.getTagList("components", 10);
|
|
|
|
components.clear();
|
|
|
|
for (int i = 0;i<l.tagCount();i++) {
|
|
|
|
PanelComponent pc = PanelComponent.read(l.getCompoundTagAt(i));
|
|
|
|
if (pc!=null) {
|
|
|
|
components.add(pc);
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 18:15:41 +02:00
|
|
|
l = in.getTagList("rsConns", 4);
|
|
|
|
rsWireConns.clear();
|
|
|
|
for (int i = 0;i<l.tagCount();i++) {
|
|
|
|
rsWireConns.add(BlockPos.fromLong(((NBTTagLong)l.get(i)).getLong()));
|
|
|
|
}
|
|
|
|
components.facing = EnumFacing.getHorizontal(in.getInteger("facing"));
|
|
|
|
components.height = in.getFloat("height");
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumFacing getFacing() {
|
2017-03-28 18:15:41 +02:00
|
|
|
return components.facing;
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFacing(EnumFacing facing) {
|
2017-03-28 18:15:41 +02:00
|
|
|
this.components.facing = facing;
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getFacingLimitation() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean mirrorFacingOnPlacement(EntityLivingBase placer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canHammerRotate(EnumFacing side, float hitX, float hitY, float hitZ, EntityLivingBase entity) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canRotate(EnumFacing axis) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
private AxisAlignedBB defAABB;
|
2017-03-24 17:40:05 +01:00
|
|
|
@Override
|
|
|
|
public AxisAlignedBB getBoundingBox() {
|
2017-03-28 18:15:41 +02:00
|
|
|
if (defAABB==null) {
|
|
|
|
defAABB = new AxisAlignedBB(0, 0, 0, 1, components.height, 1);
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
return defAABB;
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:15:41 +02:00
|
|
|
public PropertyComponents.PanelRenderProperties getComponents() {
|
2017-03-24 17:40:05 +01:00
|
|
|
return components;
|
|
|
|
}
|
2017-03-28 18:15:41 +02:00
|
|
|
public AxisAlignedBB apply(Matrix4 mat, AxisAlignedBB in) {
|
|
|
|
Vec3d min = new Vec3d(in.minX, in.minY, in.minZ);
|
|
|
|
Vec3d max = new Vec3d(in.maxX, in.maxY, in.maxZ);
|
|
|
|
min = mat.apply(min);
|
|
|
|
max = mat.apply(max);
|
|
|
|
return new AxisAlignedBB(min, max);
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
@Nullable
|
2017-03-28 18:15:41 +02:00
|
|
|
public Pair<PanelComponent, RayTraceResult> getSelectedComponent(EntityPlayer player, Vec3d hit, boolean hitAbs) {
|
|
|
|
Matrix4 mat = components.getPanelTopTransform();
|
2017-03-24 17:40:05 +01:00
|
|
|
for (PanelComponent pc : components) {
|
2017-03-28 18:15:41 +02:00
|
|
|
AxisAlignedBB box = pc.getBlockRelativeAABB();
|
|
|
|
if (box!=null) {
|
|
|
|
box = apply(mat, box.expandXyz(.002));
|
|
|
|
Vec3d hitVec = hitAbs ? hit.addVector(-pos.getX(), -pos.getY(), -pos.getZ()) : hit;
|
|
|
|
Vec3d playerPos = Minecraft.getMinecraft().thePlayer.getPositionVector().addVector(-pos.getX(), player.getEyeHeight() - pos.getY(), -pos.getZ());
|
|
|
|
RayTraceResult ray = box.calculateIntercept(playerPos, hitVec);
|
|
|
|
if (ray != null) {
|
|
|
|
return new ImmutablePair<>(pc, ray);
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean interact(EnumFacing side, EntityPlayer player, EnumHand hand, ItemStack heldItem, float hitX, float hitY, float hitZ) {
|
2017-03-28 18:15:41 +02:00
|
|
|
Pair<PanelComponent, RayTraceResult> pc = getSelectedComponent(player, new Vec3d(hitX, hitY, hitZ), false);
|
|
|
|
return pc != null && pc.getLeft().interactWith(pc.getRight().hitVec, this);
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void triggerRenderUpdate() {
|
|
|
|
IBlockState state = worldObj.getBlockState(pos);
|
|
|
|
worldObj.notifyBlockUpdate(pos,state,state,3);
|
|
|
|
worldObj.addBlockEvent(pos, state.getBlock(), 255, 0);
|
|
|
|
}
|
2017-03-28 18:15:41 +02:00
|
|
|
|
|
|
|
public TileEntityRSPanelConn getRSConn(int id) {
|
|
|
|
if (id < 0 || id >= rsWireConns.size()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
TileEntity te = worldObj.getTileEntity(pos.add(rsWireConns.get(id)));
|
|
|
|
if (te instanceof TileEntityRSPanelConn) {
|
|
|
|
return (TileEntityRSPanelConn) te;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
}
|