Move container builder to Reborn Core

This commit is contained in:
modmuss50 2018-12-02 01:15:03 +00:00
parent e7604cd9ce
commit b399e211fa
58 changed files with 130 additions and 1265 deletions

View file

@ -1,32 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container;
import net.minecraft.entity.player.EntityPlayer;
import techreborn.client.container.builder.BuiltContainer;
public interface IContainerProvider {
BuiltContainer createContainer(EntityPlayer player);
}

View file

@ -1,36 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container;
import net.minecraft.entity.player.EntityPlayer;
import techreborn.client.container.builder.BuiltContainer;
/**
* Created by Mark on 12/04/2017.
*/
public interface IRightClickHandler {
public boolean handleRightClick(int slotID, EntityPlayer player, BuiltContainer container);
}

View file

@ -1,307 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.tuple.MutableTriple;
import org.apache.commons.lang3.tuple.Pair;
import reborncore.common.tile.TileLegacyMachineBase;
import reborncore.common.util.ItemUtils;
import techreborn.client.container.IRightClickHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.Predicate;
public class BuiltContainer extends Container {
private final String name;
private final Predicate<EntityPlayer> canInteract;
private final List<Range<Integer>> playerSlotRanges;
private final List<Range<Integer>> tileSlotRanges;
private final ArrayList<MutableTriple<IntSupplier, IntConsumer, Short>> shortValues;
private final ArrayList<MutableTriple<IntSupplier, IntConsumer, Integer>> integerValues;
private List<Consumer<InventoryCrafting>> craftEvents;
private Integer[] integerParts;
private final TileLegacyMachineBase tile;
public BuiltContainer(final String name, final Predicate<EntityPlayer> canInteract,
final List<Range<Integer>> playerSlotRange,
final List<Range<Integer>> tileSlotRange, TileLegacyMachineBase tile) {
this.name = name;
this.canInteract = canInteract;
this.playerSlotRanges = playerSlotRange;
this.tileSlotRanges = tileSlotRange;
this.shortValues = new ArrayList<>();
this.integerValues = new ArrayList<>();
this.tile = tile;
}
public void addShortSync(final List<Pair<IntSupplier, IntConsumer>> syncables) {
for (final Pair<IntSupplier, IntConsumer> syncable : syncables)
this.shortValues.add(MutableTriple.of(syncable.getLeft(), syncable.getRight(), (short) 0));
this.shortValues.trimToSize();
}
public void addIntegerSync(final List<Pair<IntSupplier, IntConsumer>> syncables) {
for (final Pair<IntSupplier, IntConsumer> syncable : syncables)
this.integerValues.add(MutableTriple.of(syncable.getLeft(), syncable.getRight(), 0));
this.integerValues.trimToSize();
this.integerParts = new Integer[this.integerValues.size()];
}
public void addCraftEvents(final List<Consumer<InventoryCrafting>> craftEvents) {
this.craftEvents = craftEvents;
}
public void addSlot(final Slot slot) {
this.addSlotToContainer(slot);
}
@Override
public boolean canInteractWith(final EntityPlayer playerIn) {
if(this.tile != null) {
return tile.isUsableByPlayer(playerIn);
} else {
return this.canInteract.test(playerIn); // <
}
}
@Override
public final void onCraftMatrixChanged(final IInventory inv) {
if (!this.craftEvents.isEmpty())
this.craftEvents.forEach(consumer -> consumer.accept((InventoryCrafting) inv));
}
@Override
public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player) {
if (dragType == 1 && slotId > 0 && slotId < 1000) {
Slot slot = this.inventorySlots.get(slotId);
if (slot instanceof IRightClickHandler) {
if (((IRightClickHandler) slot).handleRightClick(slot.getSlotIndex(), player, this)) {
return ItemStack.EMPTY;
}
}
}
return super.slotClick(slotId, dragType, clickTypeIn, player);
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
for (final IContainerListener listener : this.listeners) {
int i = 0;
if (!this.shortValues.isEmpty())
for (final MutableTriple<IntSupplier, IntConsumer, Short> value : this.shortValues) {
final short supplied = (short) value.getLeft().getAsInt();
if (supplied != value.getRight()) {
listener.sendWindowProperty(this, i, supplied);
value.setRight(supplied);
}
i++;
}
if (!this.integerValues.isEmpty())
for (final MutableTriple<IntSupplier, IntConsumer, Integer> value : this.integerValues) {
final int supplied = value.getLeft().getAsInt();
if (supplied != value.getRight()) {
listener.sendWindowProperty(this, i, supplied >> 16);
listener.sendWindowProperty(this, i + 1, (short) (supplied & 0xFFFF));
value.setRight(supplied);
}
i += 2;
}
}
}
@Override
public void addListener(final IContainerListener listener) {
super.addListener(listener);
int i = 0;
if (!this.shortValues.isEmpty())
for (final MutableTriple<IntSupplier, IntConsumer, Short> value : this.shortValues) {
final short supplied = (short) value.getLeft().getAsInt();
listener.sendWindowProperty(this, i, supplied);
value.setRight(supplied);
i++;
}
if (!this.integerValues.isEmpty())
for (final MutableTriple<IntSupplier, IntConsumer, Integer> value : this.integerValues) {
final int supplied = value.getLeft().getAsInt();
listener.sendWindowProperty(this, i, supplied >> 16);
listener.sendWindowProperty(this, i + 1, (short) (supplied & 0xFFFF));
value.setRight(supplied);
i += 2;
}
}
@SideOnly(Side.CLIENT)
@Override
public void updateProgressBar(final int id, final int value) {
if (id < this.shortValues.size()) {
this.shortValues.get(id).getMiddle().accept((short) value);
this.shortValues.get(id).setRight((short) value);
} else if (id - this.shortValues.size() < this.integerValues.size() * 2) {
if ((id - this.shortValues.size()) % 2 == 0)
this.integerParts[(id - this.shortValues.size()) / 2] = value;
else {
this.integerValues.get((id - this.shortValues.size()) / 2).getMiddle().accept(
(this.integerParts[(id - this.shortValues.size()) / 2] & 0xFFFF) << 16 | value & 0xFFFF);
}
}
}
@Override
public ItemStack transferStackInSlot(final EntityPlayer player, final int index) {
ItemStack originalStack = ItemStack.EMPTY;
final Slot slot = this.inventorySlots.get(index);
if (slot != null && slot.getHasStack()) {
final ItemStack stackInSlot = slot.getStack();
originalStack = stackInSlot.copy();
boolean shifted = false;
for (final Range<Integer> range : this.playerSlotRanges)
if (range.contains(index)) {
if (this.shiftToTile(stackInSlot))
shifted = true;
break;
}
if (!shifted)
for (final Range<Integer> range : this.tileSlotRanges)
if (range.contains(index)) {
if (this.shiftToPlayer(stackInSlot))
shifted = true;
break;
}
slot.onSlotChange(stackInSlot, originalStack);
if (stackInSlot.getCount() <= 0)
slot.putStack(ItemStack.EMPTY);
else
slot.onSlotChanged();
if (stackInSlot.getCount() == originalStack.getCount())
return ItemStack.EMPTY;
slot.onTake(player, stackInSlot);
}
return originalStack;
}
protected boolean shiftItemStack(final ItemStack stackToShift, final int start, final int end) {
boolean changed = false;
if (stackToShift.isStackable()) {
for (int slotIndex = start; stackToShift.getCount() > 0 && slotIndex < end; slotIndex++) {
final Slot slot = this.inventorySlots.get(slotIndex);
final ItemStack stackInSlot = slot.getStack();
if (!stackInSlot.isEmpty() && ItemUtils.isItemEqual(stackInSlot, stackToShift, true, true)
&& slot.isItemValid(stackToShift)) {
final int resultingStackSize = stackInSlot.getCount() + stackToShift.getCount();
final int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit());
if (resultingStackSize <= max) {
stackToShift.setCount(0);
stackInSlot.setCount(resultingStackSize);
slot.onSlotChanged();
changed = true;
} else if (stackInSlot.getCount() < max) {
stackToShift.shrink(max - stackInSlot.getCount());
stackInSlot.setCount(max);
slot.onSlotChanged();
changed = true;
}
}
}
}
if (stackToShift.getCount() > 0) {
for (int slotIndex = start; stackToShift.getCount() > 0 && slotIndex < end; slotIndex++) {
final Slot slot = this.inventorySlots.get(slotIndex);
ItemStack stackInSlot = slot.getStack();
if (stackInSlot.isEmpty() && slot.isItemValid(stackToShift)) {
final int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit());
stackInSlot = stackToShift.copy();
stackInSlot.setCount(Math.min(stackToShift.getCount(), max));
stackToShift.shrink(stackInSlot.getCount());
slot.putStack(stackInSlot);
slot.onSlotChanged();
changed = true;
}
}
}
return changed;
}
private boolean shiftToTile(final ItemStack stackToShift) {
for (final Range<Integer> range : this.tileSlotRanges)
if (this.shiftItemStack(stackToShift, range.getMinimum(), range.getMaximum() + 1))
return true;
return false;
}
private boolean shiftToPlayer(final ItemStack stackToShift) {
for (final Range<Integer> range : this.playerSlotRanges)
if (this.shiftItemStack(stackToShift, range.getMinimum(), range.getMaximum() + 1))
return true;
return false;
}
public String getName() {
return this.name;
}
}

View file

@ -1,130 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.tuple.Pair;
import reborncore.common.tile.TileLegacyMachineBase;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.Predicate;
public class ContainerBuilder {
private final String name;
private Predicate<EntityPlayer> canInteract = player -> true;
final List<Slot> slots;
final List<Range<Integer>> playerInventoryRanges, tileInventoryRanges;
final List<Pair<IntSupplier, IntConsumer>> shortValues;
final List<Pair<IntSupplier, IntConsumer>> integerValues;
final List<Consumer<InventoryCrafting>> craftEvents;
public ContainerBuilder(final String name) {
this.name = name;
this.slots = new ArrayList<>();
this.playerInventoryRanges = new ArrayList<>();
this.tileInventoryRanges = new ArrayList<>();
this.shortValues = new ArrayList<>();
this.integerValues = new ArrayList<>();
this.craftEvents = new ArrayList<>();
}
public ContainerBuilder interact(final Predicate<EntityPlayer> canInteract) {
this.canInteract = canInteract;
return this;
}
public ContainerPlayerInventoryBuilder player(final InventoryPlayer player) {
return new ContainerPlayerInventoryBuilder(this, player);
}
public ContainerTileInventoryBuilder tile(final IInventory tile) {
return new ContainerTileInventoryBuilder(this, tile);
}
void addPlayerInventoryRange(final Range<Integer> range) {
this.playerInventoryRanges.add(range);
}
void addTileInventoryRange(final Range<Integer> range) {
this.tileInventoryRanges.add(range);
}
@Deprecated
/**
* The container have to know if the tile is still available (the block was not destroyed)
* and if the player is not to far from him to close the GUI if necessary
*/
public BuiltContainer create() {
final BuiltContainer built = new BuiltContainer(this.name, this.canInteract,
this.playerInventoryRanges,
this.tileInventoryRanges, null);
if (!this.shortValues.isEmpty())
built.addShortSync(this.shortValues);
if (!this.integerValues.isEmpty())
built.addIntegerSync(this.integerValues);
if (!this.craftEvents.isEmpty())
built.addCraftEvents(this.craftEvents);
this.slots.forEach(built::addSlot);
this.slots.clear();
return built;
}
public BuiltContainer create(final TileLegacyMachineBase tile) {
final BuiltContainer built = new BuiltContainer(this.name, this.canInteract,
this.playerInventoryRanges,
this.tileInventoryRanges, tile);
if (!this.shortValues.isEmpty())
built.addShortSync(this.shortValues);
if (!this.integerValues.isEmpty())
built.addIntegerSync(this.integerValues);
if (!this.craftEvents.isEmpty())
built.addCraftEvents(this.craftEvents);
this.slots.forEach(built::addSlot);
this.slots.clear();
return built;
}
}

View file

@ -1,129 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import org.apache.commons.lang3.Range;
import reborncore.client.gui.slots.BaseSlot;
import techreborn.client.IconSupplier;
import techreborn.client.container.builder.slot.SpriteSlot;
public final class ContainerPlayerInventoryBuilder {
private final InventoryPlayer player;
private final ContainerBuilder parent;
private Range<Integer> main;
private Range<Integer> hotbar;
private Range<Integer> armor;
ContainerPlayerInventoryBuilder(final ContainerBuilder parent, final InventoryPlayer player) {
this.player = player;
this.parent = parent;
}
public ContainerPlayerInventoryBuilder inventory(final int xStart, final int yStart) {
final int startIndex = this.parent.slots.size();
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 9; ++j)
this.parent.slots.add(new BaseSlot(this.player, j + i * 9 + 9, xStart + j * 18, yStart + i * 18));
this.main = Range.between(startIndex, this.parent.slots.size() - 1);
return this;
}
public ContainerPlayerInventoryBuilder hotbar(final int xStart, final int yStart) {
final int startIndex = this.parent.slots.size();
for (int i = 0; i < 9; ++i)
this.parent.slots.add(new BaseSlot(this.player, i, xStart + i * 18, yStart));
this.hotbar = Range.between(startIndex, this.parent.slots.size() - 1);
return this;
}
public ContainerPlayerInventoryBuilder inventory() {
return this.inventory(8, 94);
}
public ContainerPlayerInventoryBuilder hotbar() {
return this.hotbar(8, 152);
}
public ContainerPlayerArmorInventoryBuilder armor() {
return new ContainerPlayerArmorInventoryBuilder(this);
}
public ContainerBuilder addInventory() {
if (this.hotbar != null)
this.parent.addPlayerInventoryRange(this.hotbar);
if (this.main != null)
this.parent.addPlayerInventoryRange(this.main);
if (this.armor != null)
this.parent.addTileInventoryRange(this.armor);
return this.parent;
}
public static final class ContainerPlayerArmorInventoryBuilder {
private final ContainerPlayerInventoryBuilder parent;
private final int startIndex;
public ContainerPlayerArmorInventoryBuilder(final ContainerPlayerInventoryBuilder parent) {
this.parent = parent;
this.startIndex = parent.parent.slots.size();
}
private ContainerPlayerArmorInventoryBuilder armor(final int index, final int xStart, final int yStart,
final EntityEquipmentSlot slotType, final String sprite) {
this.parent.parent.slots.add(new SpriteSlot(this.parent.player, index, xStart, yStart, sprite, 1)
.setFilter(stack -> stack.getItem().isValidArmor(stack, slotType, this.parent.player.player)));
return this;
}
public ContainerPlayerArmorInventoryBuilder helmet(final int xStart, final int yStart) {
return this.armor(this.parent.player.getSizeInventory() - 2, xStart, yStart, EntityEquipmentSlot.HEAD, IconSupplier.armour_head_name);
}
public ContainerPlayerArmorInventoryBuilder chestplate(final int xStart, final int yStart) {
return this.armor(this.parent.player.getSizeInventory() - 3, xStart, yStart, EntityEquipmentSlot.CHEST, IconSupplier.armour_chest_name);
}
public ContainerPlayerArmorInventoryBuilder leggings(final int xStart, final int yStart) {
return this.armor(this.parent.player.getSizeInventory() - 4, xStart, yStart, EntityEquipmentSlot.LEGS, IconSupplier.armour_legs_name);
}
public ContainerPlayerArmorInventoryBuilder boots(final int xStart, final int yStart) {
return this.armor(this.parent.player.getSizeInventory() - 5, xStart, yStart, EntityEquipmentSlot.FEET, IconSupplier.armour_feet_name);
}
public ContainerPlayerArmorInventoryBuilder complete(final int xStart, final int yStart) {
return this.helmet(xStart, yStart).chestplate(xStart, yStart + 18).leggings(xStart, yStart + 18 + 18)
.boots(xStart, yStart + 18 + 18 + 18);
}
public ContainerPlayerInventoryBuilder addArmor() {
this.parent.armor = Range.between(this.startIndex, this.parent.parent.slots.size() - 1);
return this.parent;
}
}
}

View file

@ -1,181 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.tuple.Pair;
import reborncore.api.recipe.IRecipeCrafterProvider;
import reborncore.api.tile.IUpgrade;
import reborncore.api.tile.IUpgradeable;
import reborncore.client.gui.slots.BaseSlot;
import reborncore.client.gui.slots.SlotFake;
import reborncore.client.gui.slots.SlotOutput;
import reborncore.common.powerSystem.TilePowerAcceptor;
import techreborn.Core;
import techreborn.client.container.builder.slot.FilteredSlot;
import techreborn.client.container.builder.slot.UpgradeSlot;
import techreborn.compat.CompatManager;
import techreborn.utils.IC2ItemCharger;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.Predicate;
public class ContainerTileInventoryBuilder {
private final IInventory tile;
private final ContainerBuilder parent;
private final int rangeStart;
ContainerTileInventoryBuilder(final ContainerBuilder parent, final IInventory tile) {
this.tile = tile;
this.parent = parent;
this.rangeStart = parent.slots.size();
if (tile instanceof IUpgradeable) {
upgradeSlots((IUpgradeable) tile);
}
}
public ContainerTileInventoryBuilder slot(final int index, final int x, final int y) {
this.parent.slots.add(new BaseSlot(this.tile, index, x, y));
return this;
}
public ContainerTileInventoryBuilder outputSlot(final int index, final int x, final int y) {
this.parent.slots.add(new SlotOutput(this.tile, index, x, y));
return this;
}
public ContainerTileInventoryBuilder fakeSlot(final int index, final int x, final int y) {
this.parent.slots.add(new SlotFake(this.tile, index, x, y, false, false, Integer.MAX_VALUE));
return this;
}
public ContainerTileInventoryBuilder filterSlot(final int index, final int x, final int y,
final Predicate<ItemStack> filter) {
this.parent.slots.add(new FilteredSlot(this.tile, index, x, y).setFilter(filter));
return this;
}
public ContainerTileInventoryBuilder energySlot(final int index, final int x, final int y) {
this.parent.slots.add(new FilteredSlot(this.tile, index, x, y)
.setFilter(stack -> stack.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.UP)
|| (CompatManager.isIC2Loaded && IC2ItemCharger.isIC2PoweredItem(stack))));
return this;
}
public ContainerTileInventoryBuilder fluidSlot(final int index, final int x, final int y) {
this.parent.slots.add(new FilteredSlot(this.tile, index, x, y).setFilter(
stack -> stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)));
return this;
}
public ContainerTileInventoryBuilder fuelSlot(final int index, final int x, final int y) {
this.parent.slots.add(new SlotFurnaceFuel(this.tile, index, x, y));
return this;
}
@Deprecated
public ContainerTileInventoryBuilder upgradeSlot(final int index, final int x, final int y) {
this.parent.slots.add(new FilteredSlot(this.tile, index, x, y)
.setFilter(stack -> stack.getItem() instanceof IUpgrade));
return this;
}
private ContainerTileInventoryBuilder upgradeSlots(IUpgradeable upgradeable) {
if (upgradeable.canBeUpgraded()) {
for (int i = 0; i < upgradeable.getUpgradeSlotCount(); i++) {
this.parent.slots.add(new UpgradeSlot(upgradeable.getUpgradeInvetory(), i, -19, i * 18 + 12));
}
}
return this;
}
/**
* @param supplier The supplier must supply a variable holding inside a Short, it
* will be truncated by force.
* @param setter The setter to call when the variable has been updated.
* @return ContainerTileInventoryBuilder Inventory which will do the sync
*/
public ContainerTileInventoryBuilder syncShortValue(final IntSupplier supplier, final IntConsumer setter) {
this.parent.shortValues.add(Pair.of(supplier, setter));
return this;
}
/**
* @param supplier The supplier it can supply a variable holding in an Integer it
* will be split inside multiples shorts.
* @param setter The setter to call when the variable has been updated.
* @return ContainerTileInventoryBuilder Inventory which will do the sync
*/
public ContainerTileInventoryBuilder syncIntegerValue(final IntSupplier supplier, final IntConsumer setter) {
this.parent.integerValues.add(Pair.of(supplier, setter));
return this;
}
public ContainerTileInventoryBuilder syncEnergyValue() {
if (this.tile instanceof TilePowerAcceptor)
return this.syncIntegerValue(() -> (int) ((TilePowerAcceptor) this.tile).getEnergy(),
((TilePowerAcceptor) this.tile)::setEnergy)
.syncIntegerValue(() -> (int) ((TilePowerAcceptor) this.tile).extraPowerStoage,
((TilePowerAcceptor) this.tile)::setExtraPowerStoage)
.syncIntegerValue(() -> (int) ((TilePowerAcceptor) this.tile).getPowerChange(),
((TilePowerAcceptor) this.tile)::setPowerChange);
Core.logHelper.error(this.tile + " is not an instance of TilePowerAcceptor! Energy cannot be synced.");
return this;
}
public ContainerTileInventoryBuilder syncCrafterValue() {
if (this.tile instanceof IRecipeCrafterProvider)
return this
.syncIntegerValue(() -> ((IRecipeCrafterProvider) this.tile).getRecipeCrafter().currentTickTime,
(currentTickTime) -> ((IRecipeCrafterProvider) this.tile)
.getRecipeCrafter().currentTickTime = currentTickTime)
.syncIntegerValue(() -> ((IRecipeCrafterProvider) this.tile).getRecipeCrafter().currentNeededTicks,
(currentNeededTicks) -> ((IRecipeCrafterProvider) this.tile)
.getRecipeCrafter().currentNeededTicks = currentNeededTicks);
Core.logHelper
.error(this.tile + " is not an instance of IRecipeCrafterProvider! Craft progress cannot be synced.");
return this;
}
public ContainerTileInventoryBuilder onCraft(final Consumer<InventoryCrafting> onCraft) {
this.parent.craftEvents.add(onCraft);
return this;
}
public ContainerBuilder addInventory() {
this.parent.tileInventoryRanges.add(Range.between(this.rangeStart, this.parent.slots.size() - 1));
return this.parent;
}
}

View file

@ -1,65 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder.slot;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import java.util.function.Predicate;
public class FilteredSlot extends Slot {
private Predicate<ItemStack> filter;
private int stackLimit = 64;
public FilteredSlot(final IInventory inventory, final int index, final int xPosition, final int yPosition) {
super(inventory, index, xPosition, yPosition);
}
public FilteredSlot(final IInventory inventory, final int index, final int xPosition, final int yPosition, int stackLimit) {
super(inventory, index, xPosition, yPosition);
this.stackLimit = stackLimit;
}
public FilteredSlot setFilter(final Predicate<ItemStack> filter) {
this.filter = filter;
return this;
}
@Override
public boolean isItemValid(final ItemStack stack) {
try {
return this.filter.test(stack);
} catch (NullPointerException e) {
return true;
}
}
@Override
public int getSlotStackLimit() {
return stackLimit;
}
}

View file

@ -1,59 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder.slot;
import net.minecraft.inventory.IInventory;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nullable;
public class SpriteSlot extends FilteredSlot {
private final String spriteName;
int stacksize;
public SpriteSlot(final IInventory inventory, final int index, final int xPosition, final int yPosition, final String sprite, final int stacksize) {
super(inventory, index, xPosition, yPosition);
this.spriteName = sprite;
this.stacksize = stacksize;
}
public SpriteSlot(final IInventory inventory, final int index, final int xPosition, final int yPosition, final String sprite) {
this(inventory, index, xPosition, yPosition, sprite, 64);
}
@Override
public int getSlotStackLimit() {
return this.stacksize;
}
@Override
@Nullable
@SideOnly(Side.CLIENT)
public String getSlotTexture() {
return this.spriteName;
}
}

View file

@ -1,74 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.client.container.builder.slot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import reborncore.api.tile.IUpgrade;
import reborncore.api.tile.IUpgradeable;
import reborncore.common.util.Inventory;
import techreborn.client.container.IRightClickHandler;
import techreborn.client.container.builder.BuiltContainer;
public class UpgradeSlot extends Slot implements IRightClickHandler {
public UpgradeSlot(final IInventory inventory, final int index, final int xPosition, final int yPosition) {
super(inventory, index, xPosition, yPosition);
}
@Override
public boolean isItemValid(final ItemStack stack) {
return stack.getItem() instanceof IUpgrade;
}
@Override
public int getSlotStackLimit() {
return 1;
}
@Override
public boolean handleRightClick(int slotID, EntityPlayer player, BuiltContainer container) {
if (inventory instanceof Inventory) {
Inventory inv = (Inventory) inventory;
TileEntity tileEntity = inv.getTileBase();
if (tileEntity instanceof IUpgradeable) {
IUpgradeable upgradeable = (IUpgradeable) tileEntity;
if (upgradeable.canBeUpgraded()) {
ItemStack stack = upgradeable.getUpgradeInvetory().getStackInSlot(slotID);
if (!stack.isEmpty() && stack.getItem() instanceof IUpgrade) {
if (player.world.isRemote) {
((IUpgrade) stack.getItem()).handleRightClick(tileEntity, stack, container, slotID);
}
}
}
}
}
return true;
}
}