Jump to content

Recommended Posts

Posted

I am having trouble getting my block to behave. The basic gist is you select some elements and hit a button. This then checks the elements you select against some combinations and puts an itemstack in a slot. This all works, however when I try to take the item out of the slot it simply disappears.

I've already searched around in other people's posts with similar issues but their cases are too different for me to have found a working solution.

 

I've already established its something to do with packet handling (which I have no experience in). I currently have it set up like so:

1) Button click is detected in GUI class.

2) Calls a method in PacketHandler class with button ID as the parameter

3) This method tries to write an id and the button ID to a dataStream, and then send this packet to the PacketHandler class (as suggested by                   

    VSWE in his tutorials) (If it fails it prints that it has failed)

4) The PacketHandler onPacketData method receives this packet and does a switch for the id (currently only 1 case but open for future expansion)

5) Then the button ID is sent to the container class where a switch is done for the button ID

6) This then calls the relevant class which runs the combination check method, and then sets the contents of the relevant container slot.

 

These are the classes that are involved.

(Note this was a summer project to learn java which turned into a school coursework project as I couldn't think of anything else to do, therefore the code is probably really messy and I also had to simplify some things (mainly combination check) so try to just ignore those bits)

 

I've only included the bits of code that are relevant to the situation...otherwise it would be cluttered.

 

GuiSciBench

// Listens for button click
protected void actionPerformed(GuiButton guibutton) {
	// Checks GUI ID
	switch (guibutton.id) {
	case 31:
		PacketHandler.sendButtonPacket((byte)guibutton.id);
		break;
	case 32:			
		PacketHandler.sendButtonPacket((byte)guibutton.id);
		break;
	default:
		addToElementList(guibutton.id);
	}
}

 

 

PacketHandler

package com.atomictim.atomiccraft.network;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import com.atomictim.atomiccraft.Info;
import com.atomictim.atomiccraft.client.container.ContainerSciBench;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

public class PacketHandler implements IPacketHandler {

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
	ByteArrayDataInput reader = ByteStreams.newDataInput(packet.data);		
	EntityPlayer entityPlayer = (EntityPlayer)player;
	byte packetID = reader.readByte();
	switch (packetID) {
	case 0:
		byte buttonID = reader.readByte();
		Container container = entityPlayer.openContainer;
		if(container != null && container instanceof ContainerSciBench) {
			ContainerSciBench.receiveButtonEvent(buttonID);
		}
		break;
	}
}

public static void sendButtonPacket(byte id) {
	ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
	DataOutputStream dataStream = new DataOutputStream(byteStream);

	try {
		dataStream.writeByte((byte)0);
		dataStream.writeByte(id);
		PacketDispatcher.sendPacketToServer(PacketDispatcher.getPacket(Info.CHANNEL, byteStream.toByteArray()));
	} catch(IOException io) {
		System.out.println("Failed to send packet");
	}

}
}

 

 

ContainerSciBench

        public static void receiveButtonEvent(byte buttonID) {
	switch (buttonID) {
	//breakdown
	case 31:
		breakdown();
		break;
		//combine	
	case 32:
		combine();
		break;
	}
}

public static void breakdown() {
	IInventory inventory = (IInventory) ContainerSciBench.bench;
	ItemStack itemstack = inventory.getStackInSlot(0);
	if (itemstack != null) {
		if (itemstack.stackSize > 1) {
			itemstack.stackSize -= 1;
		} else {
			inventory.setInventorySlotContents(0, null);
			itemstack = null;
		}
	}
}

public static void combine() {
	IInventory inventory = (IInventory) ContainerSciBench.bench;
	ItemStack itemstack = createNewItem(GuiSciBench.elements);
	inventory.setInventorySlotContents(1, itemstack);
	GuiSciBench.elements.clear();
	System.out.println("Array list cleared");
}
        
        public static ItemStack createNewItem(ArrayList<Integer> elements) {
                Assume the code I have removed from this bit works because it does (Its too long otherwise) and that an itemstack is returned.
}

 

 

The tutorial of VSWE's I was following originally had some code in the TileEntitySciBench class, but I was told that it should be put in the container class. here is the TileEntity class anyway:

 

TileEntitySciBench

package com.atomictim.atomiccraft.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;

public class TileEntitySciBench extends TileEntity implements IInventory {

// Create new array
private ItemStack[] inventory;

// Set the size of the array to 2 (allowing 2 new Itemstacks within the
// inventory, which will be represented as the slots)
public TileEntitySciBench() {
	inventory = new ItemStack[2];
}

// Gets the size of inventory array and sets it as the size of the inventory
@Override
public int getSizeInventory() {
	return inventory.length;
}

// Gets the itemstack currently in the slot of inventory.
@Override
public ItemStack getStackInSlot(int i) {
	return inventory[i];
}

// Handles decreasing the size of the stack in the slot. For example when
// the player right clicks a stack, it halves it into 2 stacks.
@Override
public ItemStack decrStackSize(int slot, int count) {
	ItemStack itemstack = getStackInSlot(slot);
	if (itemstack != null) {
		if (itemstack.stackSize <= count) {
			setInventorySlotContents(slot, null);
		} else {
			itemstack = itemstack.splitStack(count);
			onInventoryChanged();
		}
	}
	return itemstack;
}

// Creates new itemstacks for each slot of the inventory then sets the
// existing stacks to null.
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	ItemStack itemstack = getStackInSlot(slot);
	setInventorySlotContents(slot, null);
	return itemstack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
	inventory[slot] = itemstack;
	if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
		itemstack.stackSize = getInventoryStackLimit();
	}
	onInventoryChanged();
}

// Sets a localized name for the inventory
@Override
public String getInvName() {
	return "Scientific Workbench";
}

// Ensures the inventory name set above is localized
@Override
public boolean isInvNameLocalized() {
	return true;
}

// Sets the stack size limit to 64, which is the maximum size of a stack
@Override
public int getInventoryStackLimit() {
	return 64;
}

// Checks within a radius using pythagoras to check if the player can use
// the inventory.
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64;
}

@Override
public void openChest() {

}

@Override
public void closeChest() {

}

// Allows every item to be used in every slot.
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return true;
}

// Saves the contents of the tile entity to an NBT file linked with the tile
// entity.
@Override
public void writeToNBT(NBTTagCompound compound) {
	super.writeToNBT(compound);

	NBTTagList list = new NBTTagList();

	for (int i = 0; i < getSizeInventory(); i++) {
		ItemStack itemstack = getStackInSlot(i);
		if (itemstack != null) {
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("SlotSciBench", (byte) i);
			itemstack.writeToNBT(item);
			list.appendTag(item);
		}
	}
	compound.setTag("ItemsSciBench", list);
}

// Sets the contents of a tile entity from an NBT file linked with the tile
// entity.
@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);

	NBTTagList list = compound.getTagList("ItemsSciBench");

	for (int i = 0; i < list.tagCount(); i++) {
		NBTTagCompound item = (NBTTagCompound) list.tagAt(i);
		int slot = item.getByte("SlotSciBench");

		if (slot >= 0 && slot < getSizeInventory()) {
			setInventorySlotContents(slot,
					ItemStack.loadItemStackFromNBT(item));
		}
	}
}

@Override
public Packet getDescriptionPacket() {
        NBTTagCompound nbtTag = new NBTTagCompound();
        this.writeToNBT(nbtTag);
        return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
}

@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) {
        readFromNBT(packet.data);
}
}

 

 

Any help would be greatly appreciated as this has been bugging me for 5 months now (haven't been trying to solve it all the time xD but I feel as though I should solve it)

Thanks! (Say if anything else is needed)

Posted

Ok that makes so much more sense now.

So, if I understand correctly, I currently have calls to static methods which will not work with Minecraft as it will create a new instance, right? So what I need to do is get the existing instance and run the method there correct? Now I have one small problem here...as much as I'd like to say oh yeah I can do that, I can't. If you could give me any pointers that would really help me out.

Thanks.

Posted

Sorry for being so retarded (got a whacking headache so not on top form mentally right now) but I think I've done it right yet the problem of items disappearing persists.

I now have this in my PacketHandler class:

case 0:
byte buttonID = reader.readByte();
Container container = entityPlayer.openContainer;
if(container != null && container instanceof ContainerSciBench) {
	((ContainerSciBench) container).receiveButtonEvent(buttonID);
}
break;
}

 

This is the only thing I can seem to get to work without an error, but inevitably I'm still doing something wrong.

If you could give me a little push in the right direction or just kick me right down the track that would be epic...really starting to get tired of this problem xD

Posted

dude..you are an absolute god..thank you so much for your help...I had a feeling the problem was so simple...too simple for a beginner to notice (at times)...and its been bugging me for ages...thank you :)

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi,  I'm using Forge 47.3.0 for Minecraft 1.20.1 I apologise if this is obvious I am very new to modding for Minecraft. I sucessfully made a mod that launched without errors or crashes (without it doing anything) but in order to add the features I need, I need to add "Custom Portal API [Forge]" as a dependency. However no matter the way I've tried to acheive this, it crashes. I am pretty sure it's not the way I'm putting it in the repositories, the dependencies or the way I'm refrencing it, as I've a hundred diffrent combinations and multiple Maven methods. And on all those diffrent variations I still get this crash: pastebin.com/UhumzZCZ Any tips would be invaluable as I've been loosing my mind over this!
    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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