Jump to content

OBCLetter

Members
  • Posts

    14
  • Joined

  • Last visited

Posts posted by OBCLetter

  1. Hello all. I have a Container that functions similarly to a crafting table; however, you craft things using Matter.

     

    There is a problem. You can only craft once, and after that, the container becomes unusable until the game is restarted (See attachment).

     

    Honestly, I've been working at this for hours on end, and I haven't got anywhere.

     

    ContainerAssembler.java

     
     
     
    Quote
    
    package com.obcletter.chemicanalyzer.inventory;
    
    import com.obcletter.chemicanalyzer.init.ModBlocks;
    import com.obcletter.chemicanalyzer.recipes.AssemblerFactory.AssemblerRecipe;
    
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.EntityPlayerMP;
    import net.minecraft.entity.player.InventoryPlayer;
    import net.minecraft.inventory.Container;
    import net.minecraft.inventory.IInventory;
    import net.minecraft.inventory.InventoryCraftResult;
    import net.minecraft.inventory.InventoryCrafting;
    import net.minecraft.inventory.Slot;
    import net.minecraft.item.ItemStack;
    import net.minecraft.item.crafting.IRecipe;
    import net.minecraft.network.play.server.SPacketSetSlot;
    import net.minecraft.util.ResourceLocation;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.world.World;
    import net.minecraftforge.fml.common.registry.ForgeRegistries;
    
    public class ContainerAssembler extends Container {
    
    	/**
    	 * Crafting Matrix, seems to have dun <strong> D I E D</strong>
    	 */
    	public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
    	public InventoryMatter matter = new InventoryMatter(this, craftMatrix);
    	/**
    	 * Result of the craft
    	 */
    	public InventoryCraftResult craftResult = new InventoryCraftResult();
    
    	/**
    	 * Position of the assembler
    	 */
    	private BlockPos pos;
    	/**
    	 * The current world
    	 */
    	private World world;
    	/**
    	 * The player using the assembler
    	 */
    	private EntityPlayer player;
    
    	private SlotAssembler assemblerSlot;
    
    	/**
    	 * Constructor, mostly for initializing slots
    	 * 
    	 * @param playerInv
    	 *            Player inventory
    	 * @param worldIn
    	 *            World
    	 * @param pos
    	 *            Position of the assembler
    	 */
    	public ContainerAssembler(InventoryPlayer playerInv, World worldIn, BlockPos pos) {
    		this.world = worldIn;
    		this.player = playerInv.player;
    		this.assemblerSlot = new SlotAssembler(this, playerInv.player, this.craftMatrix, this.craftResult, this.matter,
    				0, 125, 52, this.windowId);
    		this.addSlotToContainer(this.assemblerSlot);
    
    		for (int i = 0; i < 3; ++i) {
    			for (int j = 0; j < 3; ++j) {
    				this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));
    			}
    		}
    		this.addSlotToContainer(new Slot(this.matter, 0, 125, 17));
    
    		for (int k = 0; k < 3; ++k) {
    			for (int i1 = 0; i1 < 9; ++i1) {
    				this.addSlotToContainer(new Slot(playerInv, i1 + k * 9 + 9, 8 + i1 * 18, 84 + k * 18));
    			}
    		}
    
    		for (int l = 0; l < 9; ++l) {
    			this.addSlotToContainer(new Slot(playerInv, l, 8 + l * 18, 142));
    		}
    	}
    
    	public void update() {
    
    	}
    
    	@Override
    	public boolean canInteractWith(EntityPlayer playerIn) {
    		if (this.pos != null && this.world.getBlockState(this.pos).getBlock() != ModBlocks.assembler) {
    			return false;
    		} else if (this.pos != null) {
    			return playerIn.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D,
    					(double) this.pos.getZ() + 0.5D) <= 64.0D;
    		} else {
    			return true;
    		}
    	}
    
    	public void onContainerClosed(EntityPlayer playerIn) {
    		super.onContainerClosed(playerIn);
    
    		if (!this.world.isRemote) {
    			this.clearContainer(playerIn, this.world, this.craftMatrix);
    			this.clearContainer(playerIn, this.world, this.matter);
    		}
    	}
    
    	@Override
    	public void onCraftMatrixChanged(IInventory inventoryIn) {
    		if (!this.world.isRemote) {
    			EntityPlayerMP entityplayermp = (EntityPlayerMP) this.player;
    			ItemStack itemstack = ItemStack.EMPTY;
    			for (ResourceLocation location : ForgeRegistries.RECIPES.getKeys()) {
    				IRecipe recipe = ForgeRegistries.RECIPES.getValue(location);
    				if (recipe != null && recipe instanceof AssemblerRecipe) {
    					((AssemblerRecipe) recipe).setMatterSlot(matter);
    					this.craftResult.setRecipeUsed(recipe);
    					itemstack = recipe.getCraftingResult(this.craftMatrix);
    					((AssemblerRecipe) recipe).setMatterSlot(null);
    					assemblerSlot.setCurrentRecipe(((AssemblerRecipe) recipe));
    				}
    			}
    			this.craftResult.setInventorySlotContents(0, itemstack);
    			entityplayermp.connection.sendPacket(new SPacketSetSlot(this.windowId, 0, itemstack));
    		}
    	}
    
    	@Override
    	public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
    		ItemStack itemstack = ItemStack.EMPTY;
    		Slot slot = this.inventorySlots.get(index);
    
    		if (slot != null && slot.getHasStack()) {
    			ItemStack itemstack1 = slot.getStack();
    			itemstack = itemstack1.copy();
    
    			if (index == 0) {
    				itemstack1.getItem().onCreated(itemstack1, this.world, playerIn);
    
    				if (!this.mergeItemStack(itemstack1, 10, 46, true)) {
    					return ItemStack.EMPTY;
    				}
    
    				slot.onSlotChange(itemstack1, itemstack);
    			} else if (index >= 10 && index < 37) {
    				if (!this.mergeItemStack(itemstack1, 37, 46, false)) {
    					return ItemStack.EMPTY;
    				}
    			} else if (index >= 37 && index < 46) {
    				if (!this.mergeItemStack(itemstack1, 10, 37, false)) {
    					return ItemStack.EMPTY;
    				}
    			} else if (!this.mergeItemStack(itemstack1, 10, 46, false)) {
    				return ItemStack.EMPTY;
    			}
    
    			if (itemstack1.isEmpty()) {
    				slot.putStack(ItemStack.EMPTY);
    			} else {
    				slot.onSlotChanged();
    			}
    
    			if (itemstack1.getCount() == itemstack.getCount()) {
    				return ItemStack.EMPTY;
    			}
    
    			ItemStack itemstack2 = slot.onTake(playerIn, itemstack1);
    
    			if (index == 0) {
    				playerIn.dropItem(itemstack2, false);
    			}
    		}
    
    		return itemstack;
    	}
    
    	public boolean canMergeSlot(ItemStack stack, Slot slotIn) {
    		return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
    	}
    }

     

     

    InventoryMatter.java

     
     
     
    Quote
    
    package com.obcletter.chemicanalyzer.inventory;
    
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.inventory.Container;
    import net.minecraft.inventory.IInventory;
    import net.minecraft.inventory.InventoryCrafting;
    import net.minecraft.inventory.ItemStackHelper;
    import net.minecraft.item.ItemStack;
    import net.minecraft.util.NonNullList;
    import net.minecraft.util.text.ITextComponent;
    import net.minecraft.util.text.TextComponentString;
    import net.minecraft.util.text.TextComponentTranslation;
    
    public class InventoryMatter implements IInventory {
    
    	private final NonNullList<ItemStack> stackList;
    
    	private Container eventHandler;
    	private InventoryCrafting crafting;
    
    	public InventoryMatter(Container eventHandler, InventoryCrafting crafting) {
    		this.stackList = NonNullList.withSize(1, ItemStack.EMPTY);
    		this.eventHandler = eventHandler;
    		this.crafting = crafting;
    	}
    
    	@Override
    	public String getName() {
    		return "container.matter";
    	}
    
    	@Override
    	public boolean hasCustomName() {
    		return false;
    	}
    
    	@Override
    	public ITextComponent getDisplayName() {
    		return (ITextComponent) (this.hasCustomName() ? new TextComponentString(this.getName())
    				: new TextComponentTranslation(this.getName(), new Object[0]));
    	}
    
    	@Override
    	public int getSizeInventory() {
    		return 1;
    	}
    
    	@Override
    	public boolean isEmpty() {
    		if (stackList.get(0).isEmpty())
    			return true;
    		return false;
    	}
    
    	@Override
    	public ItemStack getStackInSlot(int index) {
    		return stackList.get(index);
    	}
    
    	@Override
    	public ItemStack decrStackSize(int index, int count) {
    		ItemStack itemstack = ItemStackHelper.getAndSplit(this.stackList, 0, count);
    
    		if (!itemstack.isEmpty()) {
    			this.eventHandler.onCraftMatrixChanged(crafting);
    		}
    
    		return itemstack;
    	}
    
    	@Override
    	public ItemStack removeStackFromSlot(int index) {
    		return ItemStackHelper.getAndRemove(stackList, 0);
    	}
    
    	@Override
    	public void setInventorySlotContents(int index, ItemStack stack) {
    		stackList.set(0, stack);
    		this.eventHandler.onCraftMatrixChanged(crafting);
    	}
    
    	@Override
    	public int getInventoryStackLimit() {
    		return 64;
    	}
    
    	@Override
    	public void markDirty() {
    	}
    
    	@Override
    	public boolean isUsableByPlayer(EntityPlayer player) {
    		return true;
    	}
    
    	@Override
    	public void openInventory(EntityPlayer player) {
    	}
    
    	@Override
    	public void closeInventory(EntityPlayer player) {
    	}
    
    	@Override
    	public boolean isItemValidForSlot(int index, ItemStack stack) {
    		return true;
    	}
    
    	@Override
    	public int getField(int id) {
    		return 0;
    	}
    
    	@Override
    	public void setField(int id, int value) {
    
    	}
    
    	@Override
    	public int getFieldCount() {
    		return 0;
    	}
    
    	@Override
    	public void clear() {
    		this.stackList.clear();
    	}
    
    }

     

     

    SlotAssembler.java

     
     
     
     
     
     
    Quote
    
    package com.obcletter.chemicanalyzer.inventory;
    
    import com.obcletter.chemicanalyzer.recipes.AssemblerFactory.AssemblerRecipe;
    
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.inventory.Container;
    import net.minecraft.inventory.IInventory;
    import net.minecraft.inventory.InventoryCraftResult;
    import net.minecraft.inventory.InventoryCrafting;
    import net.minecraft.inventory.Slot;
    import net.minecraft.item.ItemStack;
    import net.minecraft.item.crafting.IRecipe;
    
    public class SlotAssembler extends Slot {
    	/** The craft matrix inventory linked to this result slot. */
    	private final InventoryCrafting craftMatrix;
    
    	private final InventoryMatter matter;
    	/** The player that is using the GUI where this slot resides. */
    	private final EntityPlayer player;
    
    	private AssemblerRecipe currentRecipe;
    	private Container eventHandler;
    
    	private int windowid;
    	/**
    	 * The number of items that have been crafted so far. Gets passed to
    	 * ItemStack.onCrafting before being reset.
    	 */
    	private int amountCrafted;
    
    	public SlotAssembler(Container eventHandler, EntityPlayer player, InventoryCrafting craftingInventory,
    			IInventory inventoryIn, InventoryMatter matter, int slotIndex, int xPosition, int yPosition, int windowid) {
    		super(inventoryIn, slotIndex, xPosition, yPosition);
    		this.eventHandler = eventHandler;
    		this.player = player;
    		this.craftMatrix = craftingInventory;
    		this.matter = matter;
    		this.windowid = windowid;
    	}
    
    	/**
    	 * Check if the stack is allowed to be placed in this slot, used for armor slots
    	 * as well as furnace fuel.
    	 */
    	public boolean isItemValid(ItemStack stack) {
    		return false;
    	}
    
    	/**
    	 * Decrease the size of the stack in slot (first int arg) by the amount of the
    	 * second int arg. Returns the new stack.
    	 */
    	public ItemStack decrStackSize(int amount) {
    		if (this.getHasStack()) {
    			this.amountCrafted += Math.min(amount, this.getStack().getCount());
    		}
    
    		return super.decrStackSize(amount);
    	}
    
    	/**
    	 * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not
    	 * ore and wood. Typically increases an internal count then calls
    	 * onCrafting(item).
    	 */
    	protected void onCrafting(ItemStack stack, int amount) {
    		this.amountCrafted += amount;
    		this.onCrafting(stack);
    	}
    
    	protected void onSwapCraft(int p_190900_1_) {
    		this.amountCrafted += p_190900_1_;
    	}
    
    	/**
    	 * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not
    	 * ore and wood.
    	 */
    	protected void onCrafting(ItemStack stack) {
    		if (this.amountCrafted > 0) {
    			stack.onCrafting(this.player.world, this.player, this.amountCrafted);
    			net.minecraftforge.fml.common.FMLCommonHandler.instance().firePlayerCraftingEvent(this.player, stack,
    					craftMatrix);
    		}
    	}
    
    	public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack) {
    		this.onCrafting(stack);
    		InventoryCraftResult inventorycraftresult = (InventoryCraftResult) this.inventory;
    		if (this.currentRecipe != null) {
    			matter.decrStackSize(0, this.currentRecipe.getRequiredMatter());
    		}
    		inventorycraftresult.setRecipeUsed((IRecipe) null);
    		eventHandler.onCraftMatrixChanged(craftMatrix);
    		return stack;
    	}
    
    	public void setCurrentRecipe(AssemblerRecipe currentRecipe) {
    		this.currentRecipe = currentRecipe;
    	}
    }

     

     

    GuiAssembler.java (probably the most pointless one to showcase)

     
     
     
    Quote
    
    package com.obcletter.chemicanalyzer.client.gui.inventory;
    
    import com.obcletter.chemicanalyzer.ModMain;
    import com.obcletter.chemicanalyzer.inventory.ContainerAssembler;
    
    import net.minecraft.client.gui.inventory.GuiContainer;
    import net.minecraft.client.resources.I18n;
    import net.minecraft.entity.player.InventoryPlayer;
    import net.minecraft.util.ResourceLocation;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.world.World;
    
    public class GuiAssembler extends GuiContainer {
    	private static final ResourceLocation ASSEMBLER_GUI_TEXTURES = new ResourceLocation(
    			ModMain.MODID + ":textures/gui/assembler.png");
    
    	public GuiAssembler(InventoryPlayer playerInv, World worldIn, BlockPos pos) {
    		super(new ContainerAssembler(playerInv, worldIn, pos));
    	}
    
    	@Override
    	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
    		super.drawDefaultBackground();
    		this.mc.getTextureManager().bindTexture(ASSEMBLER_GUI_TEXTURES);
    		int i = this.guiLeft;
    		int j = (this.height - this.ySize) / 2;
    		this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
    	}
    
    	@Override
    	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
    		this.fontRenderer.drawString(I18n.format("container.assembler"), 28, 6, 4210752);
    		this.fontRenderer.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
    	}
    }

     

     

  2. Hi everyone. I am making a mod where there is a block that can "mask" itself so that people can't find it. However, I cannot seem to get the code to work, no matter how hard I try. I have tried a

    TileEntitySpecialRenderer

    , however this wouldn't work, I also have tried storing the blockstate and simply returning the blockstate in the tileentity in

    getExtendedState()

     and

    getActualState()

    , but nothing works. I don't understand why. A little help would help me greatly.

    TileEntity:

    Quote
    
    package com.obcletter.moddingchallenge.block.explosive_clay;
    
    import com.obcletter.moddingchallenge.Reference;
    import com.obcletter.moddingchallenge.tile.TileEntityBase;
    
    import net.minecraft.block.state.IBlockState;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.nbt.NBTUtil;
    
    public class TileEntityExplosiveClay extends TileEntityBase {
    
        private static final String DEFAULT_BLOCK_VALUE = Reference.MOD_ID + ":explosive_clay";
    
        private IBlockState state;
    
        @Override
        public void readFromNBT(NBTTagCompound compound) {
            state = NBTUtil.readBlockState(compound.getCompoundTag("state"));
            System.out.println(state.getBlock().getRegistryName().toString());
            super.readFromNBT(compound);
        }
    
        @Override
        public NBTTagCompound writeToNBT(NBTTagCompound compound) {
            NBTTagCompound stateTag = new NBTTagCompound();
            if (state != null) {
                System.out.println(state.getBlock().getRegistryName().toString());
                NBTUtil.writeBlockState(stateTag, state);
                compound.setTag("state", stateTag);
            }
            return super.writeToNBT(compound);
        }
    
        public IBlockState getState() {
            return state;
        }
    
        public boolean setState(IBlockState state) {
            this.state = state;
            this.markDirty();
            return true;
        }
    }

     

    Block:

    Quote
    
    package com.obcletter.moddingchallenge.block.explosive_clay;
    
    import com.obcletter.moddingchallenge.block.BlockTileEntity;
    
    import net.minecraft.block.Block;
    import net.minecraft.block.SoundType;
    import net.minecraft.block.material.Material;
    import net.minecraft.block.state.IBlockState;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.init.Blocks;
    import net.minecraft.item.ItemStack;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.EnumFacing;
    import net.minecraft.util.EnumHand;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.world.IBlockAccess;
    import net.minecraft.world.World;
    
    public class BlockExplosiveClay extends BlockTileEntity<TileEntityExplosiveClay> {
    
        public BlockExplosiveClay() {
            super("explosive_clay", Material.CLAY);
            this.setSoundType(SoundType.GROUND);
        }
    
        @Override
        public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
                EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
            if (!worldIn.isRemote) {
                TileEntity te = worldIn.getTileEntity(pos);
                if (te instanceof TileEntityExplosiveClay) {
                    TileEntityExplosiveClay explosive_clay = (TileEntityExplosiveClay) te;
                    ItemStack stack = playerIn.getHeldItem(hand);
                    IBlockState staate = Block.getBlockFromItem(stack.getItem()).getStateFromMeta(stack.getMetadata());
                    if (staate != Blocks.AIR.getDefaultState()) {
                        return explosive_clay.setState(staate);
    
                    } else {
                        return explosive_clay.setState(getDefaultState());
                    }
                }
            }
            return false;
        }
    
        @Override
        public Class<TileEntityExplosiveClay> getTileEntityClass() {
            return TileEntityExplosiveClay.class;
        }
    
        @Override
        public TileEntity createNewTileEntity(World worldIn, int meta) {
            return new TileEntityExplosiveClay();
        }
        
        @Override
        public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) {
            TileEntity entity = world.getTileEntity(pos);
            if (entity instanceof TileEntityExplosiveClay) {
                TileEntityExplosiveClay explosiveTe = (TileEntityExplosiveClay) entity;
                if (explosiveTe.getState() != null) {
                    System.out.println(explosiveTe.getState().getBlock().getRegistryName().toString());
                    return explosiveTe.getState();
                }
            }
            return super.getExtendedState(state, world, pos);
        }
    
    }

     

     

  3. When I try to extract an item from a furnace, the furnace is broken. Not like glitched broken, but like survival player mines the block broken. Here is my code:

    	@Override
    	public void updateTask() {
    		if (soldier.wantsToGoTo != BlockPos.NULL_VECTOR) {
    			soldier.getNavigator().tryMoveToXYZ(soldier.wantsToGoTo.getX(), soldier.wantsToGoTo.getY(),
    					soldier.wantsToGoTo.getZ(), speedIn);
    			double distance = soldier.getPosition().getDistance(soldier.wantsToGoTo.getX(), soldier.wantsToGoTo.getY(),
    					soldier.wantsToGoTo.getZ());
    			if (distance < 5) {
    				TileEntityFurnace furnace = soldier.getFurnace();
    				if (soldier.getHeldItem(EnumHand.MAIN_HAND).isEmpty()) {
    					soldier.setHeldItem(EnumHand.MAIN_HAND, furnace.getStackInSlot(2));
    					furnace.getStackInSlot(2).splitStack(furnace.getStackInSlot(2).getCount());
    				} else if (soldier.getHeldItem(EnumHand.MAIN_HAND) == furnace.getStackInSlot(2)) {
    					soldier.setHeldItem(EnumHand.MAIN_HAND, new ItemStack(furnace.getStackInSlot(2).getItem(),
    							furnace.getStackInSlot(2).getCount() + soldier.getHeldItem(EnumHand.MAIN_HAND).getCount()));
    					furnace.getStackInSlot(2).splitStack(furnace.getStackInSlot(2).getCount());
    				}
    
    			}
    		}
    	}

     

  4. Just now, Draco18s said:

    Also:

     

    BlockPos.getAllInBox(...) is a thing.

     

    5 minutes ago, jabelar said:

    you made a typo in your loop. when you cut and pasted you left the second limit as "i" in all the loops, when it should be "j" and "k".

    You guys are life-savers.

  5. I think I may have done something completely wrong:

    	@Override
    	public boolean shouldExecute() {
    		boolean output = false;
    		BlockPos position = soldier.getPosition();
    		for (int i = position.getX() + 2; i > position.getX() - 2; i--) {
    			for (int j = position.getZ() + 2; i > position.getZ() - 2; j--) {
    				for (int k = position.getY() + 2; i > position.getY() - 2; k--) {
    					BlockPos foundBlockPos = new BlockPos(i + position.getX(), k + position.getY(),
    							j + position.getZ());
    					if (world.getBlockState(foundBlockPos).equals(stateToFind)) {
    						output = true;
    						soldier.wantsToGoTo = foundBlockPos;
    					}
    				}
    			}
    		}
    		return output;
    	}

     

  6. I have an entity that tries to find a block, in this case wood, to mine and use for other purposes. I thought of checking every single block in a 5x5x5* area (above ground), but I quickly noticed it would slow my game to a snail's pace. How could I optimize my block-checking?

     

    EDIT: *4x4x4

×
×
  • Create New...

Important Information

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