2017-04-10 16:20:39 +02: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;
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
import blusunrize.immersiveengineering.api.ApiUtils;
|
|
|
|
import blusunrize.immersiveengineering.common.util.IELogger;
|
|
|
|
import blusunrize.immersiveengineering.common.util.Utils;
|
2017-04-10 16:20:39 +02:00
|
|
|
import blusunrize.immersiveengineering.common.util.inventory.IIEInventory;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
|
|
|
import malte0811.industrialWires.blocks.INetGUI;
|
2017-04-10 16:20:39 +02:00
|
|
|
import malte0811.industrialWires.blocks.TileEntityIWBase;
|
2017-04-13 17:14:05 +02:00
|
|
|
import malte0811.industrialWires.controlpanel.MessageType;
|
|
|
|
import malte0811.industrialWires.controlpanel.PanelComponent;
|
2017-04-28 20:20:52 +02:00
|
|
|
import malte0811.industrialWires.controlpanel.PanelUtils;
|
2017-04-27 18:15:11 +02:00
|
|
|
import malte0811.industrialWires.items.ItemPanelComponent;
|
2017-04-13 17:14:05 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2017-04-10 16:20:39 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2017-04-12 15:55:37 +02:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2017-04-17 19:35:14 +02:00
|
|
|
import java.util.Iterator;
|
2017-04-12 15:55:37 +02:00
|
|
|
import java.util.List;
|
2017-04-10 16:20:39 +02:00
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
public class TileEntityPanelCreator extends TileEntityIWBase implements IIEInventory, INetGUI {
|
2017-04-12 15:55:37 +02:00
|
|
|
public List<PanelComponent> components = new ArrayList<>();
|
|
|
|
public float height = 0.5F;
|
2017-04-13 17:14:05 +02:00
|
|
|
public ItemStack[] inv = new ItemStack[1];
|
2017-04-10 16:20:39 +02:00
|
|
|
|
2017-04-12 15:55:37 +02:00
|
|
|
@Override
|
|
|
|
public void readNBT(NBTTagCompound nbt, boolean updatePacket) {
|
|
|
|
NBTTagList l = nbt.getTagList("components", 10);
|
2017-04-28 20:20:52 +02:00
|
|
|
PanelUtils.readListFromNBT(l, components);
|
2017-04-12 15:55:37 +02:00
|
|
|
height = nbt.getFloat("height");
|
2017-04-13 17:14:05 +02:00
|
|
|
inv = Utils.readInventory(nbt.getTagList("inventory", 10), inv.length);
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-04-12 15:55:37 +02:00
|
|
|
public void writeNBT(NBTTagCompound nbt, boolean updatePacket) {
|
|
|
|
writeToItemNBT(nbt, false);
|
2017-04-13 17:14:05 +02:00
|
|
|
nbt.setTag("inventory", Utils.writeInventory(inv));
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|
2017-04-10 16:20:39 +02:00
|
|
|
|
2017-04-12 15:55:37 +02:00
|
|
|
public void writeToItemNBT(NBTTagCompound nbt, boolean toItem) {
|
|
|
|
NBTTagList comps = new NBTTagList();
|
|
|
|
for (PanelComponent p : components) {
|
|
|
|
NBTTagCompound nbtInner = new NBTTagCompound();
|
|
|
|
p.writeToNBT(nbtInner, toItem);
|
|
|
|
comps.appendTag(nbtInner);
|
|
|
|
}
|
|
|
|
nbt.setTag("components", comps);
|
|
|
|
nbt.setFloat("height", height);
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack[] getInventory() {
|
2017-04-13 17:14:05 +02:00
|
|
|
return inv;
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isStackValid(int slot, ItemStack stack) {
|
2017-04-27 18:15:11 +02:00
|
|
|
if (slot == 0) {
|
2017-04-13 17:14:05 +02:00
|
|
|
return ApiUtils.compareToOreName(stack, "plateIron");
|
|
|
|
}
|
|
|
|
return true;
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlotLimit(int slot) {
|
2017-04-27 18:15:11 +02:00
|
|
|
return slot == 0 ? 1 : 64;
|
2017-04-10 16:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doGraphicalUpdates(int slot) {
|
|
|
|
|
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChange(NBTTagCompound nbt, EntityPlayer p) {
|
|
|
|
int type = nbt.getInteger("type");
|
|
|
|
switch (MessageType.values()[type]) {
|
|
|
|
case ADD:
|
2017-04-22 18:14:49 +02:00
|
|
|
ItemStack curr = p.inventory.getItemStack();
|
2017-04-27 18:15:11 +02:00
|
|
|
PanelComponent pc = ItemPanelComponent.componentFromStack(curr);
|
|
|
|
if (pc != null) {
|
2017-04-22 18:14:49 +02:00
|
|
|
pc.setX(nbt.getFloat("x"));
|
|
|
|
pc.setY(nbt.getFloat("y"));
|
2017-04-13 17:14:05 +02:00
|
|
|
pc.setPanelHeight(height);
|
|
|
|
components.add(pc);
|
2017-04-27 18:15:11 +02:00
|
|
|
if (curr != null) {
|
2017-04-13 17:14:05 +02:00
|
|
|
curr.stackSize--;
|
2017-04-27 18:15:11 +02:00
|
|
|
if (curr.stackSize <= 0) {
|
2017-04-13 17:14:05 +02:00
|
|
|
p.inventory.setItemStack(null);
|
|
|
|
}
|
|
|
|
p.inventory.markDirty();
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-27 18:15:11 +02:00
|
|
|
IELogger.info("(IndustrialWires) Failed to load panel component send by " + p.getDisplayNameString());
|
2017-04-13 17:14:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REMOVE:
|
2017-04-27 18:15:11 +02:00
|
|
|
int id = nbt.getInteger("id");
|
|
|
|
if (id >= 0 && id < components.size() && p.inventory.getItemStack() == null) {
|
|
|
|
PanelComponent removed = components.get(id);
|
|
|
|
ItemStack remItem = ItemPanelComponent.stackFromComponent(removed);
|
|
|
|
p.inventory.setItemStack(remItem);
|
|
|
|
p.inventory.markDirty();
|
|
|
|
components.remove(id);
|
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
break;
|
|
|
|
case CREATE_PANEL:
|
|
|
|
if (ApiUtils.compareToOreName(inv[0], "plateIron")) {
|
|
|
|
NBTTagCompound panelNBT = new NBTTagCompound();
|
|
|
|
writeToItemNBT(panelNBT, true);
|
|
|
|
ItemStack panel = new ItemStack(IndustrialWires.panel, 1, BlockTypes_Panel.TOP.ordinal());
|
|
|
|
panel.setTagCompound(panelNBT);
|
|
|
|
inv[0] = panel;
|
2017-04-20 20:44:08 +02:00
|
|
|
components.clear();
|
2017-04-13 17:14:05 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REMOVE_ALL:
|
2017-04-17 19:35:14 +02:00
|
|
|
Iterator<PanelComponent> it = components.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
PanelComponent next = it.next();
|
2017-04-27 18:15:11 +02:00
|
|
|
ItemStack nextStack = ItemPanelComponent.stackFromComponent(next);
|
|
|
|
if (nextStack != null) {
|
2017-04-17 19:35:14 +02:00
|
|
|
if (p.inventory.addItemStackToInventory(nextStack)) {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 17:14:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
markDirty();
|
|
|
|
IBlockState state = worldObj.getBlockState(pos);
|
|
|
|
worldObj.notifyBlockUpdate(pos, state, state, 3);
|
|
|
|
}
|
2017-04-12 15:55:37 +02:00
|
|
|
}
|