Jump to content

Recommended Posts

  • Replies 73
  • Created
  • Last Reply

Top Posters In This Topic

Posted
Block brokenBlock = Block.blocksList[world.getBlockId(x, y, z)];
int meta = world.getBlockMetadata(x, y, z);
ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

Posted

Block brokenBlock = Block.blocksList[world.getBlockId(x, y, z)];
int meta = world.getBlockMetadata(x, y, z);
ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

Ok does this just check if it is a smelting recipe or should this be all the code I need for it to work?

Posted

Yes.

For the auto-smelting pickaxe this is my code but how do I drop the item I've messed around and whenever I try something it crashes when i break the block and doesnt work

public boolean onBlockDestroyed(ItemStack itemStack, World world, int par1, int par2, int par3, int par4, EntityLivingBase entityLivingBase) {

	Block brokenBlock = Block.blocksList[world.getBlockId(par1, par1, par3)];
	int meta = world.getBlockMetadata(par1, par2, par3);
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null) {

		//How do I drop the item?

	}

	return true;

}

Posted

EntityItem item = new EntityItem(world, i, j, k, result);
world.spawnEntityInWorld(item);

When i login and break a block I crash and when i log back in theres  the ingot and the ore on the ground if it was an ore block i broke

public boolean onBlockDestroyed(ItemStack itemStack, World world, int par1, int par2, int par3, int par4, EntityLivingBase entityLivingBase) {

	Block brokenBlock = Block.blocksList[world.getBlockId(par2, par3, par4)];
	int meta = world.getBlockMetadata(par2, par3, par4);
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null) {

		EntityItem drop = new EntityItem(world, par2, par3, par4, result);
		world.spawnEntityInWorld(drop);

	}

	return false;

}

Posted

Okay, well I kind of need a crash log for that.

Ok here

---- Minecraft Crash Report ----

// I feel sad now :(

 

Time: 7/21/13 4:09 PM

Description: Unexpected error

 

java.lang.NullPointerException

at net.minecraft.item.ItemStack.<init>(ItemStack.java:71)

at Kenoba10.Too_Much_Tools.NetherStar.ItemNetherStarPickaxe.onBlockDestroyed(ItemNetherStarPickaxe.java:33)

at net.minecraft.item.ItemStack.onBlockDestroyed(ItemStack.java:402)

at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDestroyBlock(PlayerControllerMP.java:174)

at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDamageBlock(PlayerControllerMP.java:293)

at net.minecraft.client.Minecraft.sendClickBlockToController(Minecraft.java:1307)

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

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

at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:826)

at net.minecraft.client.main.Main.main(Main.java:93)

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

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

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

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

 

 

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

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

 

-- Head --

Stacktrace:

at net.minecraft.item.ItemStack.<init>(ItemStack.java:71)

at Kenoba10.Too_Much_Tools.NetherStar.ItemNetherStarPickaxe.onBlockDestroyed(ItemNetherStarPickaxe.java:33)

at net.minecraft.item.ItemStack.onBlockDestroyed(ItemStack.java:402)

at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDestroyBlock(PlayerControllerMP.java:174)

at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDamageBlock(PlayerControllerMP.java:293)

at net.minecraft.client.Minecraft.sendClickBlockToController(Minecraft.java:1307)

 

-- Affected level --

Details:

Level name: MpServer

All players: 1 total; [EntityClientPlayerMP['Player747'/302, l='MpServer', x=204.59, y=62.62, z=251.30]]

Chunk stats: MultiplayerChunkCache: 441

Level seed: 0

Level generator: ID 00 - default, ver 1. Features enabled: false

Level generator options:

Level spawn location: World: (232,64,256), Chunk: (at 8,4,0 in 14,16; contains blocks 224,0,256 to 239,255,271), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)

Level time: 29596 game time, 1312 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: survival (ID 0). Hardcore: false. Cheats: false

Forced entities: 70 total; [EntityZombie['Zombie'/137, l='MpServer', x=243.69, y=27.00, z=275.47], EntityBat['Bat'/136, l='MpServer', x=248.75, y=32.10, z=243.50], EntityCow['Cow'/142, l='MpServer', x=264.59, y=69.00, z=237.47], EntitySkeleton['Skeleton'/128, l='MpServer', x=239.50, y=30.00, z=273.50], EntityZombie['Zombie'/133, l='MpServer', x=247.34, y=27.00, z=211.94], EntityZombie['Zombie'/132, l='MpServer', x=243.30, y=11.00, z=208.70], EntityBat['Bat'/135, l='MpServer', x=248.41, y=32.10, z=243.47], EntitySkeleton['Skeleton'/134, l='MpServer', x=245.09, y=24.00, z=218.47], EntityCow['Cow'/152, l='MpServer', x=283.31, y=65.00, z=254.47], EntityZombie['Zombie'/153, l='MpServer', x=276.50, y=21.00, z=273.50], EntitySkeleton['Skeleton'/42, l='MpServer', x=133.59, y=42.00, z=213.94], EntitySkeleton['Skeleton'/43, l='MpServer', x=132.50, y=41.00, z=222.50], EntityZombie['Zombie'/41, l='MpServer', x=127.97, y=53.00, z=326.47], EntityZombie['Zombie'/46, l='MpServer', x=142.31, y=38.00, z=274.56], EntityCreeper['Creeper'/47, l='MpServer', x=140.94, y=30.00, z=291.59], EntityBat['Bat'/44, l='MpServer', x=132.00, y=27.10, z=241.56], EntityZombie['Zombie'/45, l='MpServer', x=134.72, y=24.00, z=258.50], EntitySquid['Squid'/51, l='MpServer', x=157.50, y=56.38, z=223.13], EntityMinecartChest['entity.MinecartChest.name'/49, l='MpServer', x=139.50, y=32.50, z=330.50], EntityOcelot['Ocelot'/48, l='MpServer', x=138.06, y=65.00, z=318.56], EntityBat['Bat'/55, l='MpServer', x=156.53, y=36.10, z=296.75], EntityZombie['Zombie'/54, l='MpServer', x=146.31, y=26.00, z=250.88], EntityZombie['Zombie'/53, l='MpServer', x=146.50, y=26.00, z=252.50], EntityBat['Bat'/52, l='MpServer', x=146.59, y=18.10, z=247.75], EntityCreeper['Creeper'/58, l='MpServer', x=145.50, y=51.00, z=323.50], EntityMinecartChest['entity.MinecartChest.name'/57, l='MpServer', x=147.50, y=36.50, z=318.50], EntityCreeper['Creeper'/56, l='MpServer', x=145.00, y=31.00, z=310.41], EntityZombie['Zombie'/63, l='MpServer', x=175.03, y=54.00, z=249.47], EntityCow['Cow'/62, l='MpServer', x=171.50, y=67.00, z=225.50], EntitySquid['Squid'/61, l='MpServer', x=172.50, y=56.38, z=213.50], EntityClientPlayerMP['Player747'/302, l='MpServer', x=204.59, y=62.62, z=251.30], EntityItem['item.item.dyePowder.black'/64, l='MpServer', x=161.38, y=18.13, z=256.84], EntityBat['Bat'/65, l='MpServer', x=174.72, y=33.10, z=292.53], EntityCreeper['Creeper'/66, l='MpServer', x=160.50, y=31.00, z=314.50], EntityCow['Cow'/77, l='MpServer', x=187.88, y=63.00, z=175.91], EntityCow['Cow'/78, l='MpServer', x=188.78, y=63.00, z=208.75], EntitySkeleton['Skeleton'/79, l='MpServer', x=183.53, y=28.00, z=235.88], EntityMinecartChest['entity.MinecartChest.name'/85, l='MpServer', x=182.50, y=28.50, z=329.50], EntityCow['Cow'/84, l='MpServer', x=186.47, y=67.00, z=312.63], EntityCow['Cow'/86, l='MpServer', x=191.03, y=64.00, z=330.13], EntitySkeleton['Skeleton'/81, l='MpServer', x=190.50, y=21.00, z=269.50], EntityCow['Cow'/80, l='MpServer', x=183.06, y=63.00, z=251.91], EntityCow['Cow'/83, l='MpServer', x=189.97, y=63.00, z=308.88], EntityCreeper['Creeper'/82, l='MpServer', x=188.50, y=33.00, z=265.53], EntityCow['Cow'/93, l='MpServer', x=204.50, y=71.00, z=175.50], EntityCow['Cow'/95, l='MpServer', x=201.13, y=65.00, z=180.13], EntityCow['Cow'/94, l='MpServer', x=206.44, y=65.00, z=191.25], EntityBat['Bat'/102, l='MpServer', x=201.13, y=28.02, z=285.50], EntityCow['Cow'/103, l='MpServer', x=201.69, y=64.00, z=284.72], EntitySkeleton['Skeleton'/100, l='MpServer', x=195.59, y=17.21, z=283.30], EntityCreeper['Creeper'/101, l='MpServer', x=200.69, y=26.36, z=277.69], EntityCreeper['Creeper'/98, l='MpServer', x=194.09, y=21.00, z=268.97], EntitySpider['Spider'/99, l='MpServer', x=191.50, y=22.00, z=272.75], EntityCow['Cow'/96, l='MpServer', x=196.81, y=64.00, z=188.88], EntitySkeleton['Skeleton'/97, l='MpServer', x=196.50, y=21.00, z=267.50], EntityCreeper['Creeper'/110, l='MpServer', x=208.50, y=26.00, z=269.50], EntityCreeper['Creeper'/111, l='MpServer', x=213.00, y=28.00, z=270.63], EntityBat['Bat'/108, l='MpServer', x=227.88, y=13.01, z=239.03], EntitySpider['Spider'/109, l='MpServer', x=208.72, y=58.77, z=250.94], EntityCow['Cow'/104, l='MpServer', x=192.44, y=63.00, z=316.09], EntityEnderman['Enderman'/117, l='MpServer', x=211.22, y=39.00, z=324.34], EntityCow['Cow'/116, l='MpServer', x=223.31, y=64.00, z=316.22], EntitySpider['Spider'/115, l='MpServer', x=211.28, y=48.00, z=305.47], EntityCreeper['Creeper'/114, l='MpServer', x=221.63, y=53.00, z=307.03], EntitySpider['Spider'/113, l='MpServer', x=212.53, y=40.00, z=319.25], EntityCreeper['Creeper'/112, l='MpServer', x=208.28, y=26.00, z=272.97], EntityCreeper['Creeper'/127, l='MpServer', x=225.69, y=17.00, z=250.44], EntityBat['Bat'/126, l='MpServer', x=230.25, y=32.10, z=230.38], EntityCreeper['Creeper'/125, l='MpServer', x=227.31, y=11.00, z=234.63], EntityCreeper['Creeper'/124, l='MpServer', x=232.50, y=32.00, z=207.00]]

Retry entities: 0 total; []

Server brand: fml,forge

Server type: Integrated singleplayer server

Stacktrace:

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

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

at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:851)

at net.minecraft.client.main.Main.main(Main.java:93)

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

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

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

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

 

-- System Details --

Details:

Minecraft Version: 1.6.2

Operating System: Windows 7 (amd64) version 6.1

Java Version: 1.7.0_25, Oracle Corporation

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

Memory: 810777376 bytes (773 MB) / 1038876672 bytes (990 MB) up to 1038876672 bytes (990 MB)

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

AABB Pool Size: 7171 (401576 bytes; 0 MB) allocated, 1027 (57512 bytes; 0 MB) used

Suspicious classes: FML and Forge are installed

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

FML: MCP v8.04 FML v6.2.19.787 Minecraft Forge 9.10.0.787 4 mods loaded, 4 mods active

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

FML{6.2.19.787} [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Forge{9.10.0.787} [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

TooMuchTools{1.0} [Too Much Tools] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Launched Version: 1.6

LWJGL: 2.9.0

OpenGL: Intel® HD Graphics Family GL version 3.1.0 - Build 8.15.10.2430, Intel

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

Type: Client (map_client.txt)

Resource Pack: Default

Current Language: English (US)

Profiler Position: N/A (disabled)

Vec3 Pool Size: 1971 (110376 bytes; 0 MB) allocated, 223 (12488 bytes; 0 MB) used

Posted
if(brokenBlock != null)
{
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null)
	{
		EntityItem drop = new EntityItem(world, par2, par3, par4, result);
		world.spawnEntityInWorld(drop);
		return false;
	}
}

Posted

if(brokenBlock != null)
{
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null)
	{
		EntityItem drop = new EntityItem(world, par2, par3, par4, result);
		world.spawnEntityInWorld(drop);
		return false;
	}
}

Thanks but when I break it with the pick the first time i break that ore type I get an ingot and the ore, the rest of the then I just get the ore back

Posted

if(world.getBlockId(i, j, k) == Block.wood.blockID)
	{
		for(int x = -1; x <= 1; x++)
		{
			for(int y = 0; y <= 15; y++)
			{
				for(int z = -1; z <= 1; z++)
				{
					if(world.getBlockId(i + x, j + y, k + z) == Block.wood.blockID)
					{
						int meta = world.getBlockMetadata(i + x, j + y, k + z);
						ItemStack drop = new ItemStack(Block.wood, 1, meta);
						EntityItem item = new EntityItem(world, i + x, j + y, k + z, drop);
						world.setBlock(i + x, j + y, k + z, 0);
						world.spawnEntityInWorld(item);
					}
				}
			}
		}
	}

 

That first if statement seems EXTREMELY redundant... But oh well :P

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

if(brokenBlock != null)
{
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null)
	{
		EntityItem drop = new EntityItem(world, par2, par3, par4, result);
		world.spawnEntityInWorld(drop);
		return false;
	}
}

Thanks but when I break it with the pick the first time i break that ore type I get an ingot and the ore, the rest of the then I just get the ore back

 

That makes sense. The problem with the ore is the fact that what a block drops is set inside the block's class. And to my knowledge, there is no way to void that block drop. But I guess you could do a hackish type job by using gamerules... By that I mean set block drops to false and then back to true again once it dropped the item.

 

As to it only working once... I am not sure :/

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

That first if statement seems EXTREMELY redundant... But oh well :P

It's to tell it that breaking a block beneath a wood block won't have the "timber" effect. If that weren't there it could work by breaking it otherwise. Thanks for the concern, though.

Posted

That first if statement seems EXTREMELY redundant... But oh well :P

It's to tell it that breaking a block beneath a wood block won't have the "timber" effect. If that weren't there it could work by breaking it otherwise. Thanks for the concern, though.

 

That still doesn't quite work out logically in my mind... Why check the block you are breaking is wood, if you are wanting to check the block under it? That is what caused my confusion in the first place.

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

That still doesn't quite work out logically in my mind... Why check the block you are breaking is wood, if you are wanting to check the block under it? That is what caused my confusion in the first place.

So for example, you are using the axe to break a dirt block. Above the dirt block is a tree. The timber effect should not take place since you are breaking dirt. That is why it checks for logs first, so that "hey, you're breaking a wood block now, let's know out the rest of 'em" will happen.

Posted

That still doesn't quite work out logically in my mind... Why check the block you are breaking is wood, if you are wanting to check the block under it? That is what caused my confusion in the first place.

So for example, you are using the axe to break a dirt block. Above the dirt block is a tree. The timber effect should not take place since you are breaking dirt. That is why it checks for logs first, so that "hey, you're breaking a wood block now, let's know out the rest of 'em" will happen.

 

That makes sense now :P I didn't think of that... Good job mate! Glad someone has common sense :P

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

if(brokenBlock != null)
{
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null)
	{
		EntityItem drop = new EntityItem(world, par2, par3, par4, result);
		world.spawnEntityInWorld(drop);
		return false;
	}
}

For the pickaxe do you know of any way to prevent the default block drop and when I break the ore for the first time it gives me an ingot and the ore. The rest of the time i only get the ore.

Posted

if(brokenBlock != null)
{
	ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(new ItemStack(brokenBlock, 1, meta));

	if(result != null)
	{
		EntityItem drop = new EntityItem(world, par2, par3, par4, result);
		world.spawnEntityInWorld(drop);
		return false;
	}
}

Thanks but when I break it with the pick the first time i break that ore type I get an ingot and the ore, the rest of the then I just get the ore back

 

That makes sense. The problem with the ore is the fact that what a block drops is set inside the block's class. And to my knowledge, there is no way to void that block drop. But I guess you could do a hackish type job by using gamerules... By that I mean set block drops to false and then back to true again once it dropped the item.

 

As to it only working once... I am not sure :/

Ok how could I do that gamerule thing? Also when I overrided canHarvestBlock when I broke a block I got the ingot back and niot the ore but it took a super long time to mine and then for all blocks mined with the pick this happened

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.