Jump to content

[1.12.2] Slot rendering different slot's items


Cadiboo

Recommended Posts

Title says it all basically, probably some small thing that I've failed to do

Slot 1 renders slot 9's itemstack

5aa6656db9c5e_ScreenShot2018-03-12at10_27_20pm.png.0bd741bded03f1f567fe695350dfb48f.png5aa6646a40ef0_ScreenShot2018-03-12at10_27_12pm.png.53d7724a16bf595a905e4cbfab5ac86b.png5aa66468e6d20_ScreenShot2018-03-12at10_27_20pm.png.d0b975e96bcf86284704b2a90906e950.png5aa6646b1027b_ScreenShot2018-03-12at10_27_08pm.png.cf85ec4ffef5c3384ed1c1ca70d95739.png

 

ContainerCoiler.java

package cadiboo.wiptech.block.coiler;

import javax.annotation.Nonnull;

import cadiboo.wiptech.WIPTech;
import cadiboo.wiptech.init.Items;
import cadiboo.wiptech.init.Recipes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class ContainerCoiler extends Container {

	public ContainerCoiler(InventoryPlayer playerInv, final TileEntityCoiler coiler) {
		IItemHandler inventory = coiler.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);


		for(int i=0; i<10; i++) {
			
			if ( (i & 1) == 0 ) // even - Input slot
			{
				addSlotToContainer(new SlotItemHandler(inventory, i, 19 + 30*i/2, 17) {
					@Override
					public void onSlotChanged() {
						coiler.markDirty();
					}
				});

			}
			else
			{
				addSlotToContainer(new SlotItemHandler(inventory, i, 19 + 30*((i-1)/2), 53) {
					/*@Override
					public void onSlotChanged() {
						coiler.markDirty();
					}
					
					@Override
					public boolean isItemValid(@Nonnull ItemStack stack){
						return Recipes.getCoilResult(stack)!=null && Recipes.getCoilResult(stack).size()>0;
					}*/
				});
			}
		}

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 9; j++) {
				addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
			}
		}

		for (int k = 0; k < 9; k++) {
			addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142));
		}
	}

	@Override
	public boolean canInteractWith(EntityPlayer player) {
		return true;
	}

	@Override
	public ItemStack transferStackInSlot(EntityPlayer player, int index) {
		ItemStack itemstack = ItemStack.EMPTY;
		Slot slot = inventorySlots.get(index);

		if (slot != null && slot.getHasStack()) {
			ItemStack itemstack1 = slot.getStack();
			itemstack = itemstack1.copy();

			int containerSlots = inventorySlots.size() - player.inventory.mainInventory.size();

			if (index < containerSlots) {
				if (!this.mergeItemStack(itemstack1, containerSlots, inventorySlots.size(), true)) {
					return ItemStack.EMPTY;
				}
			} else if (!this.mergeItemStack(itemstack1, 0, containerSlots, false)) {
				return ItemStack.EMPTY;
			}

			if (itemstack1.getCount() == 0) {
				slot.putStack(ItemStack.EMPTY);
			} else {
				slot.onSlotChanged();
			}

			if (itemstack1.getCount() == itemstack.getCount()) {
				return ItemStack.EMPTY;
			}

			slot.onTake(player, itemstack1);
		}

		return itemstack;
	}


}

 

BlockCoiler.java

package cadiboo.wiptech.block.coiler;

import java.util.List;
import java.util.Random;

import javax.annotation.Nullable;

import cadiboo.wiptech.ModGuiHandler;
import cadiboo.wiptech.WIPTech;
import cadiboo.wiptech.block.BlockTileEntity;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class BlockCoiler extends BlockTileEntity<TileEntityCoiler>{

	public BlockCoiler (String name, Material material) {
		super(name, material);
		this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
		this.setTileEntity();
		this.setNonSolidBlock();
	}

	@Override
	public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		tooltip.add("\u00A76\u00A7o"+"Takes Spools and winds them into high performance coils.");
	}

	public static final PropertyDirection FACING = BlockHorizontal.FACING;

	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
	{
		if (TileEntityCoiler.isWinding(this.getTileEntity(worldIn, pos)))
		{
			EnumFacing enumfacing = (EnumFacing)stateIn.getValue(FACING);
			double x = (double)pos.getX() + 0.5D;
			double y = (double)pos.getY() + 0.5D;
			double z = (double)pos.getZ() + 0.5D;
			double d4 = rand.nextDouble() * 0.6D - 0.3D;

			if (rand.nextDouble() < 0.1D)
			{
				worldIn.playSound((double)pos.getX() + 0.5D, (double)pos.getY(), (double)pos.getZ() + 0.5D, SoundEvents.BLOCK_PISTON_EXTEND, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
			}

			worldIn.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, x + d4, y+0.4+(d4/10), y + d4, 0.0D, 0.0D, 0.0D);

		}
	}

	private double randomBetween(int min, int max) {
		return new Random().nextInt((int) ((max - min)+1) )+min;
	}

	@Override
	public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
	{
		if (!world.isRemote) {
			TileEntityCoiler tile = getTileEntity(world, pos);
			IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
			WIPTech.logger.info("player.isSneaking(): "+player.isSneaking());
			if (!player.isSneaking()) {
				player.openGui(WIPTech.instance, ModGuiHandler.COILER, world, pos.getX(), pos.getY(), pos.getZ());
			}
		}
		return true;
	}

	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
	{
		this.setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
	{
		if (!worldIn.isRemote)
		{
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
			{
				enumfacing = EnumFacing.SOUTH;
			}
			else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
			{
				enumfacing = EnumFacing.NORTH;
			}
			else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
			{
				enumfacing = EnumFacing.EAST;
			}
			else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
			{
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
	{
		return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}


	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
	{
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
	}

	public IBlockState getStateFromMeta(int meta)
	{
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y)
		{
			enumfacing = EnumFacing.NORTH;
		}

		return this.getDefaultState().withProperty(FACING, enumfacing);
	}

	/**
	 * Convert the BlockState into the correct metadata value
	 */
	public int getMetaFromState(IBlockState state)
	{
		return ((EnumFacing)state.getValue(FACING)).getIndex();
	}

	/**
	 * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
	 * blockstate.
	 */
	public IBlockState withRotation(IBlockState state, Rotation rot)
	{
		return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
	}

	/**
	 * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
	 * blockstate.
	 */
	public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
	{
		return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
	}

	protected BlockStateContainer createBlockState()
	{
		return new BlockStateContainer(this, new IProperty[] {FACING});
	}

	@Override
	public void breakBlock(World world, BlockPos pos, IBlockState state) {
		TileEntityCoiler tile = getTileEntity(world, pos);
		IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);
		for(int i = 0; i<2; i++) {
			ItemStack stack = itemHandler.getStackInSlot(i);
			if (!stack.isEmpty()) {
				EntityItem item = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), stack);
				world.spawnEntity(item);
			}
		}
		super.breakBlock(world, pos, state);
	}

	@Nullable
	@Override
	public TileEntityCoiler createTileEntity(World world, IBlockState state) {
		return new TileEntityCoiler();
	}

}


 

TileEntityCoiler.java

package cadiboo.wiptech.block.coiler;

import java.util.ArrayList;

import javax.annotation.Nullable;

import cadiboo.wiptech.WIPTech;
import cadiboo.wiptech.block.coiler.TileEntityCoiler;
import cadiboo.wiptech.init.Items;
import cadiboo.wiptech.init.Recipes;
import cadiboo.wiptech.network.PacketRequestUpdateCoiler;
import cadiboo.wiptech.network.PacketUpdateCoiler;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityCoiler extends TileEntity implements ITickable {

	public float WindTime;
	//private int activeSlot = -1;
	public long lastChangeTime;

	public ItemStackHandler inventory = new ItemStackHandler(10) {
		@Override
		protected void onContentsChanged(int slot) {
			if (!world.isRemote) {
				lastChangeTime = world.getTotalWorldTime();
				WIPTech.network.sendToAllAround(new PacketUpdateCoiler(TileEntityCoiler.this), new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64));
			}
		};
	};


	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		compound.setTag("inventory", inventory.serializeNBT());
		compound.setLong("lastChangeTime", lastChangeTime);
		compound.setFloat("WindTime", WindTime);
		return super.writeToNBT(compound);
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		inventory.deserializeNBT(compound.getCompoundTag("inventory"));
		lastChangeTime = compound.getLong("lastChangeTime");
		WindTime = compound.getFloat("WindTime");
		super.readFromNBT(compound);
	}

	@Override
	public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
	}

	@Nullable
	@Override
	public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
		return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)inventory : super.getCapability(capability, facing);
	}

	@Override
	public void onLoad() {
		if (world.isRemote) {
			WIPTech.network.sendToServer(new PacketRequestUpdateCoiler(this));
		}
	}

	@Override
	public AxisAlignedBB getRenderBoundingBox() {
		return new AxisAlignedBB(getPos(), getPos().add(1, 1, 1));
	}

	@Override
	public void update() {
		if(world.getBlockState(pos).getBlock() != this.getBlockType()) return;
		if(WindTime > 0) {
			--WindTime;
			if(!canCoil()) {
				WindTime = 0;
			}
			else if (WindTime <= 0) {
				WindItem();
			}
		}
		else {
			if(canCoil())
			{
				this.setWindTime((int) Recipes.getCoilResult(inventory.getStackInSlot(1)).get(7) * 1.0F);
			} else {
				WindTime = 0;
			}
		}
	}

	private void WindItem()
	{
		if(inventory.getStackInSlot(0) != null && !inventory.getStackInSlot(1).isEmpty()){

			/*ItemStack stack = inventory.getStackInSlot(0);
			ArrayList resultList = (ArrayList) Recipes.getCoilResult(inventory.getStackInSlot(1));
			if(resultList !=null) {
				inventory.insertItem(2, ((ItemStack) resultList.get(1)).copy(), false);
				inventory.insertItem(3, ((ItemStack) resultList.get(2)).copy(), false);
				inventory.insertItem(4, ((ItemStack) resultList.get(3)).copy(), false);
				inventory.insertItem(5, ((ItemStack) resultList.get(4)).copy(), false);
				inventory.insertItem(6, ((ItemStack) resultList.get(5)).copy(), false);
				inventory.insertItem(7, ((ItemStack) resultList.get(6)).copy(), false);
				inventory.extractItem(1, 1, false);
			} else {
				WIPTech.logger.info("ERROR resultList =null so could NOT Coil ITEM");
			}
			return;*/
		} else {
			WIPTech.logger.info("ERROR COULD NOT Coil ITEM");
			WindTime = 0;
			return;
		}
	}

	private boolean canCoil() {
		if(inventory.getStackInSlot(0) != null && !inventory.getStackInSlot(0).isEmpty())
		{
			
			/*ItemStack stack = inventory.getStackInSlot(0);
			ArrayList resultsList = (ArrayList) Recipes.getCoilResult(inventory.getStackInSlot(1));

			boolean recipeExists = resultsList!=null;
			if(recipeExists) {
				boolean slot0 = !inventory.getStackInSlot(0).isEmpty();
				boolean slot1 = !inventory.getStackInSlot(1).isEmpty();
				boolean slot2 = inventory.getStackInSlot(2).getCount() < inventory.getStackInSlot(2).getMaxStackSize();
				boolean slot3 = inventory.getStackInSlot(3).getCount() < inventory.getStackInSlot(3).getMaxStackSize();
				boolean slot4 = inventory.getStackInSlot(4).getCount() < inventory.getStackInSlot(4).getMaxStackSize();
				boolean slot5 = inventory.getStackInSlot(5).getCount() < inventory.getStackInSlot(5).getMaxStackSize();
				boolean slot6 = inventory.getStackInSlot(6).getCount() < inventory.getStackInSlot(6).getMaxStackSize();
				boolean slot7 = inventory.getStackInSlot(7).getCount() < inventory.getStackInSlot(7).getMaxStackSize();

				if(slot0 && slot1 && slot2 && slot3 && slot4 && slot5 && slot6 && slot7) {
					recipeExists = recipeExists && resultsList.size()>0;
					if(recipeExists) {
						recipeExists = recipeExists && resultsList.get(0)!=null;
						if(recipeExists) {
							slot2 = slot2&& (ItemStack) resultsList.get(1)!=null;
							slot3 = slot3&& (ItemStack) resultsList.get(2)!=null;
							slot4 = slot4&& (ItemStack) resultsList.get(3)!=null;
							slot5 = slot5&& (ItemStack) resultsList.get(4)!=null;
							slot6 = slot6&& (ItemStack) resultsList.get(5)!=null;
							slot7 = slot7&& (ItemStack) resultsList.get(6)!=null;
							if(slot0 && slot1 && slot2 && slot3 && slot4 && slot5 && slot6 && slot7) {
								slot2 = slot2&& inventory.insertItem(2, ((ItemStack) resultsList.get(1)).copy(), true).isEmpty();
								slot3 = slot3&& inventory.insertItem(3, ((ItemStack) resultsList.get(2)).copy(), true).isEmpty();
								slot4 = slot4&& inventory.insertItem(4, ((ItemStack) resultsList.get(3)).copy(), true).isEmpty();
								slot5 = slot5&& inventory.insertItem(5, ((ItemStack) resultsList.get(4)).copy(), true).isEmpty();
								slot6 = slot6&& inventory.insertItem(6, ((ItemStack) resultsList.get(5)).copy(), true).isEmpty();
								slot7 = slot7&& inventory.insertItem(7, ((ItemStack) resultsList.get(6)).copy(), true).isEmpty();
								if(slot0 && slot1 && slot2 && slot3 && slot4 && slot5 && slot6 && slot7) {
									return true;
								}
							}
						}
					}
				}
			}*/

		}
		return false;
	}

	public static boolean isWinding(TileEntityCoiler tileEntity) {
		//return ((BlockCoiler) tileEntity.getWorld().getBlockState(tileEntity.getPos()).getBlock()).isCoiling();
		return tileEntity.isWinding();
	}
	private boolean isWinding() {
		return WindTime > 0;
	}

	public static float getCoilTime(TileEntityCoiler tileEntity) {
		//return (tileEntity.getWorld().getBlockState(tileEntity.getPos()).getBlock());
		return tileEntity.getWindTime();
	}
	private float getWindTime() {
		return WindTime;
	}

	public void setWindTime(TileEntityCoiler tileEntity, float time) {
		tileEntity.setWindTime(time);
	}
	private void setWindTime(float time) {
		WindTime = time;
	}

	public static int getTotalWindTime(TileEntityCoiler tileEntity) {
		return tileEntity.getTotalWindTime();
	}
	private int getTotalWindTime() {
		return (int) (!inventory.getStackInSlot(1).isEmpty()?Recipes.getCoilResult(inventory.getStackInSlot(1)).get(7):0);
	}

	public static int getPercentageOfWindTimeComplete(TileEntityCoiler tileEntity) {
		return tileEntity.getPercentageOfWindTimeComplete();
	}
	private int getPercentageOfWindTimeComplete() {
		return (int) Math.round(getFractionOfWindTimeComplete()*100);
	}

	public static double getFractionOfWindTimeComplete(TileEntityCoiler tileEntity) {
		return tileEntity.getFractionOfWindTimeComplete();
	}
	private double getFractionOfWindTimeComplete() {
		if(getWindTime()>0)
			return (getTotalWindTime() - getWindTime())/getTotalWindTime();
		else
			return 0;
	}

	public static int getPercentageOfWindTimeRemaining(TileEntityCoiler tileEntity) {
		return tileEntity.getPercentageOfWindTimeRemaining();
	}
	private int getPercentageOfWindTimeRemaining() {
		return (int) Math.round(getFractionOfWindTimeRemaining()*100);
	}

	public static double getFractionOfWindTimeRemaining(TileEntityCoiler tileEntity) {
		return tileEntity.getFractionOfWindTimeRemaining();
	}
	private double getFractionOfWindTimeRemaining() {
		//Utils.getLogger().info("getFractionOfWindTimeRemaining: "+getWindTime() / getTotalWindTime());
		if(getWindTime()>0)
			return getWindTime() / getTotalWindTime();
		else
			return getTotalWindTime();
	}

}

 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.