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

47 lines
1.8 KiB
Java
Raw Normal View History

2020-04-14 01:38:48 +02:00
package de.ellpeck.prettypipes;
2020-04-20 13:12:26 +02:00
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resources.I18n;
2020-04-14 01:38:48 +02:00
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
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;
import net.minecraftforge.items.IItemHandler;
2020-04-20 13:12:26 +02:00
import java.util.List;
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)
tooltip.add(new StringTextComponent(s).setStyle(new Style().setColor(TextFormatting.GRAY)));
} else {
tooltip.add(new TranslationTextComponent("info." + PrettyPipes.ID + ".shift").setStyle(new Style().setColor(TextFormatting.DARK_GRAY)));
}
}
2020-04-14 01:38:48 +02:00
}