Jump to content

Recommended Posts

Posted

How can i make a player invisible to mobs on wearing an specific armor set, by the way the player is not been attacked by them.

 

I've tried this, but the mobs still attack the player:

player.addPotionEffect((new PotionEffect(Potion.invisibility.id, 2, 0)));

 

Thanks for helping.

Posted

I'm not sure if this would be the most efficient way, but:

 

1. Setup an entity tick event

2. Check if entity is a mob

3. Check if mob has a target (the player)

4. Remove if it does

Kain

Posted

I'm not sure if this would be the most efficient way, but:

 

1. Setup an entity tick event

2. Check if entity is a mob

3. Check if mob has a target (the player)

4. Remove if it does

 

There is LivingSetAttackTargetEvent.

 

Ok, but, could you give me some steps to do it? code examples?

Posted

Hi

 

This has some useful code

http://www.minecraftforum.net/topic/1419836-131-forge-4x-events-howto/

 

 

Not sure how you would cancel the event, it doesn't seem to be cancelable like normal events, unless I'm missing something.

 

You could perhaps use

 

@ForgeSubscribe
public void entityTarget(LivingSetAttackTargetEvent event)
{
  
  if (event.target != null
     && event.target instanceof EntityPlayer) {
    event.entityLiving.setAttackTarget(null);
  }
}

 

Not sure if that will work since the call to setAttackTarget is reentrant, i.e. will trigger another LivingSetAttackTargetEvent.  Also, setting target to null like this might perhaps cause a crash.

 

And, probably, the mobs will just stand around doing nothing instead of ignoring the player.

 

But it's worth a try :-)

 

-TGG

Posted

What TGG said, though if you check properly the second post to the set living attack target event should process just fine. Btw, checking instanceof automatically null checks for you:

if (event.target instanceof EntityPlayer) {
((EntityLiving) event.entity).setAttackTarget(null);
}

That will make the mobs mostly just stand around doing nothing, even if you attack them. If you want them to fight back, you should add the following check to the check for instanceof EntityPlayer:

&& event.entityLiving.func_94060_bK() != event.target)

Note also that this only works on mobs that are using the AI system - spiders, for example, will still attack you.

Posted

Hi

 

This has some useful code

http://www.minecraftforum.net/topic/1419836-131-forge-4x-events-howto/

 

 

Not sure how you would cancel the event, it doesn't seem to be cancelable like normal events, unless I'm missing something.

 

You could perhaps use

 

@ForgeSubscribe
public void entityTarget(LivingSetAttackTargetEvent event)
{
  
  if (event.target != null
     && event.target instanceof EntityPlayer) {
    event.entityLiving.setAttackTarget(null);
  }
}

 

Not sure if that will work since the call to setAttackTarget is reentrant, i.e. will trigger another LivingSetAttackTargetEvent.  Also, setting target to null like this might perhaps cause a crash.

 

And, probably, the mobs will just stand around doing nothing instead of ignoring the player.

 

But it's worth a try :-)

 

-TGG

 

What TGG said, though if you check properly the second post to the set living attack target event should process just fine. Btw, checking instanceof automatically null checks for you:

if (event.target instanceof EntityPlayer) {
((EntityLiving) event.entity).setAttackTarget(null);
}

That will make the mobs mostly just stand around doing nothing, even if you attack them. If you want them to fight back, you should add the following check to the check for instanceof EntityPlayer:

&& event.entityLiving.func_94060_bK() != event.target)

Note also that this only works on mobs that are using the AI system - spiders, for example, will still attack you.

 

Ok, thanks, it works.

 

Now i have another problem:

 

If the player is firstly invisible, then the mobs don't attack him, but if the mobs are attacking him and the player becomes invisible, the mobs still attack him.

 

This is my code:

/**
 * Only works with AI mobs: Zombies, Skeletons and Creepers.
 * @param event
 */
@SubscribeEvent
public void onEntitySetTarget(LivingSetAttackTargetEvent event)
{
	if (Loader.isModLoaded("SKC-Camouflage"))
	{
		if (event.target != null && event.target instanceof EntityPlayer && event.entityLiving.func_94060_bK() != event.target)
    	{
			if (event.target.isInvisible())
			{
				((EntityLiving) event.entity).setAttackTarget(null);
			}
    	}
	}
    }

Posted

Hi

 

This has some useful code

http://www.minecraftforum.net/topic/1419836-131-forge-4x-events-howto/

 

 

Not sure how you would cancel the event, it doesn't seem to be cancelable like normal events, unless I'm missing something.

 

You could perhaps use

 

@ForgeSubscribe
public void entityTarget(LivingSetAttackTargetEvent event)
{
  
  if (event.target != null
     && event.target instanceof EntityPlayer) {
    event.entityLiving.setAttackTarget(null);
  }
}

 

Not sure if that will work since the call to setAttackTarget is reentrant, i.e. will trigger another LivingSetAttackTargetEvent.  Also, setting target to null like this might perhaps cause a crash.

 

And, probably, the mobs will just stand around doing nothing instead of ignoring the player.

 

But it's worth a try :-)

 

-TGG

 

What TGG said, though if you check properly the second post to the set living attack target event should process just fine. Btw, checking instanceof automatically null checks for you:

if (event.target instanceof EntityPlayer) {
((EntityLiving) event.entity).setAttackTarget(null);
}

That will make the mobs mostly just stand around doing nothing, even if you attack them. If you want them to fight back, you should add the following check to the check for instanceof EntityPlayer:

&& event.entityLiving.func_94060_bK() != event.target)

Note also that this only works on mobs that are using the AI system - spiders, for example, will still attack you.

 

Ok, thanks, it works.

 

Now i have another problem:

 

If the player is firstly invisible, then the mobs don't attack him, but if the mobs are attacking him and the player becomes invisible, the mobs still attack him.

 

This is my code:

/**
 * Only works with AI mobs: Zombies, Skeletons and Creepers.
 * @param event
 */
@SubscribeEvent
public void onEntitySetTarget(LivingSetAttackTargetEvent event)
{
	if (Loader.isModLoaded("SKC-Camouflage"))
	{
		if (event.target != null && event.target instanceof EntityPlayer && event.entityLiving.func_94060_bK() != event.target)
    	{
			if (event.target.isInvisible())
			{
				((EntityLiving) event.entity).setAttackTarget(null);
			}
    	}
	}
    }

That's because the mob already has a target, so it won't set a new. So you'll have to deal with it, or find a solution for it.

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/

Posted

You could subscribe to LivingAttackEvent as well, checking if the player is invisible and getting the source of the source of damage (yes, you read that right), checking if that is an EntityLiving and then setting their current target to null. Obviously that won't work for everything - creepers, for instance - but it should cover most. Otherwise, you could get all of the living entities nearby the player when they turn invisible and set their targets to null at that time, if their current target is the player turning invisible. Probably simpler to just deal with some mobs still attacking.

Posted

You could subscribe to LivingAttackEvent as well, checking if the player is invisible and getting the source of the source of damage (yes, you read that right), checking if that is an EntityLiving and then setting their current target to null. Obviously that won't work for everything - creepers, for instance - but it should cover most. Otherwise, you could get all of the living entities nearby the player when they turn invisible and set their targets to null at that time, if their current target is the player turning invisible. Probably simpler to just deal with some mobs still attacking.

 

Ok, but where's the ".setCurrentTarget()" (or similar) method is located?

 

Posted

Ok, but where's the ".setCurrentTarget()" (or similar) method is located?

You can't be serious... haven't you been paying attention to the thread at all? EntityLiving#setAttackTarget has been featured in probably every single bit of code that has been used in the examples.

 

:'( Sorry for that stupid question, but i've just got up.

 

Ok, did it and the mobs (only tested zombies) don't attack me. But if i touch them then minecraft crashes.

 

[12:56:51] [server thread/ERROR]: Encountered an unexpected exception
net.minecraft.util.ReportedException: Ticking entity
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:745) ~[MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:650) ~[MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120) ~[integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:528) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:787) [MinecraftServer$2.class:?]
Caused by: java.lang.ClassCastException: net.minecraft.entity.player.EntityPlayerMP cannot be cast to net.minecraft.entity.EntityLiving
at com.sackcastellon.core.event.MinecraftForgeEvents.onEntityAttack(MinecraftForgeEvents.java:50) ~[MinecraftForgeEvents.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_6_MinecraftForgeEvents_onEntityAttack_LivingAttackEvent.invoke(.dynamic) ~[?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:51) ~[ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:122) ~[EventBus.class:?]
at net.minecraftforge.common.ForgeHooks.onLivingAttack(ForgeHooks.java:274) ~[ForgeHooks.class:?]
at net.minecraft.entity.player.EntityPlayer.attackEntityFrom(EntityPlayer.java:1132) ~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.attackEntityFrom(EntityPlayerMP.java:564) ~[EntityPlayerMP.class:?]
at net.minecraft.entity.monster.EntityMob.attackEntityAsMob(EntityMob.java:140) ~[EntityMob.class:?]
at net.minecraft.entity.monster.EntityZombie.attackEntityAsMob(EntityZombie.java:333) ~[EntityZombie.class:?]
at net.minecraft.entity.ai.EntityAIAttackOnCollide.updateTask(EntityAIAttackOnCollide.java:175) ~[EntityAIAttackOnCollide.class:?]
at net.minecraft.entity.ai.EntityAITasks.onUpdateTasks(EntityAITasks.java:131) ~[EntityAITasks.class:?]
at net.minecraft.entity.EntityLiving.updateAITasks(EntityLiving.java:633) ~[EntityLiving.class:?]
at net.minecraft.entity.EntityLivingBase.onLivingUpdate(EntityLivingBase.java:2023) ~[EntityLivingBase.class:?]
at net.minecraft.entity.EntityLiving.onLivingUpdate(EntityLiving.java:451) ~[EntityLiving.class:?]
at net.minecraft.entity.monster.EntityMob.onLivingUpdate(EntityMob.java:39) ~[EntityMob.class:?]
at net.minecraft.entity.monster.EntityZombie.onLivingUpdate(EntityZombie.java:236) ~[EntityZombie.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:1856) ~[EntityLivingBase.class:?]
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:270) ~[EntityLiving.class:?]
at net.minecraft.entity.monster.EntityMob.onUpdate(EntityMob.java:47) ~[EntityMob.class:?]
at net.minecraft.entity.monster.EntityZombie.onUpdate(EntityZombie.java:328) ~[EntityZombie.class:?]
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2286) ~[World.class:?]
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:705) ~[WorldServer.class:?]
at net.minecraft.world.World.updateEntity(World.java:2246) ~[World.class:?]
at net.minecraft.world.World.updateEntities(World.java:2096) ~[World.class:?]
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:536) ~[WorldServer.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:739) ~[MinecraftServer.class:?]
... 4 more
[12:56:51] [server thread/ERROR]: This crash report has been saved to: C:\Users\Juanjo hijo\AppData\Roaming\Modding\forge-1.7.2-10.12.0.1024-src\.\crash-reports\crash-2014-03-24_12.56.51-server.txt
[12:56:51] [server thread/INFO]: Stopping server
[12:56:51] [server thread/INFO]: Saving players
---- Minecraft Crash Report ----
// Quite honestly, I wouldn't worry myself about that.

Time: 24/03/14 12:56
Description: Ticking entity

java.lang.ClassCastException: net.minecraft.entity.player.EntityPlayerMP cannot be cast to net.minecraft.entity.EntityLiving
at com.sackcastellon.core.event.MinecraftForgeEvents.onEntityAttack(MinecraftForgeEvents.java:50)
at cpw.mods.fml.common.eventhandler.ASMEventHandler_6_MinecraftForgeEvents_onEntityAttack_LivingAttackEvent.invoke(.dynamic)
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:51)
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:122)
at net.minecraftforge.common.ForgeHooks.onLivingAttack(ForgeHooks.java:274)
at net.minecraft.entity.player.EntityPlayer.attackEntityFrom(EntityPlayer.java:1132)
at net.minecraft.entity.player.EntityPlayerMP.attackEntityFrom(EntityPlayerMP.java:564)
at net.minecraft.entity.monster.EntityMob.attackEntityAsMob(EntityMob.java:140)
at net.minecraft.entity.monster.EntityZombie.attackEntityAsMob(EntityZombie.java:333)
at net.minecraft.entity.ai.EntityAIAttackOnCollide.updateTask(EntityAIAttackOnCollide.java:175)
at net.minecraft.entity.ai.EntityAITasks.onUpdateTasks(EntityAITasks.java:131)
at net.minecraft.entity.EntityLiving.updateAITasks(EntityLiving.java:633)
at net.minecraft.entity.EntityLivingBase.onLivingUpdate(EntityLivingBase.java:2023)
at net.minecraft.entity.EntityLiving.onLivingUpdate(EntityLiving.java:451)
at net.minecraft.entity.monster.EntityMob.onLivingUpdate(EntityMob.java:39)
at net.minecraft.entity.monster.EntityZombie.onLivingUpdate(EntityZombie.java:236)
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:1856)
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:270)
at net.minecraft.entity.monster.EntityMob.onUpdate(EntityMob.java:47)
at net.minecraft.entity.monster.EntityZombie.onUpdate(EntityZombie.java:328)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2286)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:705)
at net.minecraft.world.World.updateEntity(World.java:2246)
at net.minecraft.world.World.updateEntities(World.java:2096)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:536)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:739)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:650)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:528)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:787)


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

-- Head --
Stacktrace:
at com.sackcastellon.core.event.MinecraftForgeEvents.onEntityAttack(MinecraftForgeEvents.java:50)
at cpw.mods.fml.common.eventhandler.ASMEventHandler_6_MinecraftForgeEvents_onEntityAttack_LivingAttackEvent.invoke(.dynamic)
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:51)
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:122)
at net.minecraftforge.common.ForgeHooks.onLivingAttack(ForgeHooks.java:274)
at net.minecraft.entity.player.EntityPlayer.attackEntityFrom(EntityPlayer.java:1132)
at net.minecraft.entity.player.EntityPlayerMP.attackEntityFrom(EntityPlayerMP.java:564)
at net.minecraft.entity.monster.EntityMob.attackEntityAsMob(EntityMob.java:140)
at net.minecraft.entity.monster.EntityZombie.attackEntityAsMob(EntityZombie.java:333)
at net.minecraft.entity.ai.EntityAIAttackOnCollide.updateTask(EntityAIAttackOnCollide.java:175)
at net.minecraft.entity.ai.EntityAITasks.onUpdateTasks(EntityAITasks.java:131)
at net.minecraft.entity.EntityLiving.updateAITasks(EntityLiving.java:633)
at net.minecraft.entity.EntityLivingBase.onLivingUpdate(EntityLivingBase.java:2023)
at net.minecraft.entity.EntityLiving.onLivingUpdate(EntityLiving.java:451)
at net.minecraft.entity.monster.EntityMob.onLivingUpdate(EntityMob.java:39)
at net.minecraft.entity.monster.EntityZombie.onLivingUpdate(EntityZombie.java:236)
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:1856)
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:270)
at net.minecraft.entity.monster.EntityMob.onUpdate(EntityMob.java:47)
at net.minecraft.entity.monster.EntityZombie.onUpdate(EntityZombie.java:328)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2286)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:705)
at net.minecraft.world.World.updateEntity(World.java:2246)

-- Entity being ticked --
Details:
Entity Type: Zombie (net.minecraft.entity.monster.EntityZombie)
Entity ID: 4
Entity Name: Zombie
Entity's Exact location: -1321,83, 56,00, 925,34
Entity's Block location: World: (-1322,56,925), Chunk: (at 6,3,13 in -83,57; contains blocks -1328,0,912 to -1313,255,927), Region: (-3,1; contains chunks -96,32 to -65,63, blocks -1536,0,512 to -1025,255,1023)
Entity's Momentum: 0,00, -0,08, 0,00
Stacktrace:
at net.minecraft.world.World.updateEntities(World.java:2096)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:536)

-- Affected level --
Details:
Level name: New World
All players: 1 total; [EntityPlayerMP['Player4'/6, l='New World', x=-1320,69, y=56,00, z=925,46]]
Chunk stats: ServerChunkCache: 646 Drop: 0
Level seed: 4740230574978669681
Level generator: ID 01 - flat, ver 0. Features enabled: false
Level generator options: 2;7,3x1,52x24;2;
Level spawn location: World: (-1359,4,966), Chunk: (at 1,0,6 in -85,60; contains blocks -1360,0,960 to -1345,255,975), Region: (-3,1; contains chunks -96,32 to -65,63, blocks -1536,0,512 to -1025,255,1023)
Level time: 141224 game time, 13000 day time
Level dimension: 0
Level storage version: 0x04ABD - Anvil
Level weather: Rain time: 80681 (now: false), thunder time: 91378 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true
Stacktrace:
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:739)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:650)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:528)
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:787)

-- System Details --
Details:
Minecraft Version: 1.7.2
Operating System: Windows 7 (amd64) version 6.1
Java Version: 1.7.0_25, Oracle Corporation
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
Memory: 169809664 bytes (161 MB) / 448659456 bytes (427 MB) up to 878051328 bytes (837 MB)
JVM Flags: 0 total; 
AABB Pool Size: 188 (10528 bytes; 0 MB) allocated, 55 (3080 bytes; 0 MB) used
IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
FML: MCP v9.01-pre FML v7.2.116.1024 Minecraft Forge 10.12.0.1024 7 mods loaded, 7 mods active
mcp{8.09} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{7.2.116.1024} [Forge Mod Loader] (forgeSrc-1.7.2-10.12.0.1024.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{10.12.0.1024} [Minecraft Forge] (forgeSrc-1.7.2-10.12.0.1024.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
SKC-Core{1.1.1.0} [sKC Core] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
SKC-BetterWood{1.1.0.0} [better Wood] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
SKC-Camouflage{1.1.0.0} [Camouflage] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
SKC-CraftableHorseArmor{1.1.1.0} [Craftable Horse Armor] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Profiler Position: N/A (disabled)
Vec3 Pool Size: 149 (8344 bytes; 0 MB) allocated, 92 (5152 bytes; 0 MB) used
Player Count: 1 / 8; [EntityPlayerMP['Player4'/6, l='New World', x=-1320,69, y=56,00, z=925,46]]
Type: Integrated Server (map_client.txt)
Is Modded: Definitely; Client brand changed to 'fml,forge'
#@!@# Game crashed! Crash report saved to: #@!@# .\crash-reports\crash-2014-03-24_12.56.51-server.txt
[12:56:51] [Client Shutdown Thread/INFO]: Stopping server
[12:56:51] [Client Shutdown Thread/INFO]: Saving players
AL lib: (EE) alc_cleanup: 1 device not closed

Posted

You don't know how to prevent class cast exceptions? It crashed because whatever code you ran is indiscriminately casting every single entity it comes across as an EntityLiving, when the entity may very well not be one, such as the EntityPlayerMP in your crash log.

 

Solution: instanceof

 

EntityLiving and EntityPlayer are both subclasses of EntityLivingBase, but you cannot cast one to the other unless it is an X instanceof Y (you can cast both to EntityLivingBase, if that's what you need, though.) SO, put an 'if' in there to catch that and use logic.

 

Posted

EntityLiving and EntityPlayer are both subclasses of EntityLivingBase, but you cannot cast one to the other unless it is an X instanceof Y (you can cast both to EntityLivingBase, if that's what you need, though.) SO, put an 'if' in there to catch that and use logic.

That has nothing to do with the topic, he is trying to use a method in EntityLiving, not EntityLivingBase, so he needs to check if the entity in question is an instanceof EntityLiving before casting. Stop confusing people with random comments.

Posted

EntityLiving and EntityPlayer are both subclasses of EntityLivingBase, but you cannot cast one to the other unless it is an X instanceof Y (you can cast both to EntityLivingBase, if that's what you need, though.) SO, put an 'if' in there to catch that and use logic.

That has nothing to do with the topic, he is trying to use a method in EntityLiving, not EntityLivingBase, so he needs to check if the entity in question is an instanceof EntityLiving before casting. Stop confusing people with random comments.

 

I don't find your attitude helpful. Perhaps you could keep your snide comments out of the fora.

Posted

EntityLiving and EntityPlayer are both subclasses of EntityLivingBase, but you cannot cast one to the other unless it is an X instanceof Y (you can cast both to EntityLivingBase, if that's what you need, though.) SO, put an 'if' in there to catch that and use logic.

That has nothing to do with the topic, he is trying to use a method in EntityLiving, not EntityLivingBase, so he needs to check if the entity in question is an instanceof EntityLiving before casting. Stop confusing people with random comments.

I don't find your attitude helpful. Perhaps you could keep your snide comments out of the fora.

How is clarifying a statement that confuses the issue at hand being snide or unhelpful? I've seen you give people some excellent advice on challenging questions in other threads, but there have been several cases where you've been either plain wrong or providing extraneous information that diverts from the main point; in the latter cases, I feel it is indeed helpful to point it out and provide either clarification or correction as needed. When I say "stop confusing people with random statements", I'm referring to the latter; for the former, it is helpful to fact-check before posting replies to make sure the information you provide is accurate. If you consider that snide, well, that's your call.

Posted

EntityLiving and EntityPlayer are both subclasses of EntityLivingBase, but you cannot cast one to the other unless it is an X instanceof Y (you can cast both to EntityLivingBase, if that's what you need, though.) SO, put an 'if' in there to catch that and use logic.

That has nothing to do with the topic, he is trying to use a method in EntityLiving, not EntityLivingBase, so he needs to check if the entity in question is an instanceof EntityLiving before casting. Stop confusing people with random comments.

I don't find your attitude helpful. Perhaps you could keep your snide comments out of the fora.

How is clarifying a statement that confuses the issue at hand being snide or unhelpful? I've seen you give people some excellent advice on challenging questions in other threads, but there have been several cases where you've been either plain wrong or providing extraneous information that diverts from the main point; in the latter cases, I feel it is indeed helpful to point it out and provide either clarification or correction as needed. When I say "stop confusing people with random statements", I'm referring to the latter; for the former, it is helpful to fact-check before posting replies to make sure the information you provide is accurate. If you consider that snide, well, that's your call.

Your remarks were and continue to be out of band for these fora. Your tutorials are nice, but all you do is self-advertise your ability to learn from tutorials. So, when all you have to add is "hey, look at my totorial," you really don't have any place suggesting I change my posting habits to suit you.

Now, please grow the hell up and stop this quibbling. It gains nothing and bores me.

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.