PrettyPipes/src/main/java/de/ellpeck/prettypipes/Utility.java

121 lines
5.2 KiB
Java
Raw Normal View History

2020-04-14 01:38:48 +02:00
package de.ellpeck.prettypipes;
2020-05-07 18:30:40 +02:00
import de.ellpeck.prettypipes.items.IModule;
2020-05-09 12:57:25 +02:00
import de.ellpeck.prettypipes.network.PipeItem;
2020-04-20 13:12:26 +02:00
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resources.I18n;
2020-05-07 18:30:40 +02:00
import net.minecraft.entity.player.PlayerEntity;
2020-04-14 01:38:48 +02:00
import net.minecraft.inventory.InventoryHelper;
2020-05-07 18:30:40 +02:00
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
2020-04-14 01:38:48 +02:00
import net.minecraft.item.ItemStack;
2020-05-09 12:57:25 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
2020-04-14 01:38:48 +02:00
import net.minecraft.tileentity.TileEntity;
2020-04-14 18:51:43 +02:00
import net.minecraft.util.Direction;
2020-04-14 01:38:48 +02:00
import net.minecraft.util.math.BlockPos;
2020-04-20 13:12:26 +02:00
import net.minecraft.util.text.*;
2020-04-14 01:38:48 +02:00
import net.minecraft.world.World;
2020-05-09 12:57:25 +02:00
import net.minecraftforge.common.util.INBTSerializable;
2020-04-14 01:38:48 +02:00
import net.minecraftforge.items.IItemHandler;
2020-05-07 18:30:40 +02:00
import org.apache.commons.lang3.tuple.Pair;
2020-04-14 01:38:48 +02:00
2020-05-09 12:57:25 +02:00
import java.util.ArrayList;
import java.util.Collection;
2020-04-20 13:12:26 +02:00
import java.util.List;
2020-05-07 18:30:40 +02:00
import java.util.function.Function;
import java.util.stream.IntStream;
2020-04-20 13:12:26 +02:00
2020-04-14 01:38:48 +02:00
public final class Utility {
public static <T extends TileEntity> T getTileEntity(Class<T> type, World world, BlockPos pos) {
TileEntity tile = world.getTileEntity(pos);
return type.isInstance(tile) ? (T) tile : null;
}
public static void dropInventory(TileEntity tile, IItemHandler inventory) {
BlockPos pos = tile.getPos();
for (int i = 0; i < inventory.getSlots(); i++) {
ItemStack stack = inventory.getStackInSlot(i);
if (!stack.isEmpty())
InventoryHelper.spawnItemStack(tile.getWorld(), pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
2020-04-14 18:51:43 +02:00
public static Direction getDirectionFromOffset(BlockPos pos, BlockPos other) {
BlockPos diff = pos.subtract(other);
return Direction.getFacingFromVector(diff.getX(), diff.getY(), diff.getZ());
}
2020-04-20 13:12:26 +02:00
public static void addTooltip(String name, List<ITextComponent> tooltip) {
if (Screen.hasShiftDown()) {
String[] content = I18n.format("info." + PrettyPipes.ID + "." + name).split("\n");
for (String s : content)
2020-09-22 19:14:07 +02:00
tooltip.add(new StringTextComponent(s).setStyle(Style.EMPTY.setFormatting(TextFormatting.GRAY)));
2020-04-20 13:12:26 +02:00
} else {
2020-09-22 19:14:07 +02:00
tooltip.add(new TranslationTextComponent("info." + PrettyPipes.ID + ".shift").setStyle(Style.EMPTY.setFormatting(TextFormatting.DARK_GRAY)));
2020-04-20 13:12:26 +02:00
}
}
2020-05-07 18:30:40 +02:00
public static ItemStack transferStackInSlot(Container container, IMergeItemStack merge, PlayerEntity player, int slotIndex, Function<ItemStack, Pair<Integer, Integer>> predicate) {
int inventoryStart = (int) container.inventorySlots.stream().filter(slot -> slot.inventory != player.inventory).count();
int inventoryEnd = inventoryStart + 26;
int hotbarStart = inventoryEnd + 1;
int hotbarEnd = hotbarStart + 8;
Slot slot = container.inventorySlots.get(slotIndex);
if (slot != null && slot.getHasStack()) {
ItemStack newStack = slot.getStack();
ItemStack currentStack = newStack.copy();
if (slotIndex >= inventoryStart) {
// shift into this container here
// mergeItemStack with the slots that newStack should go into
// return an empty stack if mergeItemStack fails
Pair<Integer, Integer> slots = predicate.apply(newStack);
if (slots != null) {
if (!merge.mergeItemStack(newStack, slots.getLeft(), slots.getRight(), false))
return ItemStack.EMPTY;
}
// end custom code
else if (slotIndex >= inventoryStart && slotIndex <= inventoryEnd) {
if (!merge.mergeItemStack(newStack, hotbarStart, hotbarEnd + 1, false))
return ItemStack.EMPTY;
} else if (slotIndex >= inventoryEnd + 1 && slotIndex < hotbarEnd + 1 && !merge.mergeItemStack(newStack, inventoryStart, inventoryEnd + 1, false)) {
return ItemStack.EMPTY;
}
} else if (!merge.mergeItemStack(newStack, inventoryStart, hotbarEnd + 1, false)) {
return ItemStack.EMPTY;
}
if (newStack.isEmpty()) {
slot.putStack(ItemStack.EMPTY);
} else {
slot.onSlotChanged();
}
if (newStack.getCount() == currentStack.getCount())
return ItemStack.EMPTY;
slot.onTake(player, newStack);
return currentStack;
}
return ItemStack.EMPTY;
}
2020-05-09 12:57:25 +02:00
public static ListNBT serializeAll(Collection<? extends INBTSerializable<CompoundNBT>> items) {
ListNBT list = new ListNBT();
for (INBTSerializable<CompoundNBT> item : items)
list.add(item.serializeNBT());
return list;
}
public static <T extends INBTSerializable<CompoundNBT>> List<T> deserializeAll(ListNBT list, Function<CompoundNBT, T> supplier) {
List<T> items = new ArrayList<>();
for (int i = 0; i < list.size(); i++)
items.add(supplier.apply(list.getCompound(i)));
return items;
}
2020-05-07 18:30:40 +02:00
public interface IMergeItemStack {
boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection);
}
2020-04-14 01:38:48 +02:00
}