Jump to content

Recommended Posts

Posted (edited)

hey i'm new to minecraft modding, but have been programming more or less good java for some time now.

I've read the forge doc and a few topics here in the forum about overwriting and changing vanilla elements.

which helped me to overwrite the beacon, but now I have an error that I cannot explain to myself.

the beacon works so far (block, item, container, tileentity- / render). only something is wrong with the screen when I put an activation item in the slot and press the Confirm button,

the screen is closed and the item drops, and I don't get any effects.

what i find strange about it i have taken over the minecraft code of the screen directly and have not changed anything.

this is the screen class:

public class ModBeaconScreen extends ContainerScreen<ModBeaconContainer> {

	private static final ResourceLocation BEACON_GUI_TEXTURES = new ResourceLocation("textures/gui/container/beacon.png");
	private static final ITextComponent field_243334_B = new TranslationTextComponent("block.minecraft.beacon.primary");
	private static final ITextComponent field_243335_C = new TranslationTextComponent("block.minecraft.beacon.secondary");
	private ModBeaconScreen.ConfirmButton beaconConfirmButton;
	private boolean buttonsNotDrawn;
	private Effect primaryEffect;
	private Effect secondaryEffect;

	public ModBeaconScreen(ModBeaconContainer container, PlayerInventory playerInventory, ITextComponent title) {
		
		super(container, playerInventory, title);
		this.xSize = 230;
		this.ySize = 219;
		container.addListener(new IContainerListener() {
			@Override
			public void sendAllContents(Container containerToSend, NonNullList<ItemStack> itemsList) {
				
			}

			@Override
			public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) {
				
			}

			@Override
			public void sendWindowProperty(Container containerIn, int varToUpdate, int newValue) {
				
				ModBeaconScreen.this.primaryEffect = container.func_216967_f();
				ModBeaconScreen.this.secondaryEffect = container.func_216968_g();
				ModBeaconScreen.this.buttonsNotDrawn = true;
				
			}
		});
		
	}
	
	@Override
	protected void init() {
		
		super.init();
		this.beaconConfirmButton = this.addButton(new ModBeaconScreen.ConfirmButton(this.guiLeft + 164, this.guiTop + 107));
		this.addButton(new ModBeaconScreen.CancelButton(this.guiLeft + 190, this.guiTop + 107));
		this.buttonsNotDrawn = true;
		this.beaconConfirmButton.active = false;
		
	}
	
	@Override
	public void tick() {
		
		super.tick();
		int i = this.container.func_216969_e();
		
		if (this.buttonsNotDrawn && i >= 0) {
			
			this.buttonsNotDrawn = false;

			for (int j = 0; j <= 2; ++j) {
				
				int k = BeaconTileEntity.EFFECTS_LIST[j].length;
				int l = k * 22 + (k - 1) * 2;

				for (int i1 = 0; i1 < k; ++i1) {
					
					Effect effect = BeaconTileEntity.EFFECTS_LIST[j][i1];
					ModBeaconScreen.PowerButton beaconscreen$powerbutton = new ModBeaconScreen.PowerButton(
							this.guiLeft + 76 + i1 * 24 - l / 2, this.guiTop + 22 + j * 25, effect, true);
					this.addButton(beaconscreen$powerbutton);
					
					if (j >= i) {
						
						beaconscreen$powerbutton.active = false;
						
					} else if (effect == this.primaryEffect) {
						
						beaconscreen$powerbutton.setSelected(true);
						
					}
					
				}
				
			}

			int k1 = BeaconTileEntity.EFFECTS_LIST[3].length + 1;
			int l1 = k1 * 22 + (k1 - 1) * 2;
			
			for (int i2 = 0; i2 < k1 - 1; ++i2) {
				
				Effect effect1 = BeaconTileEntity.EFFECTS_LIST[3][i2];
				ModBeaconScreen.PowerButton beaconscreen$powerbutton2 = new ModBeaconScreen.PowerButton(
						this.guiLeft + 167 + i2 * 24 - l1 / 2, this.guiTop + 47, effect1, false);
				this.addButton(beaconscreen$powerbutton2);
				
				if (3 >= i) {
					
					beaconscreen$powerbutton2.active = false;
					
				} else if (effect1 == this.secondaryEffect) {
					
					beaconscreen$powerbutton2.setSelected(true);
					
				}
				
			}

			if (this.primaryEffect != null) {
				
				ModBeaconScreen.PowerButton beaconscreen$powerbutton1 = new ModBeaconScreen.PowerButton(
						this.guiLeft + 167 + (k1 - 1) * 24 - l1 / 2, this.guiTop + 47, this.primaryEffect, false);
				this.addButton(beaconscreen$powerbutton1);
				
				if (3 >= i) {
					
					beaconscreen$powerbutton1.active = false;
					
				} else if (this.primaryEffect == this.secondaryEffect) {
					
					beaconscreen$powerbutton1.setSelected(true);
					
				}
				
			}
			
		}

		this.beaconConfirmButton.active = this.container.func_216970_h() && this.primaryEffect != null;
		
	}
	
	@Override
	protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
		
		drawCenteredString(matrixStack, this.font, field_243334_B, 62, 10, 14737632);
		drawCenteredString(matrixStack, this.font, field_243335_C, 169, 10, 14737632);

		for (Widget widget : this.buttons) {
			
			if (widget.isHovered()) {
				
				widget.renderToolTip(matrixStack, x - this.guiLeft, y - this.guiTop);
				break;
				
			}
			
		}

	}
	
	@Override
	@SuppressWarnings("deprecation")
	protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) {
		
		RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		this.minecraft.getTextureManager().bindTexture(BEACON_GUI_TEXTURES);
		int i = (this.width - this.xSize) / 2;
		int j = (this.height - this.ySize) / 2;
		this.blit(matrixStack, i, j, 0, 0, this.xSize, this.ySize);
		this.itemRenderer.zLevel = 100.0F;
		this.itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(Items.NETHERITE_INGOT), i + 20, j + 109);
		this.itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(Items.EMERALD), i + 41, j + 109);
		this.itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(Items.DIAMOND), i + 41 + 22, j + 109);
		this.itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(Items.GOLD_INGOT), i + 42 + 44, j + 109);
		this.itemRenderer.renderItemAndEffectIntoGUI(new ItemStack(Items.IRON_INGOT), i + 42 + 66, j + 109);
		this.itemRenderer.zLevel = 0.0F;
		
	}
	
	@Override
	public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
		
		this.renderBackground(matrixStack);
		super.render(matrixStack, mouseX, mouseY, partialTicks);
		this.renderHoveredTooltip(matrixStack, mouseX, mouseY);
		
	}
	
	abstract static class Button extends AbstractButton {
		
		private boolean selected;

		protected Button(int x, int y) {
			
			super(x, y, 22, 22, StringTextComponent.EMPTY);
			
		}
		
		@Override
		@SuppressWarnings("deprecation")
		public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
			
			Minecraft.getInstance().getTextureManager().bindTexture(ModBeaconScreen.BEACON_GUI_TEXTURES);
			RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
			int j = 0;
			
			if (!this.active) {
				
				j += this.width * 2;
				
			} else if (this.selected) {
				
				j += this.width * 1;
				
			} else if (this.isHovered()) {
				
				j += this.width * 3;
				
			}

			this.blit(matrixStack, this.x, this.y, j, 219, this.width, this.height);
			this.func_230454_a_(matrixStack);
			
		}
		
		protected abstract void func_230454_a_(MatrixStack p_230454_1_);

		public boolean isSelected() {
			
			return this.selected;
			
		}

		public void setSelected(boolean selectedIn) {
			
			this.selected = selectedIn;
			
		}
		
	}
	
	class CancelButton extends ModBeaconScreen.SpriteButton {
		
		public CancelButton(int x, int y) {
			
			super(x, y, 112, 220);
			
		}
		
		@Override
		public void onPress() {
			
			ModBeaconScreen.this.minecraft.player.connection.sendPacket(new CCloseWindowPacket(ModBeaconScreen.this.minecraft.player.openContainer.windowId));
			ModBeaconScreen.this.minecraft.displayGuiScreen((Screen) null);
			
		}
		
		@Override
		public void renderToolTip(MatrixStack matrixStack, int mouseX, int mouseY) {
			
			ModBeaconScreen.this.renderTooltip(matrixStack, DialogTexts.GUI_CANCEL, mouseX, mouseY);
			
		}
		
	}
	
	class ConfirmButton extends ModBeaconScreen.SpriteButton {
		
		public ConfirmButton(int x, int y) {
			
			super(x, y, 90, 220);
			
		}
		
		@Override
		public void onPress() {
			
			ModBeaconScreen.this.minecraft.getConnection().sendPacket(new CUpdateBeaconPacket(Effect.getId(ModBeaconScreen.this.primaryEffect),
							Effect.getId(ModBeaconScreen.this.secondaryEffect)));
			ModBeaconScreen.this.minecraft.player.connection.sendPacket(new CCloseWindowPacket(ModBeaconScreen.this.minecraft.player.openContainer.windowId));
			ModBeaconScreen.this.minecraft.displayGuiScreen((Screen) null);
			
		}
		
		@Override
		public void renderToolTip(MatrixStack matrixStack, int mouseX, int mouseY) {
			
			ModBeaconScreen.this.renderTooltip(matrixStack, DialogTexts.GUI_DONE, mouseX, mouseY);
			
		}
	}
	
	class PowerButton extends ModBeaconScreen.Button {
		
		private final Effect effect;
		private final TextureAtlasSprite field_212946_c;
		private final boolean field_212947_d;
		private final ITextComponent field_243336_e;
		
		public PowerButton(int x, int y, Effect p_i50827_4_, boolean p_i50827_5_) {
			
			super(x, y);
			this.effect = p_i50827_4_;
			this.field_212946_c = Minecraft.getInstance().getPotionSpriteUploader().getSprite(p_i50827_4_);
			this.field_212947_d = p_i50827_5_;
			this.field_243336_e = this.func_243337_a(p_i50827_4_, p_i50827_5_);
			
		}
		
		private ITextComponent func_243337_a(Effect p_243337_1_, boolean p_243337_2_) {
			
			IFormattableTextComponent iformattabletextcomponent = new TranslationTextComponent(p_243337_1_.getName());
			
			if (!p_243337_2_ && p_243337_1_ != Effects.REGENERATION) {
				
				iformattabletextcomponent.appendString(" II");
				
			}

			return iformattabletextcomponent;
			
		}
		
		@Override
		public void onPress() {
			
			if (!this.isSelected()) {
				
				if (this.field_212947_d) {
					
					ModBeaconScreen.this.primaryEffect = this.effect;
					
				} else {
					
					ModBeaconScreen.this.secondaryEffect = this.effect;
					
				}

				ModBeaconScreen.this.buttons.clear();
				ModBeaconScreen.this.children.clear();
				ModBeaconScreen.this.init();
				ModBeaconScreen.this.tick();
				
			}
			
		}
		
		@Override
		public void renderToolTip(MatrixStack matrixStack, int mouseX, int mouseY) {
			
			ModBeaconScreen.this.renderTooltip(matrixStack, this.field_243336_e, mouseX, mouseY);
			
		}

		@Override
		protected void func_230454_a_(MatrixStack p_230454_1_) {
			
			Minecraft.getInstance().getTextureManager().bindTexture(this.field_212946_c.getAtlasTexture().getTextureLocation());
			blit(p_230454_1_, this.x + 2, this.y + 2, this.getBlitOffset(), 18, 18, this.field_212946_c);
			
		}
		
	}
	
	abstract static class SpriteButton extends ModBeaconScreen.Button {
		
		private final int u;
		private final int v;

		protected SpriteButton(int x, int y, int u, int v) {
			
			super(x, y);
			this.u = u;
			this.v = v;
			
		}
		
		@Override
		protected void func_230454_a_(MatrixStack p_230454_1_) {
			
			this.blit(p_230454_1_, this.x + 2, this.y + 2, this.u, this.v, 18, 18);
			
		}
		
	}
	
}

 

my english is unfortunately not good which is why i use a online translator

Edited by Nyko
Posted

The beacon container uses a proximity check to see if the player is near enough to open the container..unfortunately this check is hardcoded to only work with the vanilla Blocks.BEACON. So if you created your own beacon block this will be different from the vanilla one, and the proximity check will always fail leading to the behavior you described above. You need to override canInteractWith in your ModBeaconContainer (which i suppose directly inherits from the BeaconContainer class?) and allow interaction with your own Beacon block

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Posted
8 hours ago, Beethoven92 said:

You need to override canInteractWith in your ModBeaconContainer (which i suppose directly inherits from the BeaconContainer class?) and allow interaction with your own Beacon block

I have already overwritten this method:

This is my container class (there is the method you mean, or is there another one):

 

	private final IInventory tileBeacon = new Inventory(1) {
		@Override
		public boolean isItemValidForSlot(int index, ItemStack stack) {
			return stack.getItem().isIn(ItemTags.BEACON_PAYMENT_ITEMS);
		}
		
		@Override
		public int getInventoryStackLimit() {
			return 1;
		}
	};
	private final ModBeaconContainer.BeaconSlot beaconSlot;
	private final IWorldPosCallable worldPosCallable;
	private final IIntArray field_216972_f;

	public ModBeaconContainer(int id, IInventory p_i50099_2_, PacketBuffer extraData) {
		
		this(id, p_i50099_2_, new IntArray(3), IWorldPosCallable.DUMMY);
		
	}

	public ModBeaconContainer(int id, IInventory inventory, IIntArray p_i50100_3_, IWorldPosCallable worldPosCallable) {
		
		super(ModContainerType.BEACON.get(), id);
		assertIntArraySize(p_i50100_3_, 3);
		this.field_216972_f = p_i50100_3_;
		this.worldPosCallable = worldPosCallable;
		this.beaconSlot = new ModBeaconContainer.BeaconSlot(this.tileBeacon, 0, 136, 110);
		this.addSlot(this.beaconSlot);
		this.trackIntArray(p_i50100_3_);

		for (int k = 0; k < 3; ++k) {
			
			for (int l = 0; l < 9; ++l) {
				
				this.addSlot(new Slot(inventory, l + k * 9 + 9, 36 + l * 18, 137 + k * 18));
				
			}
			
		}

		for (int i1 = 0; i1 < 9; ++i1) {
			
			this.addSlot(new Slot(inventory, i1, 36 + i1 * 18, 195));
			
		}

	}

	@Override
	public void onContainerClosed(PlayerEntity playerIn) {
		
		super.onContainerClosed(playerIn);
		
		if (!playerIn.world.isRemote) {
			
			ItemStack itemstack = this.beaconSlot.decrStackSize(this.beaconSlot.getSlotStackLimit());
			
			if (!itemstack.isEmpty()) {
				
				playerIn.dropItem(itemstack, false);
				
			}

		}
		
	}

	@Override
	public boolean canInteractWith(PlayerEntity playerIn) {
		
		return true;
		
	}
	
	@Override
	public void updateProgressBar(int id, int data) {
		
		super.updateProgressBar(id, data);
		this.detectAndSendChanges();
		
	}

	@Override
	public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		
		ItemStack itemstack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.get(index);
		
		if (slot != null && slot.getHasStack()) {
			
			ItemStack itemstack1 = slot.getStack();
			itemstack = itemstack1.copy();
			
			if (index == 0) {
				
				if (!this.mergeItemStack(itemstack1, 1, 37, true)) {
					
					return ItemStack.EMPTY;
					
				}

				slot.onSlotChange(itemstack1, itemstack);
				
			} else if (this.mergeItemStack(itemstack1, 0, 1, false)) { 
																		
				return ItemStack.EMPTY;
				
			} else if (index >= 1 && index < 28) {
				
				if (!this.mergeItemStack(itemstack1, 28, 37, false)) {
					
					return ItemStack.EMPTY;
					
				}
			} else if (index >= 28 && index < 37) {
				
				if (!this.mergeItemStack(itemstack1, 1, 28, false)) {
					
					return ItemStack.EMPTY;
					
				}
				
			} else if (!this.mergeItemStack(itemstack1, 1, 37, false)) {
				
				return ItemStack.EMPTY;
				
			}

			if (itemstack1.isEmpty()) {
				
				slot.putStack(ItemStack.EMPTY);
				
			} else {
				
				slot.onSlotChanged();
				
			}

			if (itemstack1.getCount() == itemstack.getCount()) {
				
				return ItemStack.EMPTY;
				
			}

			slot.onTake(playerIn, itemstack1);
			
		}

		return itemstack;
		
	}

	@OnlyIn(Dist.CLIENT)
	public int func_216969_e() {
		
		return this.field_216972_f.get(0);
		
	}

	@Nullable
	@OnlyIn(Dist.CLIENT)
	public Effect func_216967_f() {
		
		return Effect.get(this.field_216972_f.get(1));
		
	}

	@Nullable
	@OnlyIn(Dist.CLIENT)
	public Effect func_216968_g() {
		
		return Effect.get(this.field_216972_f.get(2));
		
	}
	
	public void func_216966_c(int p_216966_1_, int p_216966_2_) {
		
		if (this.beaconSlot.getHasStack()) {
			
			this.field_216972_f.set(1, p_216966_1_);
			this.field_216972_f.set(2, p_216966_2_);
			this.beaconSlot.decrStackSize(1);
			
		}

	}

	@OnlyIn(Dist.CLIENT)
	public boolean func_216970_h() {
		
		return !this.tileBeacon.getStackInSlot(0).isEmpty();
		
	}

	public IWorldPosCallable getWorldPosCallable() {
		
		return worldPosCallable;
		
	}

	class BeaconSlot extends Slot {
		
		public BeaconSlot(IInventory inventoryIn, int index, int xIn, int yIn) {
			
			super(inventoryIn, index, xIn, yIn);
			
		}

		@Override
		public boolean isItemValid(ItemStack stack) {
			
			return stack.getItem().isIn(ItemTags.BEACON_PAYMENT_ITEMS);
			
		}

		@Override
		public int getSlotStackLimit() {
			
			return 1;
			
		}
		
	}

 

Posted (edited)

I apologize...i misread the part where you say that your custom beacon inventory actually opens fine! Yeah, the proximity check is doing its job, the problem seems to be that when pressing the confirm button you are sending a vanilla CUpdateBeaconPacket, then handled by the server, which will check if your open container is a BeaconContainer. It would be helpful to see the complete code you have, please post a link to your repository

Edited by Beethoven92

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Posted
11 hours ago, Beethoven92 said:

This means your confirm button does basically nothing more than just closing the gui

what do I have to change so that the button works correctly or can I remove the CUpdateBeaconPacket?

Posted

If your custom beacon container has the same function as the vanilla container why not just having your container inherit from the BeaconContainer class (keep in mind that you would still need to override the proximity check)? This way the server side check when using the confirm button won't fail

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Posted
1 hour ago, Beethoven92 said:

If your custom beacon container has the same function as the vanilla container why not just having your container inherit from the BeaconContainer class

I changed that

 

1 hour ago, Beethoven92 said:

still need to override the proximity check

which method? what do I have to overwrite?

Posted
2 hours ago, Beethoven92 said:

The method is canInteractWith

okay since i probably misunderstood something.

I thought I had to override another method

 

thanks now it works

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

    • 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() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

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