Jump to content

Recommended Posts

Posted

I Keep getting an error on my block

when it is activated

 

MainClass:

 

package com.CraigTheMailman.MinecraftExpansion;

 

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.creativetab.CreativeTabs;

 

import com.CraigTheMailman.MinecraftExpansion.blocks.WorkSurface;

import com.CraigTheMailman.MinecraftExpansion.lib.ProxyCommon;

import com.CraigTheMailman.MinecraftExpansion.lib.Refrences;

import com.CraigTheMailman.MinecraftExpansion.tab.tabK;

 

import cpw.mods.fml.common.Mod;

import cpw.mods.fml.common.Mod.EventHandler;

import cpw.mods.fml.common.Mod.Instance;

import cpw.mods.fml.common.SidedProxy;

import cpw.mods.fml.common.event.FMLInitializationEvent;

import cpw.mods.fml.common.registry.GameRegistry;

import cpw.mods.fml.common.registry.LanguageRegistry;

 

@Mod(modid = Refrences.modid, version = Refrences.version)

public class MinecraftExpansion {

 

@SidedProxy(clientSide = Refrences.Client,

serverSide = Refrences.Common)

public static ProxyCommon proxy;

 

@Instance

public static MinecraftExpansion instance;

 

//Blocks

public static Block blockWorkSurface;

public static final int guiIDCustom1 = 1;

 

public static CreativeTabs tabK = new tabK(CreativeTabs.getNextID(), "tabK");

 

@EventHandler

public void init(FMLInitializationEvent event)

{

blockWorkSurface = new WorkSurface(Material.wood);

 

GameRegistry.registerBlock(blockWorkSurface, "CM1");

 

LanguageRegistry.addName(blockWorkSurface, "Metalwork Cooler");

}

 

@EventHandler

public void load(FMLInitializationEvent event)

{

proxy.registerRenderInformation();

}

 

}

 

 

 

Block Class:

 

package com.CraigTheMailman.MinecraftExpansion.blocks;

 

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.client.renderer.texture.IIconRegister;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.util.IIcon;

import net.minecraft.world.World;

import com.CraigTheMailman.MinecraftExpansion.MinecraftExpansion;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class WorkSurface extends Block {

 

@SideOnly(Side.CLIENT)

private IIcon workSurfaceTop;

 

public WorkSurface(Material wood) {

super(Material.wood);

 

this.setHardness(3.5F);

this.setResistance(5.0F);

this.setCreativeTab(MinecraftExpansion.tabK);

}

 

@SideOnly(Side.CLIENT)

public IIcon getIcon (int side, int metadata) {

return side == 1 ? this.workSurfaceTop : this.blockIcon;

}

 

@SideOnly(Side.CLIENT)

public void registerBlockIcons (IIconRegister iconRegister) {

this.blockIcon = iconRegister.registerIcon("MCExpansion:CustomMech1_Side");

this.workSurfaceTop = iconRegister.registerIcon("MCExpansion:CustomMech1_Top");

}

 

public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {

 

if (!player.isSneaking()) {

player.openGui(MinecraftExpansion.instance, MinecraftExpansion.guiIDCustom1, world, x, y, z);

return true;

}else{

return false;

}

}

 

 

}

 

 

And, of course last, but not least!

CRASH REPORT:

 

---- Minecraft Crash Report ----

// Uh... Did I do that?

 

Time: 5/23/14 1:28 AM

Description: Unexpected error

 

java.lang.NullPointerException: Unexpected error

at cpw.mods.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:263)

at cpw.mods.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:93)

at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2208)

at com.CraigTheMailman.MinecraftExpansion.blocks.WorkSurface.onBlockActivated(WorkSurface.java:40)

at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerRightClick(PlayerControllerMP.java:326)

at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1399)

at net.minecraft.client.Minecraft.runTick(Minecraft.java:1903)

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:916)

at net.minecraft.client.Minecraft.run(Minecraft.java:835)

at net.minecraft.client.main.Main.main(SourceFile:103)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at cpw.mods.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:263)

at cpw.mods.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:93)

at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2208)

at com.CraigTheMailman.MinecraftExpansion.blocks.WorkSurface.onBlockActivated(WorkSurface.java:40)

at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerRightClick(PlayerControllerMP.java:326)

at net.minecraft.client.Minecraft.func_147121_ag(Minecraft.java:1399)

 

-- Affected level --

Details:

Level name: MpServer

All players: 1 total; [EntityClientPlayerMP['Player706'/65, l='MpServer', x=20.74, y=5.62, z=2568.08]]

Chunk stats: MultiplayerChunkCache: 225, 225

Level seed: 0

Level generator: ID 01 - flat, ver 0. Features enabled: false

Level generator options:

Level spawn location: World: (26,4,2562), Chunk: (at 10,0,2 in 1,160; contains blocks 16,0,2560 to 31,255,2575), Region: (0,5; contains chunks 0,160 to 31,191, blocks 0,0,2560 to 511,255,3071)

Level time: 717 game time, 717 day time

Level dimension: 0

Level storage version: 0x00000 - Unknown?

Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)

Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false

Forced entities: 50 total; [EntitySlime['Slime'/3, l='MpServer', x=-59.63, y=4.00, z=2547.47], EntitySlime['Slime'/4, l='MpServer', x=-50.63, y=4.99, z=2569.21], EntitySlime['Slime'/5, l='MpServer', x=-60.04, y=4.00, z=2571.21], EntitySlime['Slime'/6, l='MpServer', x=-51.66, y=4.00, z=2566.03], EntitySlime['Slime'/7, l='MpServer', x=-53.28, y=5.06, z=2588.62], EntitySlime['Slime'/11785, l='MpServer', x=-28.80, y=4.00, z=2576.20], EntitySlime['Slime'/11, l='MpServer', x=-39.16, y=4.00, z=2507.31], EntitySlime['Slime'/12, l='MpServer', x=-35.56, y=4.00, z=2545.88], EntitySlime['Slime'/15, l='MpServer', x=-23.11, y=4.39, z=2492.87], EntitySlime['Slime'/17, l='MpServer', x=-27.76, y=5.06, z=2611.14], EntitySlime['Slime'/18, l='MpServer', x=-9.75, y=4.00, z=2525.00], EntitySlime['Slime'/19, l='MpServer', x=12.03, y=4.00, z=2580.94], EntitySlime['Slime'/20, l='MpServer', x=3.36, y=4.00, z=2603.38], EntitySlime['Slime'/21, l='MpServer', x=16.06, y=4.00, z=2619.09], EntitySlime['Slime'/23, l='MpServer', x=13.55, y=4.33, z=2641.81], EntityVillager['Villager'/24, l='MpServer', x=46.22, y=6.00, z=2615.50], EntityVillager['Villager'/25, l='MpServer', x=37.59, y=5.00, z=2637.88], EntityVillager['Villager'/26, l='MpServer', x=38.69, y=5.00, z=2637.44], EntitySlime['Slime'/27, l='MpServer', x=46.22, y=4.00, z=2649.02], EntitySlime['Slime'/29, l='MpServer', x=47.28, y=4.99, z=2556.92], EntityVillager['Villager'/30, l='MpServer', x=57.66, y=4.00, z=2599.69], EntityVillager['Villager'/31, l='MpServer', x=50.84, y=4.00, z=2613.50], EntityVillager['Villager'/32, l='MpServer', x=51.78, y=4.00, z=2616.88], EntityVillager['Villager'/33, l='MpServer', x=62.38, y=5.00, z=2631.63], EntityVillager['Villager'/34, l='MpServer', x=62.28, y=5.00, z=2633.50], EntityVillager['Villager'/35, l='MpServer', x=50.16, y=4.00, z=2626.59], EntitySlime['Slime'/36, l='MpServer', x=56.44, y=4.00, z=2645.38], EntitySlime['Slime'/38, l='MpServer', x=73.82, y=4.33, z=2492.37], EntityVillager['Villager'/39, l='MpServer', x=71.50, y=6.00, z=2596.22], EntityClientPlayerMP['Player706'/65, l='MpServer', x=20.74, y=5.62, z=2568.08], EntityVillager['Villager'/40, l='MpServer', x=70.84, y=4.00, z=2600.41], EntitySlime['Slime'/41, l='MpServer', x=76.31, y=4.00, z=2596.34], EntityVillager['Villager'/42, l='MpServer', x=76.75, y=5.00, z=2613.56], EntityVillager['Villager'/43, l='MpServer', x=77.50, y=5.00, z=2608.50], EntityVillager['Villager'/44, l='MpServer', x=79.50, y=5.00, z=2616.50], EntityVillager['Villager'/45, l='MpServer', x=66.49, y=5.00, z=2616.21], EntityVillager['Villager'/46, l='MpServer', x=73.78, y=4.00, z=2612.34], EntityVillager['Villager'/47, l='MpServer', x=71.88, y=4.00, z=2616.38], EntityVillager['Villager'/48, l='MpServer', x=77.41, y=4.00, z=2632.66], EntityVillager['Villager'/49, l='MpServer', x=74.56, y=4.00, z=2631.66], EntityVillager['Villager'/50, l='MpServer', x=64.63, y=4.00, z=2628.59], EntitySlime['Slime'/53, l='MpServer', x=91.54, y=4.57, z=2512.29], EntityVillager['Villager'/54, l='MpServer', x=80.50, y=5.00, z=2616.50], EntitySlime['Slime'/2615, l='MpServer', x=72.17, y=4.62, z=2528.23], EntityVillager['Villager'/55, l='MpServer', x=84.72, y=4.00, z=2613.22], EntitySkeleton['Skeleton'/56, l='MpServer', x=86.50, y=9.00, z=2610.50], EntityVillager['Villager'/57, l='MpServer', x=82.63, y=4.00, z=2625.44], EntityVillager['Villager'/58, l='MpServer', x=80.75, y=4.00, z=2627.44], EntitySlime['Slime'/59, l='MpServer', x=96.33, y=4.39, z=2513.28], EntitySlime['Slime'/5204, l='MpServer', x=90.18, y=4.00, z=2579.04]]

Retry entities: 0 total; []

Server brand: fml,forge

Server type: Integrated singleplayer server

Stacktrace:

at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:368)

at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2382)

at net.minecraft.client.Minecraft.run(Minecraft.java:864)

at net.minecraft.client.main.Main.main(SourceFile:103)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

 

-- System Details --

Details:

Minecraft Version: 1.7.2

Operating System: Windows Vista (amd64) version 6.0

Java Version: 1.8.0_05, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 922878464 bytes (880 MB) / 1056309248 bytes (1007 MB) up to 1056309248 bytes (1007 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

AABB Pool Size: 20006 (1120336 bytes; 1 MB) allocated, 2 (112 bytes; 0 MB) used

IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0

FML: MCP v9.03 FML v7.2.196.1085 Minecraft Forge 10.12.1.1085 4 mods loaded, 4 mods active

mcp{9.03} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

FML{7.2.196.1085} [Forge Mod Loader] (forgeBin-1.7.2-10.12.1.1085.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Forge{10.12.1.1085} [Minecraft Forge] (forgeBin-1.7.2-10.12.1.1085.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Minecraft Expansion{1.0} [Minecraft Expansion] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Launched Version: 1.6

LWJGL: 2.9.0

OpenGL: Mobile Intel® 4 Series Express Chipset Family GL version 2.1.0 - Build 8.15.10.2302, Intel

Is Modded: Definitely; Client brand changed to 'fml,forge'

Type: Client (map_client.txt)

Resource Packs: []

Current Language: English (US)

Profiler Position: N/A (disabled)

Vec3 Pool Size: 1228 (68768 bytes; 0 MB) allocated, 21 (1176 bytes; 0 MB) used

Anisotropic Filtering: Off (1)

 

Posted

Try this:

 

package com.CraigTheMailman.MinecraftExpansion.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import com.CraigTheMailman.MinecraftExpansion.MinecraftExpansion;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class WorkSurface extends Block {

   @SideOnly(Side.CLIENT)
   private IIcon workSurfaceTop;

   public WorkSurface(Material wood) {
      super(Material.wood);

      this.setHardness(3.5F);
      this.setResistance(5.0F);
      this.setCreativeTab(MinecraftExpansion.tabK);
   }

   @SideOnly(Side.CLIENT)
   public IIcon getIcon (int side, int metadata) {
      return side == 1 ? this.workSurfaceTop : this.blockIcon;
   }

   @SideOnly(Side.CLIENT)
   public void registerBlockIcons (IIconRegister iconRegister) {
      this.blockIcon = iconRegister.registerIcon("MCExpansion:CustomMech1_Side");
      this.workSurfaceTop = iconRegister.registerIcon("MCExpansion:CustomMech1_Top");
   }

   public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {

      if(!player.isSneaking() && !world.isRemote) {
         FMLNetworkHandler.openGui(player, MinecraftExpansion.instance, MinecraftExpansion.guiIDCustom1, world, x, y, z);
      }
      return true;
   }


}

Posted

@diesieben07:

 

GUI HANDLER:

 

package com.CraigTheMailman.MinecraftExpansion.gui;

 

import org.lwjgl.opengl.GL11;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.util.ResourceLocation;

import net.minecraft.util.StatCollector;

import net.minecraft.world.World;

 

public class GuiWorkSurface extends GuiContainer {

 

private ResourceLocation texture = new ResourceLocation("mcexpansion:textures/gui/WorkSurface.png");

 

public GuiWorkSurface(InventoryPlayer invPlayer, World world, int x, int y, int z) {

super(new com.CraigTheMailman.MinecraftExpansion.container.ContainerWorkSurface(invPlayer, world, x, y, z));

 

this.xSize = 176;

this.ySize = 188;

 

}

 

 

public void onGuiClosed() {

super.onGuiClosed();

}

 

protected void drawGuiContainerForegroundLayer(int i, int j) {

 

this.fontRendererObj.drawString(StatCollector.translateToLocal("Work Surface"), 100, 5, 0xFFFFFF);

 

}

 

@Override

protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {

 

GL11.glColor4f(1F, 1F, 1F, 1F);

 

Minecraft.getMinecraft().getTextureManager().bindTexture(texture);

 

drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

 

}

 

}

 

 

 

Sorry, kinda knew i forgot something...

Posted

A gui handler is a class implementing IGuiHandler. If you implement it correctly, you need to add two methods. Add those. In that method, check if the ID is the same as the ID specified in, in your case MinecraftExpansion.guiIDCustom1, and if that that, return a new gui in the client method, and a container in the server method.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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.