Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5] Help with custom container (error when put a item in an slot)


Recommended Posts

3 minutes ago, diesieben07 said:

That is not how you handle exceptions, ever.

setAccessible is also not necessary when using ObfuscationReflectionHelper.

i fixed that

this is now the code I would like to take to move the slot:

full code:

https://github.com/Luis-st/Forge-1.16.5-36.0.1-mdk/blob/main/forge-1.16.5-36.0.1-mdk/src/main/java/net/luis/cave/common/inventory/container/ModBarrelContainer.java

	public void moveSlots(ModBarrelContainer.Direction direction) {
		
		List<Slot> slots = this.inventorySlots;
		
		if (direction == ModBarrelContainer.Direction.ABOVE) {
			
			for (int i = 0; i < slots.size(); i++) {
				
				Slot slot = slots.get(i);
				
				if (this.isTopSlot(slot)) {
					
					this.setSlotPosY(slot, slot.yPos + 1000);
					
				} else {
					
					this.setSlotPosY(slot, slot.yPos + 18);
					
				}
				
			}
			
		} else if (direction == ModBarrelContainer.Direction.BELOW) {
			
			for (int i = 0; i < slots.size(); i++) {
				
				Slot slot = slots.get(i);
				
				if (this.isBottomSlot(slot)) {
					
					this.setSlotPosY(slot, slot.yPos - 1000);
					
				} else {
					
					this.setSlotPosY(slot, slot.yPos - 18);
					
				}
				
			}
			
		} else {
			
			return;
			
		}
		
	}
	
	private boolean isTopSlot(Slot slot) {
		
		if (slot.yPos == 18 || slot.yPos == -874) {
			
			return true;
			
		}
		
		return false;
		
	}
	
	private boolean isBottomSlot(Slot slot) {
		
		
		if (slot.yPos == 126 || slot.xPos == 1018) {
			
			return true;
			
		}
		
		return false;
		
	}
	
	private void setSlotPosY(Slot slot, int newPosY) {
		
		try {
			
			yPos.setInt(slot, newPosY);
			
		} catch (IllegalArgumentException | IllegalAccessException e) {
			
			e.printStackTrace();
			
		}
		
	}
	
	public enum Direction {
		
		ABOVE,
		BELOW;
		
	}

 

what is still missing is to send the packet to the server

how do i create my own package?

Link to post
Share on other sites
51 minutes ago, Luis_ST said:

what is still missing is to send the packet to the server

how do i create my own package?

https://mcforge.readthedocs.io/en/latest/networking/simpleimpl/

 

52 minutes ago, Luis_ST said:

if (direction == ModBarrelContainer.Direction.ABOVE) {
			
			for (int i = 0; i < slots.size(); i++) {
				
				Slot slot = slots.get(i);
				
				if (this.isTopSlot(slot)) {
					
					this.setSlotPosY(slot, slot.yPos + 1000);
					
				} else {
					
					this.setSlotPosY(slot, slot.yPos + 18);
					
				}
				
			}
			
		} else if (direction == ModBarrelContainer.Direction.BELOW) {
			
			for (int i = 0; i < slots.size(); i++) {
				
				Slot slot = slots.get(i);
				
				if (this.isBottomSlot(slot)) {
					
					this.setSlotPosY(slot, slot.yPos - 1000);
					
				} else {
					
					this.setSlotPosY(slot, slot.yPos - 18);
					
				}
				
			}
			
		} else {
			
			return;
			
		}
		
	}

 

  1. Use an "enhanced for loop" (Java speak for a foreach loop) instead of using indices.
  2. That positioning seems super strange. You should keep track of the position of the scroll bar and then position the slots accordingly, instead of moving the slots by a given amount each time.
  3. Copy-pasting that code for both directions is terrible.

Additionally: your implementation of isBottomSlot and isTopSlot is bad. Why is that relying on the position?

 

And no, you have not fixed the exception handling at all.

Link to post
Share on other sites
12 hours ago, diesieben07 said:

That positioning seems super strange. You should keep track of the position of the scroll bar and then position the slots accordingly, instead of moving the slots by a given amount each time.

So you mean that I take the value from the scrollbar and add this to the position of the slots

I've already think about it, but how do I move the slots that I have to move over the container (+1000) or the slots that I have to move under the container (-1000).

is there a way to check this without unsing the position?

 

12 hours ago, diesieben07 said:

And no, you have not fixed the exception handling at all

I should catch the errors one by one

Link to post
Share on other sites
1 hour ago, Luis_ST said:

So you mean that I take the value from the scrollbar and add this to the position of the slots

I've already think about it, but how do I move the slots that I have to move over the container (+1000) or the slots that I have to move under the container (-1000).

is there a way to check this without unsing the position?

What I mean is this:

  • Have a variable that contains the current scroll position (e.g. from 0-250).
  • Whenever this variable changes, calculate the current slot positions based on it. Do not work with deltas/differences, it just gets confusing.
1 hour ago, Luis_ST said:

I should catch the errors one by one

No, you should actually handle the exception instead of going "oh, exception? log it, then never mind and carry on as if nothing was wrong".

Link to post
Share on other sites
1 hour ago, diesieben07 said:

Have a variable that contains the current scroll position (e.g. from 0-250).

yes I have this variable (it goes from 0 to 109).

 

1 hour ago, diesieben07 said:

Whenever this variable changes, calculate the current slot positions based on it.

this is what i'm trying right now.

Edit: it works like this, but how do I find out when I have to move a slot outside of the visible area if I shouldn't use the slot's position

 

15 hours ago, diesieben07 said:

I've "tried" something like this before in another mod.

But I am not sure whether it works that way and whether it is even right?

code:

PacketHandler:

https://github.com/Luis-st/Forge-1.16.5-36.0.1-mdk/blob/main/forge-1.16.5-36.0.1-mdk/src/main/java/net/luis/cave/core/ModPacketHandler.java

my message interface:

https://github.com/Luis-st/Forge-1.16.5-36.0.1-mdk/blob/main/forge-1.16.5-36.0.1-mdk/src/main/java/net/luis/cave/core/IMessage.java

SyncSlotPosition message (not final/ in work):

https://github.com/Luis-st/Forge-1.16.5-36.0.1-mdk/blob/main/forge-1.16.5-36.0.1-mdk/src/main/java/net/luis/cave/core/SyncSlotPosition.java

 

is there an error somewhere / does it work like that way?

 

Edited by Luis_ST
Link to post
Share on other sites
1 minute ago, diesieben07 said:

The IMessage interface is entirely unnecessary. Other than that, yes, that is how you add packets

  1. it was once intended for something else
  2. what do I have to change that I can use it
Link to post
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.

Guest
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 Privacy Policy.