package wayoftime.bloodmagic.util; import java.util.Locale; import javax.annotation.Nullable; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.FlowingFluidBlock; import net.minecraft.block.NetherPortalBlock; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; import net.minecraft.potion.Effects; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Direction; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvents; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.fluids.IFluidBlock; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.wrapper.PlayerMainInvWrapper; import wayoftime.bloodmagic.api.item.IDemonWillViewer; import wayoftime.bloodmagic.tile.TileInventory; public class Utils { /** * @param tile - The {@link TileInventory} to input the item to * @param player - The player to take the item from. * @return {@code true} if the ItemStack is inserted, {@code false} otherwise * @see #insertItemToTile(TileInventory, PlayerEntity, int) */ public static boolean insertItemToTile(TileInventory tile, PlayerEntity player) { return insertItemToTile(tile, player, 0); } /** * Used for inserting an ItemStack with a stacksize of 1 to a tile's inventory * at slot 0 *
* EG: Block Altar * * @param tile - The {@link TileInventory} to input the item to * @param player - The player to take the item from. * @param slot - The slot to attempt to insert to * @return {@code true} if the ItemStack is inserted, {@code false} otherwise */ public static boolean insertItemToTile(TileInventory tile, PlayerEntity player, int slot) { ItemStack slotStack = tile.getStackInSlot(slot); if (slotStack.isEmpty() && !player.getHeldItemMainhand().isEmpty()) { ItemStack input = player.getHeldItemMainhand().copy(); input.setCount(1); player.getHeldItemMainhand().shrink(1); tile.setInventorySlotContents(slot, input); return true; } else if (!slotStack.isEmpty() && player.getHeldItemMainhand().isEmpty()) { ItemHandlerHelper.giveItemToPlayer(player, slotStack); tile.clear(); return false; } return false; } public static String toFancyCasing(String input) { return String.valueOf(input.charAt(0)).toUpperCase(Locale.ENGLISH) + input.substring(1); } public static boolean isImmuneToFireDamage(LivingEntity entity) { return entity.isImmuneToFire() || entity.isPotionActive(Effects.FIRE_RESISTANCE); } public static boolean isBlockLiquid(BlockState state) { return (state instanceof IFluidBlock || state.getMaterial().isLiquid()); } public static boolean isFlowingLiquid(World world, BlockPos pos, BlockState state) { Block block = state.getBlock(); return ((block instanceof IFluidBlock && Math.abs(((IFluidBlock) block).getFilledPercentage(world, pos)) == 1) || (block instanceof FlowingFluidBlock && !((FlowingFluidBlock) block).getFluidState(state).isSource())); } public static boolean spawnStackAtBlock(World world, BlockPos pos, @Nullable Direction pushDirection, ItemStack stack) { BlockPos spawnPos = new BlockPos(pos); double velX = 0; double velY = 0; double velZ = 0; double velocity = 0.15D; if (pushDirection != null) { spawnPos = spawnPos.offset(pushDirection); switch (pushDirection) { case DOWN: { velY = -velocity; break; } case UP: { velY = velocity; break; } case NORTH: { velZ = -velocity; break; } case SOUTH: { velZ = velocity; break; } case WEST: { velX = -velocity; break; } case EAST: { velX = velocity; break; } } } double posX = spawnPos.getX() + 0.5; double posY = spawnPos.getY() + 0.5; double posZ = spawnPos.getZ() + 0.5; ItemEntity entityItem = new ItemEntity(world, posX, posY, posZ, stack); entityItem.setMotion(velX, velY, velZ); entityItem.setItem(stack); return world.addEntity(entityItem); } public static boolean swapLocations(World initialWorld, BlockPos initialPos, World finalWorld, BlockPos finalPos) { return swapLocations(initialWorld, initialPos, finalWorld, finalPos, true); } public static boolean swapLocations(World initialWorld, BlockPos initialPos, World finalWorld, BlockPos finalPos, boolean playSound) { TileEntity initialTile = initialWorld.getTileEntity(initialPos); TileEntity finalTile = finalWorld.getTileEntity(finalPos); CompoundNBT initialTag = new CompoundNBT(); CompoundNBT finalTag = new CompoundNBT(); if (initialTile != null) initialTile.write(initialTag); if (finalTile != null) finalTile.write(finalTag); BlockState initialState = initialWorld.getBlockState(initialPos); BlockState finalState = finalWorld.getBlockState(finalPos); if ((initialState.getBlock().equals(Blocks.AIR) && finalState.getBlock().equals(Blocks.AIR)) || initialState.getBlock() instanceof NetherPortalBlock || finalState.getBlock() instanceof NetherPortalBlock) return false; if (playSound) { initialWorld.playSound(null, initialPos.getX(), initialPos.getY(), initialPos.getZ(), SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.AMBIENT, 1.0F, 1.0F); finalWorld.playSound(null, finalPos.getX(), finalPos.getY(), finalPos.getZ(), SoundEvents.ENTITY_ENDERMAN_TELEPORT, SoundCategory.AMBIENT, 1.0F, 1.0F); } // Finally, we get to do something! (CLEARING TILES) if (finalState.getBlock().hasTileEntity(finalState)) finalWorld.removeTileEntity(finalPos); if (initialState.getBlock().hasTileEntity(initialState)) initialWorld.removeTileEntity(initialPos); // TILES CLEARED BlockState initialBlockState = initialWorld.getBlockState(initialPos); BlockState finalBlockState = finalWorld.getBlockState(finalPos); finalWorld.setBlockState(finalPos, initialBlockState, 3); if (initialTile != null) { // TileEntity newTileInitial = TileEntity.create(finalWorld, initialTag); TileEntity newTileInitial = TileEntity.readTileEntity(finalBlockState, initialTag); finalWorld.setTileEntity(finalPos, newTileInitial); // newTileInitial.setPos(finalPos); newTileInitial.setWorldAndPos(finalWorld, finalPos); } initialWorld.setBlockState(initialPos, finalBlockState, 3); if (finalTile != null) { // TileEntity newTileFinal = TileEntity.create(initialWorld, finalTag); TileEntity newTileFinal = TileEntity.readTileEntity(initialBlockState, finalTag); initialWorld.setTileEntity(initialPos, newTileFinal); // newTileFinal.setPos(initialPos); newTileFinal.setWorldAndPos(initialWorld, initialPos); } initialWorld.notifyNeighborsOfStateChange(initialPos, finalState.getBlock()); finalWorld.notifyNeighborsOfStateChange(finalPos, initialState.getBlock()); return true; } public static ItemStack insertStackIntoTile(ItemStack stack, TileEntity tile, Direction dir) { LazyOptional