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.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I also have the same question, thank you for sharing it and I also found the answer.
    • So I had no clue what topic to put this under because its not really modding with forge but modding forge itself. So I have terrain generation mods that are only server side and Bungeecord that has Geyser packet translator so people on bedrock edition can play as well, but when a bedrock player connects it gets a unknown packet error. I tested the proxy on a Spigot server verifying it was forge. With no mods installed I still get the error. I got a wireshark log of it all and I found the packet where it aborts the connection due to the invalid packet but I have no clue how to stop it. When I still had the mods installed I disabled the forge handshake protocol but it did not fix the issue. If you know what part of forge is doing this or a way to fix it all together that would be awesome. I know modifying forge can end really baddy (definitely the handshake protocol) but I think that is my only fix here. Im open to any suggestions tho. I have run out of ideas to fix it so any help would be nice. Also it seems the packet id causing the connection error is 0xFE but im not sure cause the client also sends back a packet with that ID. I have a whole bunch of info collected so if you need any more information on the issue, I willing to give it, just not the ips on the wireshark log.
    • Okay, I got it working correctly now, but I'm not sure how to make it work with multiple blocks.   My Block Code:   package expanded.blocks; import javax.annotation.Nullable; import expanded.VanillaBuildingBlocks; import net.minecraft.block.BlockState; import net.minecraft.block.RotatedPillarBlock; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.ToolType; public class CustomLog extends RotatedPillarBlock {            public CustomLog(Properties properties) {         super(properties);     }      @Override     @Nullable     public BlockState getToolModifiedState(BlockState state, World world, BlockPos pos, PlayerEntity player, ItemStack stack, ToolType toolType) {         return toolType == ToolType.AXE ? VanillaBuildingBlocks.acacia_log.defaultBlockState() : null;     } }
    • ? Registering the block and overriding a function inside the class are two completely different things
    • Ya but I'm not sure how I would actually add/implement it into the class correctly, and then registering the block correctly in my register class.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.