Jump to content

Container does task 2 times


invizzble

Recommended Posts

Hello,

 

I've been having a problems with my custom machine in it's interface. Whenever i try to do something with a container (for example add an item), it does that task 2 times. So if i put 1 item in, there are 2 items in the container. And if i right click, it takes a fourth of the stack. My conclussion was that it should excecute 2 times (probably serverside and clientside), but i can't seem to find where. Could you guys please take a look and help me? Anyway thanks for reading.

 

Main class:

package com.invizzble.SC;

import com.invizzble.SC.block.ModBlocks;
import com.invizzble.SC.handler.ConfigurationHandler;
import com.invizzble.SC.handler.GUIHandler;
import com.invizzble.SC.item.ModItems;
import com.invizzble.SC.lib.Info;
import com.invizzble.SC.proxies.CommonProxy;
import com.invizzble.SC.recipes.ShapedRecipes;
import com.invizzble.SC.tileEntities.ModTileEntities;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

@Mod(modid = Info.MOD_ID , name=Info.MOD_NAME,version = Info.MOD_VERSION)

public class SC {

@SidedProxy(clientSide = Info.PROXY_CLIENT, serverSide = Info.PROXY_SERVER)
public static CommonProxy proxy;

@Instance(Info.MOD_ID)
public static SC instance;

@EventHandler
public void preInit(FMLPreInitializationEvent event){

	ConfigurationHandler.init(event.getSuggestedConfigurationFile());

	FMLCommonHandler.instance().bus().register(new ConfigurationHandler());

	//adds the Blocks
	ModBlocks.init();

	//adds the Items
	ModItems.init();

}

@EventHandler
public void init(FMLInitializationEvent event){
	//registers the tile entities
	ModTileEntities.registerTileEntities();

	//registers the recipes for my mod's Items
	ShapedRecipes.init();

	//registers the creativeTab(s) <- at this point there's only one buth there's room for expansion if i implement the different sciences

	//register the gui handler
	new GUIHandler();

}

@EventHandler
public void postInit(FMLPostInitializationEvent event){

}
}

 

GUIHandler:

package com.invizzble.SC.handler;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.World;

import com.invizzble.SC.SC;
import com.invizzble.SC.client.gui.ContainerMachine;
import com.invizzble.SC.client.gui.GUISciPad;
import com.invizzble.SC.client.gui.GuiMachine;
import com.invizzble.SC.item.ItemSciPad;
import com.invizzble.SC.tileEntities.TileEntityMachine;
import com.invizzble.SC.util.LogHelper;

import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class GUIHandler implements IGuiHandler{

public GUIHandler(){
	NetworkRegistry.INSTANCE.registerGuiHandler(SC.instance, this);
}

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	switch(ID){
	case 1:
		TileEntity te = world.getTileEntity(x, y, z);
		if(te != null && te instanceof TileEntityMachine){
			return new ContainerMachine(player.inventory, (TileEntityMachine)te);
		}
		break;
	}
	return null;
}

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	switch(ID){
	case 1:
		TileEntity te = world.getTileEntity(x, y, z);
		if(te != null && te instanceof TileEntityMachine){
			return new GuiMachine(new ContainerMachine(player.inventory, (TileEntityMachine)world.getTileEntity(x, y, z)), (TileEntityMachine)world.getTileEntity(x, y, z));
		}
		break;
	case 2:
		Item it = player.getCurrentEquippedItem().getItem();
		if(it != null && it instanceof ItemSciPad){
			return new GUISciPad();
		}
	}
	return null;
}

}

 

MachineContainer:

package com.invizzble.SC.client.gui;

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 com.invizzble.SC.tileEntities.TileEntityMachine;

public class ContainerMachine extends Container{

private TileEntityMachine machine;

public ContainerMachine(InventoryPlayer invPlayer, TileEntityMachine machine){
	this.machine = machine;
	//Slot(IInventory (the machine),int slotId, xCord, yCord)
	addSlotToContainer(new SlotMachine(machine, 0, 26, 17));

	addSlotToContainer(new SlotMachine(machine, 1, 26, 53));

	addSlotToContainer(new SlotMachine(machine, 2, 116, 35));

	//hotbar
	for(int i = 0; i < 9; i++){
		addSlotToContainer(new Slot(invPlayer, i, i*18+8, 142));
	}

	//player inv
	for (int y = 0; y < 3; y++){
		for(int x = 0; x < 9; x++){
			addSlotToContainer(new Slot(invPlayer, (x+9)+y*9, x*18+8, y*18+84));
		}
	}
}


@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
	// TODO Auto-generated method stub
	return machine.isUseableByPlayer(entityplayer);
}

@Override 
public ItemStack transferStackInSlot(EntityPlayer player, int i){
	return null;
}
}

 

Machine gui:

package com.invizzble.SC.client.gui;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import com.invizzble.SC.block.BlockAtomizer;
import com.invizzble.SC.lib.BlockInfo;
import com.invizzble.SC.tileEntities.TileEntityMachine;

public class GuiMachine extends GuiContainer {

static String textureName;
private static final ResourceLocation texture=new ResourceLocation(BlockInfo.TEXTURE_LOCATION, "textures/gui/machine.png");

TileEntityMachine machine;

public GuiMachine(Container container, TileEntityMachine _machine) {
	super(container);
	xSize = 176;
	ySize = 166;		
	machine = _machine;
}

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	GL11.glColor4f(1, 1, 1, 1);

	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);

	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

	if(machine.getBlockType() instanceof BlockAtomizer){

	}

}

}

 

BlockMachine :

package com.invizzble.SC.block;

import net.minecraft.block.material.Material;
import net.minecraft.client.gui.inventory.GuiFurnace;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import com.invizzble.SC.SC;
import com.invizzble.SC.lib.Info;
import com.invizzble.SC.tileEntities.TileEntityMachine;

import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public abstract class SCBlockMachine extends SCBlock{
IIcon front;
IIcon defaultIcon;
String frontName;
String defaultName;

int guiId;

TileEntityMachine machine;

public SCBlockMachine(){
	super(Material.iron);
	machine = new TileEntityMachine();
	GuiFurnace f;
}

public void setIcons(String _frontName, String _defaultName){
	frontName = _frontName;
	defaultName = _defaultName;
}

@SideOnly(Side.CLIENT)
@Override
public void registerBlockIcons(IIconRegister p_149651_1_)
{
	front= p_149651_1_.registerIcon(Info.MOD_ID+":"+frontName);
	defaultIcon = p_149651_1_.registerIcon(Info.MOD_ID+":"+defaultName);
}

/**
 * Side: 
 * 		0:Top
 * 		1:Bottom
 * 		2:North
 * 		3:East
 * 		4:South
 * 		5:West
 **/
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon(int side, int meta)
    {
	return (side != 1 && side != 0)?front:defaultIcon;
    }

protected abstract void setMachineProperties();

@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
	return machine;
}

protected void setGuiId(int id){
	guiId = id;
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ){
	if(!world.isRemote){
		FMLNetworkHandler.openGui(player, SC.instance, guiId, world, x, y, z);
	}
	return true;	
}

}

 

TileEntityMachine

package com.invizzble.SC.tileEntities;

import java.util.ArrayList;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;

import com.invizzble.SC.block.BlockAtomizer;
import com.invizzble.SC.util.LogHelper;

public class TileEntityMachine extends TileEntity implements ISidedInventory {

private ItemStack[] items;

private ArrayList<ItemStack> allowedItems;
int maxStackSize;
String name;

int powerCapacity;
int powerAmount;
int powerUsage;
int maxPowerTransfer;

public TileEntityMachine(){
	 items = new ItemStack[3];
}

public void setMachineName(String _name) {
	name = _name;
}

public void addAllowedItem(ItemStack item) {
	allowedItems.add(item);
}

/**
 * 
 * @param maxTransfer
 *            the maximum amount power that can be transported at one time
 */
public void setMaxTransfer(int maxTransfer) {
	maxPowerTransfer = maxTransfer;
}

public void setMaxStackSize(int _maxStackSize) {
	maxStackSize = _maxStackSize;
}

public void setPowerCapacity(int capacity) {
	powerCapacity = capacity;
}

public void setPowerUsage(int usage) {
	powerUsage = usage;
}

@Override
public int getSizeInventory() {
	return items.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	return items[slot];
}

@Override
public ItemStack decrStackSize(int i, int count) {
	ItemStack itemstack = getStackInSlot(i);
	if (itemstack != null) {
		if (itemstack.stackSize <= count) {
			setInventorySlotContents(i, null);
		}else{
			itemstack = itemstack.splitStack(count);
		}
	}

	return itemstack;
}


@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	if (items[slot] != null) {
		ItemStack stack = items[slot];
		items[slot] = null;
		return stack;
	} else {
		return null;
	}
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	items[slot] = stack;

	if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
		stack.stackSize = this.getInventoryStackLimit();
	}
}

@Override
public String getInventoryName() {

	return name;
}

@Override
public boolean hasCustomInventoryName() {

	return name != null ? true : false;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) <= 64;
}

@Override
public void openInventory() {
}

@Override
public void closeInventory() {
}

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
	return slot == 2?false:true;
}

@Override
public int[] getAccessibleSlotsFromSide(int side) {
	return null;
}

@Override
public boolean canInsertItem(int slot, ItemStack stack, int side) {
	return isItemValidForSlot(slot, stack);
}

@Override
public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_,
		int p_102008_3_) {
	return true;
}

@Override
public void updateEntity() {
	//processItem();

}

@Override
public void writeToNBT(NBTTagCompound tag) {
	super.writeToNBT(tag);
	NBTTagList list = new NBTTagList();
	for (int i = 0; i < items.length; i++) {
		if(getStackInSlot(i) != null){
			NBTTagCompound nbtTag = new NBTTagCompound();
			nbtTag.setByte("Slot", (byte) i);
			items[i].writeToNBT(nbtTag);
			list.appendTag(nbtTag);
		}
	}
	tag.setTag("Items", list);
}

@Override
public void readFromNBT(NBTTagCompound tag) {
	super.readFromNBT(tag);
	NBTTagList tagList = tag.getTagList("Items", 10);

	for (int i = 0; i < tagList.tagCount(); i++) {
		NBTTagCompound nbttagcompound1 = tagList.getCompoundTagAt(i);
		byte slot = nbttagcompound1.getByte("Slot");

		if (slot >= 0 && slot < items.length) {
			items[slot] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
		}
	}
}

public boolean canProcess(){
	if(getStackInSlot(0) != null){
		ItemStack stack = null;
		if(getBlockType() instanceof BlockAtomizer){
			stack = FurnaceRecipes.smelting().getSmeltingResult(getStackInSlot(0));
		}

		if(stack != null){
			if(getStackInSlot(2) == null || (getStackInSlot(2) == stack && getStackInSlot(2).stackSize + stack.stackSize <= getInventoryStackLimit())){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}else{
		return false;
	}
}

 public void processItem()
    {
        if (this.canProcess())
        {
            ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(items[0]);

            if (items[2] == null)
            {
                items[2] = itemstack.copy();
            }
            else if (items[2].getItem() == itemstack.getItem())
            {
                items[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
            }

            --items[0].stackSize;

            if (items[0].stackSize <= 0)
            {
                items[0] = null;
            }
        }
    }
}

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The crash log keeps saying something about the tick rate. Can someone please help me fix?   ---- Minecraft Crash Report ---- // Everything's going to plan. No, really, that was supposed to happen. Time: 2024-06-21 16:24:55 Description: Exception in server tick loop java.lang.NullPointerException: Cannot invoke "com.cobblemon.mod.common.api.conditional.RegistryLikeCondition.fits(Object, net.minecraft.core.Registry)" because "condition" is null at com.cobblemon.mod.common.api.spawning.condition.SpawningCondition.fits(SpawningCondition.java:85) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.AreaTypeSpawningCondition.fits(AreaTypeSpawningCondition.java:30) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.GroundedTypeSpawningCondition.fits(GroundedTypeSpawningCondition.java:28) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.GroundedTypeSpawningCondition.fits(GroundedTypeSpawningCondition.java:24) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.condition.SpawningCondition.isSatisfiedBy(SpawningCondition.java:68) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.detail.SpawnDetail.isSatisfiedBy(SpawnDetail.java:78) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.Spawner$DefaultImpls.getMatchingSpawns(Spawner.java:42) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.TickingSpawner.getMatchingSpawns(TickingSpawner.java:31) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.selection.FlatContextWeightedSelector.getSelectionData(FlatContextWeightedSelector.java:79) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.selection.FlatContextWeightedSelector.select(FlatContextWeightedSelector.java:106) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.AreaSpawner.run(AreaSpawner.java:94) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.spawner.TickingSpawner.tick(TickingSpawner.java:71) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.spawning.SpawnerManager.onServerTick(SpawnerManager.java:58) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.events.ServerTickHandler.onTick(ServerTickHandler.java:20) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.Cobblemon$initialize$7.invoke(Cobblemon.java:368) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.Cobblemon$initialize$7.invoke(Cobblemon.java:368) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.reactive.ObservableSubscription.handle(ObservableSubscription.java:16) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.common.api.reactive.SimpleObservable.emit(SimpleObservable.java:39) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.forge.event.ForgePlatformEventHandler.onTick(ForgePlatformEventHandler.kt:172) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading} at com.cobblemon.mod.forge.event.__ForgePlatformEventHandler_onTick_ServerTickEvent.invoke(.dynamic) ~[Cobblemon-forge-1.5.2+1.20.1.jar%23121!/:?] {re:classloading,pl:eventbus:B} at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:73) ~[eventbus-6.0.5.jar%2352!/:?] {} at net.minecraftforge.eventbus.EventBus.post(EventBus.java:315) ~[eventbus-6.0.5.jar%2352!/:?] {} at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.5.jar%2352!/:?] {} at net.minecraftforge.event.ForgeEventFactory.onPostServerTick(ForgeEventFactory.java:950) ~[forge-1.20.1-47.2.17-universal.jar%23138!/:?] {re:classloading} at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:835) ~[server-1.20.1-20230612.114412-srg.jar%23133!/:?] {re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B} at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:661) ~[server-1.20.1-20230612.114412-srg.jar%23133!/:?] {re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B} at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[server-1.20.1-20230612.114412-srg.jar%23133!/:?] {re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B} at java.lang.Thread.run(Thread.java:840) ~[?:?] {re:mixin}
    • okay i've managed to fix it. looks like i made the stupidest error known to man. i hadn't installed forge to the client, which i assumed only affected the launcher which wouldn't affect me because i used a third party one. evidently not. the more you know
    • tried a fresh install of above and beyond with java 8 installed. got a popup that says "Error: Unable to access jarfile forge-1.16.5-36.2.8.jar". every good programmer knows getting a different error means you've made progress!
    • https://paste.ee/p/Ntb3m
    • Updated Graphics drivers, Tried reinstalling forge, but all to no avail. I am using the official curseforge launcher for it, but the minecraft launcher with forge gives me the same log anyway. Crash-Log: https://paste.ee/p/8Eyzz
  • Topics

×
×
  • Create New...

Important Information

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