equivalent-exchange-3/src/main/java/com/pahimar/ee3/inventory/ContainerTransmutationTablet.java

545 lines
21 KiB
Java
Raw Normal View History

package com.pahimar.ee3.inventory;
import com.pahimar.ee3.EquivalentExchange3;
import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy;
import com.pahimar.ee3.api.exchange.EnergyValue;
2015-05-07 19:45:06 +02:00
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy;
import com.pahimar.ee3.api.knowledge.PlayerKnowledgeRegistryProxy;
import com.pahimar.ee3.inventory.element.IElementButtonHandler;
2015-04-02 07:26:01 +02:00
import com.pahimar.ee3.inventory.element.IElementSliderHandler;
import com.pahimar.ee3.inventory.element.IElementTextFieldHandler;
import com.pahimar.ee3.item.ItemAlchenomicon;
import com.pahimar.ee3.item.ItemMiniumStone;
import com.pahimar.ee3.item.ItemPhilosophersStone;
import com.pahimar.ee3.knowledge.PlayerKnowledge;
2015-04-02 07:26:01 +02:00
import com.pahimar.ee3.network.PacketHandler;
import com.pahimar.ee3.network.message.MessagePlayerKnowledge;
import com.pahimar.ee3.reference.Comparators;
import com.pahimar.ee3.tileentity.TileEntityTransmutationTablet;
2015-04-02 07:26:01 +02:00
import com.pahimar.ee3.util.FilterUtils;
import com.pahimar.ee3.util.ItemStackUtils;
import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.containers.LinearProgressHandler;
import com.pahimar.ee3.util.containers.ProgressMessage;
import com.pahimar.repackage.cofh.lib.util.helpers.MathHelper;
2015-05-01 20:21:59 +02:00
import cpw.mods.fml.common.FMLCommonHandler;
2015-04-02 07:26:01 +02:00
import cpw.mods.fml.common.network.NetworkRegistry;
2015-03-26 23:12:22 +01:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2015-03-22 03:02:35 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
2015-04-02 07:26:01 +02:00
import java.util.*;
/**
* FIXME Continue integrating PR#881
*/
public class ContainerTransmutationTablet extends ContainerEE implements IElementTextFieldHandler, IElementSliderHandler, IElementButtonHandler {
2015-04-02 07:26:01 +02:00
private InventoryTransmutationTablet inventoryTransmutationTablet;
private final TileEntityTransmutationTablet transmutationTablet;
private final World world;
private EnergyValue energyValue;
private String searchTerm;
private int sortOption, sortOrder, scrollBarPosition;
private LinearProgressHandler<ProgressUpdate> progressHandler;
public ContainerTransmutationTablet(InventoryPlayer inventoryPlayer, TileEntityTransmutationTablet transmutationTablet) {
this.transmutationTablet = transmutationTablet;
this.world = transmutationTablet.getWorldObj();
if (EquivalentExchange3.proxy.getClientProxy() != null) {
this.progressHandler = new LinearProgressHandler<ProgressUpdate>(ProgressUpdate.class) {
@Override
public void handle(ProgressMessage<ProgressUpdate> message) {
handleProgressUpdate(message);
}
};
2015-04-02 07:26:01 +02:00
}
handleTomeSync(transmutationTablet.getStackInSlot(TileEntityTransmutationTablet.ALCHENOMICON_INDEX));
this.sortOption = 0;
this.scrollBarPosition = 0;
this.energyValue = transmutationTablet.getAvailableEnergy();
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_1, 62, 24));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_2, 35, 35));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_3, 26, 61));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_4, 35, 87));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_5, 62, 99));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_6, 89, 87));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_7, 98, 61));
this.addSlotToContainer(new SlotTabletInput(this, transmutationTablet, TileEntityTransmutationTablet.ITEM_INPUT_8, 89, 35));
this.addSlotToContainer(new Slot(transmutationTablet, TileEntityTransmutationTablet.STONE_INDEX, 62, 61) {
@Override
public int getSlotStackLimit() {
return 1;
}
@Override
public boolean isItemValid(ItemStack itemStack) {
2015-04-02 07:26:01 +02:00
return itemStack.getItem() instanceof ItemMiniumStone || itemStack.getItem() instanceof ItemPhilosophersStone;
2015-03-26 23:12:22 +01:00
}
});
this.addSlotToContainer(new SlotAlchenomicon(transmutationTablet, TileEntityTransmutationTablet.ALCHENOMICON_INDEX, 152, 15));
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
2015-04-02 07:26:01 +02:00
this.addSlotToContainer(new SlotTabletOutput(this, inventoryTransmutationTablet, i * 3 + j, 175 + j * 20, 38 + i * 20));
}
}
// Add the player's inventory slots to the container
for (int inventoryRowIndex = 0; inventoryRowIndex < PLAYER_INVENTORY_ROWS; ++inventoryRowIndex) {
for (int inventoryColumnIndex = 0; inventoryColumnIndex < PLAYER_INVENTORY_COLUMNS; ++inventoryColumnIndex) {
this.addSlotToContainer(new Slot(inventoryPlayer, inventoryColumnIndex + inventoryRowIndex * 9 + 9, 8 + inventoryColumnIndex * 18, 164 + inventoryRowIndex * 18));
}
}
// Add the player's action bar slots to the container
for (int actionBarSlotIndex = 0; actionBarSlotIndex < PLAYER_INVENTORY_COLUMNS; ++actionBarSlotIndex) {
this.addSlotToContainer(new Slot(inventoryPlayer, actionBarSlotIndex, 8 + actionBarSlotIndex * 18, 222));
}
2015-04-02 07:26:01 +02:00
updateInventory();
}
public EnergyValue getEnergyValue() {
return energyValue;
}
2015-05-01 20:21:59 +02:00
@Override
public boolean canInteractWith(EntityPlayer entityPlayer) {
return transmutationTablet != null && transmutationTablet.isUseableByPlayer(entityPlayer);
2015-05-01 20:21:59 +02:00
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
2015-04-02 07:26:01 +02:00
this.updateInventory();
EnergyValue tileEnergyValue = transmutationTablet.getAvailableEnergy();
if (energyValue.compareTo(tileEnergyValue) != 0) {
energyValue = tileEnergyValue;
ProgressUpdate.getMessage(ProgressUpdate.EnergyUpdate, tileEnergyValue.getValue()).send(this, this.crafters);
2015-04-02 07:26:01 +02:00
}
}
@SideOnly(Side.CLIENT)
public void updateProgressBar(int valueType, int updatedValue) {
progressHandler.handle(valueType, updatedValue);
}
private void sendKnowledgeToClient(Collection<ItemStack> knownItemStacks) {
PacketHandler.INSTANCE.sendToAllAround(
new MessagePlayerKnowledge(transmutationTablet, knownItemStacks),
new NetworkRegistry.TargetPoint(world.provider.dimensionId, transmutationTablet.xCoord, transmutationTablet.yCoord, transmutationTablet.zCoord, 5d)
);
}
private void handleTomeSync(ItemStack itemStack) {
if (itemStack != null && itemStack.getItem() instanceof ItemAlchenomicon && ItemStackUtils.getOwnerName(itemStack) != null) {
if (!world.isRemote) {
Set<ItemStack> knownItemStacks = PlayerKnowledgeRegistryProxy.getKnownItemStacks(ItemStackUtils.getOwnerName(itemStack));
inventoryTransmutationTablet = new InventoryTransmutationTablet(knownItemStacks);
sendKnowledgeToClient(knownItemStacks);
}
else {
this.inventoryTransmutationTablet = new InventoryTransmutationTablet(transmutationTablet.getPlayerKnowledge());
}
}
else {
this.inventoryTransmutationTablet = new InventoryTransmutationTablet();
if (!world.isRemote) {
sendKnowledgeToClient(null);
}
}
}
2015-04-02 07:26:01 +02:00
@SideOnly(Side.CLIENT)
public void handleProgressUpdate(ProgressMessage<ProgressUpdate> message) {
switch (message.getType()) {
case EnergyUpdate: {
energyValue = new EnergyValue(message.getFloat());
LogHelper.trace("Got energy: {}", message.getFloat());
break;
}
case SortTypeUpdate: {
sortOption = message.getInt();
break;
}
case SortOrderUpdate: {
sortOrder = message.getInt();
break;
}
case ScrollBarUpdate: {
break;
}
2015-04-02 07:26:01 +02:00
}
updateInventory();
}
@Override
public void handleElementTextFieldUpdate(String elementName, String updatedText) {
if (elementName.equalsIgnoreCase("searchField")) {
searchTerm = updatedText;
updateInventory();
}
}
2015-04-02 07:26:01 +02:00
@Override
public void handleElementSliderUpdate(String elementName, int elementValue) {
if (elementName.equals("scrollBar")) {
scrollBarPosition = elementValue;
updateInventory();
}
2015-04-02 07:26:01 +02:00
}
public void handlePlayerKnowledgeUpdate(PlayerKnowledge playerKnowledge) {
if (playerKnowledge != null) {
inventoryTransmutationTablet = new InventoryTransmutationTablet(playerKnowledge.getKnownItemStacks());
updateInventory();
2015-04-02 07:26:01 +02:00
}
}
private void updateInventory() {
2015-04-02 07:26:01 +02:00
ItemStack[] newInventory = new ItemStack[30];
Set<ItemStack> filteredSet = FilterUtils.filterByDisplayName(inventoryTransmutationTablet.getKnownTransmutations(), searchTerm, FilterUtils.NameFilterType.CONTAINS);
List<ItemStack> filteredList = new ArrayList<>(FilterUtils.filterByEnergyValue(filteredSet, energyValue));
if (sortOption == 0 && sortOrder == 0) {
Collections.sort(filteredList, Comparators.DISPLAY_NAME_COMPARATOR);
}
else if (sortOption == 0 && sortOrder == 1) {
Collections.sort(filteredList, Comparators.DISPLAY_NAME_COMPARATOR.reversed());
}
else if (sortOption == 1 && sortOrder == 0) {
Collections.sort(filteredList, Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR);
}
else if (sortOption == 1 && sortOrder == 1) {
Collections.sort(filteredList, Comparators.ENERGY_VALUE_ITEM_STACK_COMPARATOR.reversed());
}
else if (sortOption == 2 && sortOrder == 0) {
Collections.sort(filteredList, Comparators.ID_COMPARATOR);
}
else if (sortOption == 2 && sortOrder == 1) {
Collections.sort(filteredList, Comparators.ID_COMPARATOR.reversed());
}
2015-04-02 07:26:01 +02:00
if (filteredList.size() <= 30) {
this.scrollBarPosition = 0;
2015-04-02 07:26:01 +02:00
}
int adjustedStartIndex = (int) ((scrollBarPosition / 187f) * filteredList.size());
adjustedStartIndex -= adjustedStartIndex % 3; // Paginate by 3 elements.
int startIndex = Math.max(0, Math.min(adjustedStartIndex, filteredList.size() - 30));
int endIndex = Math.min(adjustedStartIndex + 30, filteredList.size());
if (world.isRemote) {
LogHelper.trace("updateInventory called");
LogHelper.trace(String.format("known transmutations count: %d", this.inventoryTransmutationTablet.getKnownTransmutations().size()));
LogHelper.trace(String.format("(from, to): (%d, %d)", startIndex, endIndex));
2015-04-02 07:26:01 +02:00
}
filteredList.subList(startIndex, endIndex).toArray(newInventory);
for (int i = 0; i < 30; i++) {
2015-04-02 07:26:01 +02:00
this.getSlot(i + 10).putStack(newInventory[i]);
}
}
2015-03-22 03:02:35 +01:00
@Override
public ItemStack transferStackInSlot(EntityPlayer entityPlayer, int slotIndex) {
2015-03-22 03:02:35 +01:00
ItemStack itemStack = null;
Slot slot = (Slot) inventorySlots.get(slotIndex);
if (slot != null && slot.getHasStack()) {
2015-03-22 03:02:35 +01:00
ItemStack slotItemStack = slot.getStack();
itemStack = slotItemStack.copy();
/**
* If we are shift-clicking an item out of the Transmutation Tablet's container,
* attempt to put it in the first available slot in the entityPlayer's
* inventory
*/
if (slotIndex < TileEntityTransmutationTablet.INVENTORY_SIZE) {
if (!this.mergeItemStack(slotItemStack, TileEntityTransmutationTablet.INVENTORY_SIZE, inventorySlots.size(), false)) {
2015-03-22 03:02:35 +01:00
return null;
}
}
else if (slotIndex >= TileEntityTransmutationTablet.INVENTORY_SIZE && slotIndex < 40) {
if (!this.mergeTransmutedItemStack(entityPlayer, slot, slotItemStack, 40, inventorySlots.size(), false)) {
2015-04-21 06:06:44 +02:00
return null;
}
2015-04-21 03:16:48 +02:00
}
else {
if (slotItemStack.getItem() instanceof ItemAlchenomicon) {
if (!this.mergeItemStack(slotItemStack, TileEntityTransmutationTablet.ALCHENOMICON_INDEX, TileEntityTransmutationTablet.INVENTORY_SIZE, false)) {
2015-03-22 03:02:35 +01:00
return null;
}
}
else if (slotItemStack.getItem() instanceof ItemMiniumStone || slotItemStack.getItem() instanceof ItemPhilosophersStone) {
if (!this.mergeItemStack(slotItemStack, TileEntityTransmutationTablet.STONE_INDEX, TileEntityTransmutationTablet.INVENTORY_SIZE, false)) {
2015-03-22 03:02:35 +01:00
return null;
}
}
else {
if (!this.mergeItemStack(slotItemStack, TileEntityTransmutationTablet.ITEM_INPUT_1, TileEntityTransmutationTablet.INVENTORY_SIZE, false)) {
2015-03-22 03:02:35 +01:00
return null;
}
}
}
if (slotItemStack.stackSize == 0) {
2015-03-22 03:02:35 +01:00
slot.putStack(null);
}
else {
2015-03-22 03:02:35 +01:00
slot.onSlotChanged();
}
}
return itemStack;
}
private boolean mergeTransmutedItemStack(EntityPlayer entityPlayer, Slot transmutationOutputSlot, ItemStack itemStack, int slotMin, int slotMax, boolean ascending) {
// Calculate how many items can be transmuted
int numCanTransmute = MathHelper.floor(this.transmutationTablet.getAvailableEnergy().getValue() / EnergyValueRegistryProxy.getEnergyValue(itemStack).getValue());
int numTransmuted = 0;
ItemStack itemStack1 = itemStack.copy();
itemStack1.stackSize = Math.min(numCanTransmute, itemStack1.getMaxStackSize());
if (numCanTransmute <= 0) {
2015-04-21 06:06:44 +02:00
return false;
2015-04-21 03:16:48 +02:00
}
2015-04-21 06:06:44 +02:00
int currentSlotIndex = ascending ? slotMax - 1 : slotMin;
Slot slot;
ItemStack stackInSlot;
while (itemStack1.stackSize > 0 && (!ascending && currentSlotIndex < slotMax || ascending && currentSlotIndex >= slotMin)) {
2015-04-21 06:06:44 +02:00
slot = (Slot) this.inventorySlots.get(currentSlotIndex);
stackInSlot = slot.getStack();
2015-04-21 06:06:44 +02:00
if (stackInSlot == null) {
stackInSlot = itemStack1.copy();
stackInSlot.stackSize = itemStack1.stackSize;
slot.putStack(stackInSlot);
numTransmuted = itemStack1.stackSize;
itemStack1.stackSize = 0;
slot.onSlotChanged();
2015-04-21 06:06:44 +02:00
}
else if (slot.isItemValid(itemStack1) && ItemStackUtils.equalsIgnoreStackSize(itemStack1, stackInSlot)) {
2015-04-21 03:16:48 +02:00
int slotStackSizeLimit = Math.min(stackInSlot.getMaxStackSize(), slot.getSlotStackLimit());
int combinedStackSize = stackInSlot.stackSize + itemStack1.stackSize;
2015-04-21 06:06:44 +02:00
if (combinedStackSize <= slotStackSizeLimit) {
2015-04-21 06:06:44 +02:00
stackInSlot.stackSize = combinedStackSize;
numTransmuted = stackInSlot.stackSize - itemStack1.stackSize;
itemStack1.stackSize = 0;
2015-04-21 06:06:44 +02:00
slot.onSlotChanged();
}
else if (stackInSlot.stackSize < slotStackSizeLimit) {
2015-04-21 06:06:44 +02:00
itemStack1.stackSize -= slotStackSizeLimit - stackInSlot.stackSize;
stackInSlot.stackSize = slotStackSizeLimit;
numTransmuted = stackInSlot.stackSize - itemStack1.stackSize;
slot.onSlotChanged();
}
2015-04-21 06:06:44 +02:00
}
currentSlotIndex += ascending ? -1 : 1;
2015-04-21 06:06:44 +02:00
}
transmutationOutputSlot.onPickupFromSlot(entityPlayer, ItemStackUtils.clone(itemStack, numTransmuted));
return false;
2015-04-21 03:16:48 +02:00
}
@Override
public void handleElementButtonClick(String elementName, int mouseButton) {
if (elementName.equals("sortOption")) {
if (mouseButton == 0) {
sortOption = (sortOption + 1) % 3;
}
else if (mouseButton == 1) {
sortOption = (sortOption + (3 - 1)) % 3;
}
}
else if (elementName.equals("sortOrder")) {
sortOrder = 1 - sortOrder; // Toggle between 0 and 1
2015-05-05 03:08:06 +02:00
}
ProgressUpdate.getMessage(ProgressUpdate.SortTypeUpdate, sortOption).send(this, this.crafters);
ProgressUpdate.getMessage(ProgressUpdate.SortOrderUpdate, sortOrder).send(this, this.crafters);
}
2015-06-29 02:26:12 +02:00
@Override
public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) {
if (button == 0 && flag == 6) {
2015-06-29 02:26:12 +02:00
return null;
}
2015-06-29 02:26:12 +02:00
return super.slotClick(slot, button, flag, player);
}
private class SlotAlchenomicon extends Slot {
public SlotAlchenomicon(IInventory iInventory, int slotIndex, int x, int y) {
2015-04-02 07:26:01 +02:00
super(iInventory, slotIndex, x, y);
}
@Override
public int getSlotStackLimit() {
2015-04-02 07:26:01 +02:00
return 1;
}
@Override
public boolean isItemValid(ItemStack itemStack) {
return itemStack.getItem() instanceof ItemAlchenomicon;
2015-04-02 07:26:01 +02:00
}
@Override
public void onSlotChanged() {
super.onSlotChanged();
handleTomeSync(getStack());
updateInventory();
2015-04-02 07:26:01 +02:00
}
}
private class SlotTabletOutput extends Slot {
2015-04-02 07:26:01 +02:00
private ContainerTransmutationTablet containerTransmutationTablet;
public SlotTabletOutput(ContainerTransmutationTablet containerTransmutationTablet, IInventory iInventory, int slotIndex, int x, int y) {
super(iInventory, slotIndex, x, y);
2015-04-02 07:26:01 +02:00
this.containerTransmutationTablet = containerTransmutationTablet;
}
@Override
public boolean isItemValid(ItemStack itemStack) {
return false;
}
2015-04-02 07:26:01 +02:00
@Override
public boolean canTakeStack(EntityPlayer entityPlayer) {
2015-05-01 20:21:59 +02:00
return this.getHasStack();
2015-04-02 07:26:01 +02:00
}
@Override
public void onPickupFromSlot(EntityPlayer entityPlayer, ItemStack itemStack) {
2015-04-02 07:26:01 +02:00
super.onPickupFromSlot(entityPlayer, itemStack);
2015-05-01 20:21:59 +02:00
if (getHasStack()) {
containerTransmutationTablet.transmutationTablet.consumeInventoryForEnergyValue(itemStack);
2015-05-01 20:21:59 +02:00
}
containerTransmutationTablet.updateInventory();
2015-05-01 20:21:59 +02:00
}
@Override
public void onSlotChanged() {
2015-05-01 20:21:59 +02:00
super.onSlotChanged();
if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
containerTransmutationTablet.transmutationTablet.updateEnergyValueFromInventory();
2015-05-01 20:21:59 +02:00
}
2015-04-02 07:26:01 +02:00
}
@Override
@SideOnly(Side.CLIENT)
public boolean func_111238_b() {
2015-04-02 07:26:01 +02:00
return this.getHasStack();
}
}
private class SlotTabletInput extends Slot {
2015-04-02 07:26:01 +02:00
private ContainerTransmutationTablet containerTransmutationTablet;
public SlotTabletInput(ContainerTransmutationTablet containerTransmutationTablet, IInventory iInventory, int slotIndex, int x, int y) {
super(iInventory, slotIndex, x, y);
2015-04-02 07:26:01 +02:00
this.containerTransmutationTablet = containerTransmutationTablet;
}
@Override
public boolean isItemValid(ItemStack itemStack) {
return EnergyValueRegistryProxy.hasEnergyValue(itemStack) && BlacklistRegistryProxy.isExchangeable(itemStack);
}
2015-04-02 07:26:01 +02:00
@Override
public void onPickupFromSlot(EntityPlayer entityPlayer, ItemStack itemStack) {
2015-04-02 07:26:01 +02:00
super.onPickupFromSlot(entityPlayer, itemStack);
this.containerTransmutationTablet.transmutationTablet.updateEnergyValueFromInventory();
2015-04-02 07:26:01 +02:00
this.containerTransmutationTablet.updateInventory();
}
@Override
public void putStack(ItemStack itemStack) {
2015-04-02 07:26:01 +02:00
super.putStack(itemStack);
this.containerTransmutationTablet.transmutationTablet.updateEnergyValueFromInventory();
2015-04-02 07:26:01 +02:00
this.containerTransmutationTablet.updateInventory();
}
}
enum ProgressUpdate {
EnergyUpdate,
SortTypeUpdate,
SortOrderUpdate,
ScrollBarUpdate;
public static ProgressMessage<ProgressUpdate> getMessage(ProgressUpdate type, int data) {
return new ProgressMessage<>(ProgressUpdate.class, type, data);
}
public static ProgressMessage<ProgressUpdate> getMessage(ProgressUpdate type, float data) {
return new ProgressMessage<>(ProgressUpdate.class, type, data);
}
}
}