Jump to content

Recommended Posts

Posted

 hey guys im trying to get items from a minecart with an inventory, by default a hopper cart or chest cart but could also be any carts added by mods that extends EntityMinecart and have item capability.

 

however after retrieving the inventory the inventory is always returned empty, IItemHandler.getStackInSlot(slot) is always air even though the entire cart is filled with items,

this is the update function that is trying to access the carts inventory, my testing setup has been attached as an image.

@Override
	public void update() {
		if (getWorld().isRemote) {
			EntityMinecart cart = this.getCartInfront(EntityMinecart.class);
			if (cart != null) {
			IItemHandler cartInv = cart.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, getPullFacing());
			if (cartInv != null) {
				System.out.println( cartInv.getSlots());
				for (int i = 0; i < cartInv.getSlots(); i++) {
					System.out.println(cartInv.getStackInSlot(i));
				}
				if (! this.invIsEmpty(cartInv)) {
					System.out.println("moving items");
					moveItems(cartInv);
				} else {
					//cart is empty
					if (emitWhenCartEmpty) {
						this.isEmittingRedstone = true;
					}
				}
			} else {
				//cart has no iventory
				if (emitWhenCartEmpty) {
					this.isEmittingRedstone = true;
				}
			}
		}
		}
	}

 

this is the console output

 

5
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
5
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:10] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
5
[19:22:11] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:11] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:11] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:11] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0
[19:22:11] [main/INFO] [STDOUT]: [ghostwolf.steampunkrevolution.tileentities.TileEntityUnloader:update:33]: 1xtile.air@0

 

if anyone has any idea what might be causing this or where im messing up my code it would be a big help.

 

- thanks ghostwolf

 

2017-12-15_19.21.57.png

2017-12-15_19.21.59.png

Posted (edited)
40 minutes ago, ghostwolf_ said:

getWorld().isRemote

 

40 minutes ago, ghostwolf_ said:

IItemHandler cartInv = cart.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, getPullFacing());

Capability data is only saved on the server (meaning client doesn't know anything)

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Posted
55 minutes ago, Matryoshika said:

 

Capability data is only saved on the server (meaning client doesn't know anything)

 doesnt the if statement at the start of the function ensure that it is being run on the server to ensure that that isnt an issue?

if (getWorld().isRemote) {

 

it does return a capability and its slots, in my case a hopper with 5 slots fine, its just that the stored items dont transfer properly, or am i just messing up something obvious?

Posted

I think that isRemote means you're on the client. Perhaps you want !isRemote?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted
Just now, Matryoshika said:

World::isRemote returns true for Client

 

Capabilities still exist on the client, it's just that no data is saved = null values (tile.air is an empty stack aka null)

sigh.. im a retard. i missed the ! that i use on every other update function. no wonder shit didnt work.

Posted

well thanks its working fine now, but i ran into another issue. i cant get my block model to rotate on the x-axis,

 

{
  "forge_marker": 1,
  "defaults": {
    "model": "steampunkrevolution:unloader",
    "textures": {
		"front": "minecraft:blocks/coal_block"
	}
  },
  "variants": {
    "normal": [{}],
    "inventory": [{}],
    "facing": {
      "north": {"y": 270},
      "south": {"y": 90},
      "west": {"y": 180},
      "east": {},
      "up": { "x": -90},
      "down": {"x": 90}
    },
    "enabled": {
	"true": {},
	"false": {}
	}
  }
}

 

this is the blockstate file, the facing is set correct (checked via the F3 menu and it moves items from the cart above it when set to up).

 

but the model doesnt rotate towards up

 

{ "source": "[120, -38, 99, 98, 17, -56, -51, -52, 75, 77, 46, 74, 76, 43, 81, 53, 118, 76, -52, 44, -78, 103, -112, 71, 22, 41, 46, -55, -49, 75, -75, 47, 75, 44, -54, 76, -52, 43, -79, 5, -13, 24, -60, -111, 21, 100, 22, -27, -25, -59, 39, -27, -28, 39, 103, -37, -93, 74, 36, -25, 39, -26, -64, 36, 24, 26, 56, 66, -127, 32, 107, -43, -86, -107, 40, 120, 106, -40, -54, -84, 9, 108, -88, 24, 36, -122, -90, 14, -90, 119, 106, 104, 104, 24, 6, -114, -1, -118, -118, -79, -88, -127, -103, 3, 98, -101, -110, -120, 7, -115, 126, 96, -72, 96, -47, 31, -118, 3, -93, -22, -121, -122, 51, 40, 124, -80, -24, -113, 65, -61, -88, -6, -127, 122, 96, 113, 51, -92, -11, 83, 26, 126, 67, 61, -3, 80, -104, 127, 40, -51, -65, 0, 46, -94, -85, -38]",
"textures": {"1398723388": "minecraft:blocks/hopper_outside",
"1695556649": "#front",
"761770141": "minecraft:blocks/nether_brick",
"particle": "minecraft:blocks/nether_brick"},
"elements": [
{ "from": [1,0,1], "to": [15,0,5], "faces": { "down":  { "uv": [1,11,15,15], "texture": "#1398723388", "cullface": "down" } } },
{ "from": [1,0,5], "to": [5,0,11], "faces": { "down":  { "uv": [1,5,5,11], "texture": "#1398723388", "cullface": "down" } } },
{ "from": [1,0,11], "to": [15,0,15], "faces": { "down":  { "uv": [1,1,15,5], "texture": "#1398723388", "cullface": "down" } } },
{ "from": [11,0,5], "to": [15,0,11], "faces": { "down":  { "uv": [11,5,15,11], "texture": "#1398723388", "cullface": "down" } } },
{ "from": [1,16,1], "to": [15,16,5], "faces": { "up":  { "uv": [1,1,15,5], "texture": "#1398723388", "cullface": "up" } } },
{ "from": [1,16,5], "to": [5,16,11], "faces": { "up":  { "uv": [1,5,5,11], "texture": "#1398723388", "cullface": "up" } } },
{ "from": [1,16,11], "to": [15,16,15], "faces": { "up":  { "uv": [1,11,15,15], "texture": "#1398723388", "cullface": "up" } } },
{ "from": [11,16,5], "to": [15,16,11], "faces": { "up":  { "uv": [11,5,15,11], "texture": "#1398723388", "cullface": "up" } } },
{ "from": [1,1,0], "to": [15,5,0], "faces": { "north":  { "uv": [1,11,15,15], "texture": "#1398723388", "cullface": "north" } } },
{ "from": [1,5,0], "to": [5,11,0], "faces": { "north":  { "uv": [11,5,15,11], "texture": "#1398723388", "cullface": "north" } } },
{ "from": [1,11,0], "to": [15,15,0], "faces": { "north":  { "uv": [1,1,15,5], "texture": "#1398723388", "cullface": "north" } } },
{ "from": [11,5,0], "to": [15,11,0], "faces": { "north":  { "uv": [1,5,5,11], "texture": "#1398723388", "cullface": "north" } } },
{ "from": [1,1,16], "to": [15,5,16], "faces": { "south":  { "uv": [1,11,15,15], "texture": "#1398723388", "cullface": "south" } } },
{ "from": [1,5,16], "to": [5,11,16], "faces": { "south":  { "uv": [1,5,5,11], "texture": "#1398723388", "cullface": "south" } } },
{ "from": [1,11,16], "to": [15,15,16], "faces": { "south":  { "uv": [1,1,15,5], "texture": "#1398723388", "cullface": "south" } } },
{ "from": [11,5,16], "to": [15,11,16], "faces": { "south":  { "uv": [11,5,15,11], "texture": "#1398723388", "cullface": "south" } } },
{ "from": [0,1,1], "to": [0,15,5], "faces": { "west":  { "uv": [1,1,5,15], "texture": "#1398723388", "cullface": "west" } } },
{ "from": [0,1,5], "to": [0,5,11], "faces": { "west":  { "uv": [5,11,11,15], "texture": "#1398723388", "cullface": "west" } } },
{ "from": [0,1,11], "to": [0,15,15], "faces": { "west":  { "uv": [11,1,15,15], "texture": "#1398723388", "cullface": "west" } } },
{ "from": [0,11,5], "to": [0,15,11], "faces": { "west":  { "uv": [5,1,11,5], "texture": "#1398723388", "cullface": "west" } } },
{ "from": [16,1,1], "to": [16,15,2], "faces": { "east":  { "uv": [14,1,15,15], "texture": "#1398723388", "cullface": "east" } } },
{ "from": [16,1,2], "to": [16,2,14], "faces": { "east":  { "uv": [2,14,14,15], "texture": "#1398723388", "cullface": "east" } } },
{ "from": [16,14,2], "to": [16,15,14], "faces": { "east":  { "uv": [2,1,14,2], "texture": "#1398723388", "cullface": "east" } } },
{ "from": [16,1,14], "to": [16,15,15], "faces": { "east":  { "uv": [1,1,2,15], "texture": "#1398723388", "cullface": "east" } } },
{ "from": [15,2,2], "to": [16,2,14], "faces": { "up":  { "uv": [15,2,16,14], "texture": "#1398723388" } } },
{ "from": [15,14,2], "to": [16,14,14], "faces": { "down":  { "uv": [15,2,16,14], "texture": "#1398723388" } } },
{ "from": [15,2,2], "to": [16,14,2], "faces": { "south":  { "uv": [15,2,16,14], "texture": "#1398723388" } } },
{ "from": [15,2,14], "to": [16,14,14], "faces": { "north":  { "uv": [0,2,1,14], "texture": "#1398723388" } } },
{ "from": [15,2,2], "to": [15,14,14], "faces": { "east":  { "uv": [2,2,14,14], "texture": "#1695556649" } } },
{ "from": [1,6,6], "to": [1,10,10], "faces": { "west":  { "uv": [6,6,10,10], "texture": "#1695556649" } } },
{ "from": [6,15,6], "to": [10,15,10], "faces": { "up":  { "uv": [6,6,10,10], "texture": "#1695556649" } } },
{ "from": [6,1,6], "to": [10,1,10], "faces": { "down":  { "uv": [6,6,10,10], "texture": "#1695556649" } } },
{ "from": [6,6,15], "to": [10,10,15], "faces": { "south":  { "uv": [6,6,10,10], "texture": "#1695556649" } } },
{ "from": [6,6,1], "to": [10,10,1], "faces": { "north":  { "uv": [6,6,10,10], "texture": "#1695556649" } } },
{ "from": [0,0,0], "to": [16,0,1], "faces": { "down":  { "uv": [0,15,16,16], "texture": "#761770141", "cullface": "down" } } },
{ "from": [0,0,1], "to": [1,0,15], "faces": { "down":  { "uv": [0,1,1,15], "texture": "#761770141", "cullface": "down" } } },
{ "from": [15,0,1], "to": [16,0,15], "faces": { "down":  { "uv": [15,1,16,15], "texture": "#761770141", "cullface": "down" } } },
{ "from": [0,0,15], "to": [16,0,16], "faces": { "down":  { "uv": [0,0,16,1], "texture": "#761770141", "cullface": "down" } } },
{ "from": [5,0,6], "to": [6,0,10], "faces": { "down":  { "uv": [5,6,6,10], "texture": "#761770141", "cullface": "down" } } },
{ "from": [10,0,6], "to": [11,0,10], "faces": { "down":  { "uv": [10,6,11,10], "texture": "#761770141", "cullface": "down" } } },
{ "from": [5,0,5], "to": [11,0,6], "faces": { "down":  { "uv": [5,10,11,11], "texture": "#761770141", "cullface": "down" } } },
{ "from": [5,0,10], "to": [11,0,11], "faces": { "down":  { "uv": [5,5,11,6], "texture": "#761770141", "cullface": "down" } } },
{ "from": [0,16,0], "to": [16,16,1], "faces": { "up":  { "uv": [0,0,16,1], "texture": "#761770141", "cullface": "up" } } },
{ "from": [0,16,1], "to": [1,16,15], "faces": { "up":  { "uv": [0,1,1,15], "texture": "#761770141", "cullface": "up" } } },
{ "from": [15,16,1], "to": [16,16,15], "faces": { "up":  { "uv": [15,1,16,15], "texture": "#761770141", "cullface": "up" } } },
{ "from": [0,16,15], "to": [16,16,16], "faces": { "up":  { "uv": [0,15,16,16], "texture": "#761770141", "cullface": "up" } } },
{ "from": [5,16,6], "to": [6,16,10], "faces": { "up":  { "uv": [5,6,6,10], "texture": "#761770141", "cullface": "up" } } },
{ "from": [10,16,6], "to": [11,16,10], "faces": { "up":  { "uv": [10,6,11,10], "texture": "#761770141", "cullface": "up" } } },
{ "from": [5,16,5], "to": [11,16,6], "faces": { "up":  { "uv": [5,5,11,6], "texture": "#761770141", "cullface": "up" } } },
{ "from": [5,16,10], "to": [11,16,11], "faces": { "up":  { "uv": [5,10,11,11], "texture": "#761770141", "cullface": "up" } } },
{ "from": [0,0,0], "to": [16,1,0], "faces": { "north":  { "uv": [0,15,16,16], "texture": "#761770141", "cullface": "north" } } },
{ "from": [0,1,0], "to": [1,15,0], "faces": { "north":  { "uv": [15,1,16,15], "texture": "#761770141", "cullface": "north" } } },
{ "from": [15,1,0], "to": [16,15,0], "faces": { "north":  { "uv": [0,1,1,15], "texture": "#761770141", "cullface": "north" } } },
{ "from": [0,15,0], "to": [16,16,0], "faces": { "north":  { "uv": [0,0,16,1], "texture": "#761770141", "cullface": "north" } } },
{ "from": [5,6,0], "to": [6,10,0], "faces": { "north":  { "uv": [10,6,11,10], "texture": "#761770141", "cullface": "north" } } },
{ "from": [10,6,0], "to": [11,10,0], "faces": { "north":  { "uv": [5,6,6,10], "texture": "#761770141", "cullface": "north" } } },
{ "from": [5,5,0], "to": [11,6,0], "faces": { "north":  { "uv": [5,10,11,11], "texture": "#761770141", "cullface": "north" } } },
{ "from": [5,10,0], "to": [11,11,0], "faces": { "north":  { "uv": [5,5,11,6], "texture": "#761770141", "cullface": "north" } } },
{ "from": [0,0,16], "to": [16,1,16], "faces": { "south":  { "uv": [0,15,16,16], "texture": "#761770141", "cullface": "south" } } },
{ "from": [0,1,16], "to": [1,15,16], "faces": { "south":  { "uv": [0,1,1,15], "texture": "#761770141", "cullface": "south" } } },
{ "from": [15,1,16], "to": [16,15,16], "faces": { "south":  { "uv": [15,1,16,15], "texture": "#761770141", "cullface": "south" } } },
{ "from": [0,15,16], "to": [16,16,16], "faces": { "south":  { "uv": [0,0,16,1], "texture": "#761770141", "cullface": "south" } } },
{ "from": [5,6,16], "to": [6,10,16], "faces": { "south":  { "uv": [5,6,6,10], "texture": "#761770141", "cullface": "south" } } },
{ "from": [10,6,16], "to": [11,10,16], "faces": { "south":  { "uv": [10,6,11,10], "texture": "#761770141", "cullface": "south" } } },
{ "from": [5,5,16], "to": [11,6,16], "faces": { "south":  { "uv": [5,10,11,11], "texture": "#761770141", "cullface": "south" } } },
{ "from": [5,10,16], "to": [11,11,16], "faces": { "south":  { "uv": [5,5,11,6], "texture": "#761770141", "cullface": "south" } } },
{ "from": [0,0,0], "to": [0,16,1], "faces": { "west":  { "uv": [0,0,1,16], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,0,1], "to": [0,1,15], "faces": { "west":  { "uv": [1,15,15,16], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,15,1], "to": [0,16,15], "faces": { "west":  { "uv": [1,0,15,1], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,0,15], "to": [0,16,16], "faces": { "west":  { "uv": [15,0,16,16], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,5,6], "to": [0,6,10], "faces": { "west":  { "uv": [6,10,10,11], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,10,6], "to": [0,11,10], "faces": { "west":  { "uv": [6,5,10,6], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,5,5], "to": [0,11,6], "faces": { "west":  { "uv": [5,5,6,11], "texture": "#761770141", "cullface": "west" } } },
{ "from": [0,5,10], "to": [0,11,11], "faces": { "west":  { "uv": [10,5,11,11], "texture": "#761770141", "cullface": "west" } } },
{ "from": [16,0,0], "to": [16,16,1], "faces": { "east":  { "uv": [15,0,16,16], "texture": "#761770141", "cullface": "east" } } },
{ "from": [16,0,1], "to": [16,1,15], "faces": { "east":  { "uv": [1,15,15,16], "texture": "#761770141", "cullface": "east" } } },
{ "from": [16,15,1], "to": [16,16,15], "faces": { "east":  { "uv": [1,0,15,1], "texture": "#761770141", "cullface": "east" } } },
{ "from": [16,0,15], "to": [16,16,16], "faces": { "east":  { "uv": [0,0,1,16], "texture": "#761770141", "cullface": "east" } } },
{ "from": [6,6,0], "to": [6,10,1], "faces": { "east":  { "uv": [15,6,16,10], "texture": "#761770141" } } },
{ "from": [6,0,6], "to": [6,1,10], "faces": { "east":  { "uv": [6,15,10,16], "texture": "#761770141" } } },
{ "from": [6,15,6], "to": [6,16,10], "faces": { "east":  { "uv": [6,0,10,1], "texture": "#761770141" } } },
{ "from": [6,6,15], "to": [6,10,16], "faces": { "east":  { "uv": [0,6,1,10], "texture": "#761770141" } } },
{ "from": [10,6,0], "to": [10,10,1], "faces": { "west":  { "uv": [0,6,1,10], "texture": "#761770141" } } },
{ "from": [10,0,6], "to": [10,1,10], "faces": { "west":  { "uv": [6,15,10,16], "texture": "#761770141" } } },
{ "from": [10,15,6], "to": [10,16,10], "faces": { "west":  { "uv": [6,0,10,1], "texture": "#761770141" } } },
{ "from": [10,6,15], "to": [10,10,16], "faces": { "west":  { "uv": [15,6,16,10], "texture": "#761770141" } } },
{ "from": [6,6,0], "to": [10,6,1], "faces": { "up":  { "uv": [6,0,10,1], "texture": "#761770141" } } },
{ "from": [0,6,6], "to": [1,6,10], "faces": { "up":  { "uv": [0,6,1,10], "texture": "#761770141" } } },
{ "from": [6,6,15], "to": [10,6,16], "faces": { "up":  { "uv": [6,15,10,16], "texture": "#761770141" } } },
{ "from": [6,10,0], "to": [10,10,1], "faces": { "down":  { "uv": [6,15,10,16], "texture": "#761770141" } } },
{ "from": [0,10,6], "to": [1,10,10], "faces": { "down":  { "uv": [0,6,1,10], "texture": "#761770141" } } },
{ "from": [6,10,15], "to": [10,10,16], "faces": { "down":  { "uv": [6,0,10,1], "texture": "#761770141" } } },
{ "from": [6,0,6], "to": [10,1,6], "faces": { "south":  { "uv": [6,15,10,16], "texture": "#761770141" } } },
{ "from": [0,6,6], "to": [1,10,6], "faces": { "south":  { "uv": [0,6,1,10], "texture": "#761770141" } } },
{ "from": [6,15,6], "to": [10,16,6], "faces": { "south":  { "uv": [6,0,10,1], "texture": "#761770141" } } },
{ "from": [6,0,10], "to": [10,1,10], "faces": { "north":  { "uv": [6,15,10,16], "texture": "#761770141" } } },
{ "from": [0,6,10], "to": [1,10,10], "faces": { "north":  { "uv": [15,6,16,10], "texture": "#761770141" } } },
{ "from": [6,15,10], "to": [10,16,10], "faces": { "north":  { "uv": [6,0,10,1], "texture": "#761770141" } } }
],
"display": { "thirdperson": { "rotation": [ 10, -45, 170 ], "translation": [ 0, 1.5, -2.75 ], "scale": [ 0.375, 0.375, 0.375 ] } }, "parent": "block/cube_all" }

 

this is the model im using.

 

im fairly new to modding so im not too familiar with the model and block states files, but setting the facing to n,s,w,e works fine and rotates like its supposed to, but rotating up and down doesnt work.

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

    • It is 1.12.2 - I have no idea if there is a 1.12 pack
    • Okay, but does the modpack works with 1.12 or just with 1.12.2, because I need the Forge client specifically for Minecraft 1.12, not 1.12.2
    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
  • Topics

×
×
  • Create New...

Important Information

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