Made it so neutral mobs can kill you and fixed gravity when knocked.

This commit is contained in:
IM23a-spirgif
2025-07-23 23:43:56 +02:00
parent 843ffc4ae6
commit bd62a201f9
4 changed files with 29 additions and 6 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ mod_name=Knockedback
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT
# The mod version. See https://semver.org/
mod_version=1.1.0
mod_version=1.2.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
@@ -5,6 +5,7 @@ import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@@ -28,6 +29,20 @@ public class KnockedManager {
}
}
@SubscribeEvent
public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
if (!(event.player instanceof ServerPlayer sp)) return;
if (KnockedManager.isKnocked(sp)) {
boolean grounded = sp.onGround() || sp.isInWater();
sp.setNoGravity(false); // Always allow gravity
if (!grounded) {
sp.setDeltaMovement(0, sp.getDeltaMovement().y() - 0.08, 0); // Gravity acceleration
}
}
}
public static boolean isKnocked(LivingEntity entity) {
return knockedEntities.containsKey(entity.getUUID());
@@ -144,17 +144,25 @@ public class MobKillHandler {
p.getX() + r, p.getY() + r, p.getZ() + r
);
return world.getEntitiesOfClass(Mob.class, box).stream()
.filter(m -> isHostile(m) || isAggressiveNeutral(m))
.filter(m -> isHostile(m) || isAggressiveNeutral(m, p))
.findAny().orElse(null);
}
private boolean isHostile(Mob m) {
return m instanceof net.minecraft.world.entity.monster.Monster;
}
private boolean isAggressiveNeutral(Mob m) {
if (m instanceof net.minecraft.world.entity.animal.Wolf w) return w.isAngry();
if (m instanceof net.minecraft.world.entity.monster.EnderMan e) return e.isCreepy();
private boolean isAggressiveNeutral(Mob mob, Player target) {
if (mob instanceof net.minecraft.world.entity.animal.Wolf wolf) {
return wolf.isAngry() && wolf.getTarget() == target;
}
if (mob instanceof net.minecraft.world.entity.monster.EnderMan enderman) {
return enderman.isCreepy() && enderman.getTarget() == target;
}
if (mob instanceof net.minecraft.world.entity.animal.IronGolem golem) {
return golem.getTarget() == target;
}
return false;
}
@@ -16,10 +16,10 @@ public class KnockedEffect {
public static void onPlayerTick(LivingEvent.LivingTickEvent event) {
if (!(event.getEntity() instanceof Player player)) return;
if (KnockedManager.isKnocked(player)) {
player.setDeltaMovement(0, 0, 0);
player.setSprinting(false);
player.hurtMarked = true;
player.setNoGravity(false);
player.setDeltaMovement(0, player.getDeltaMovement().y(), 0);
}
}