Skip to content

Commit

Permalink
Fix a NullPointerException being thrown when a player is damaged by a…
Browse files Browse the repository at this point in the history
…n eliminated player
  • Loading branch information
haykam821 committed Feb 4, 2024
1 parent c177083 commit 09f4b3b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.item.Items;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionUtil;
import net.minecraft.registry.tag.DamageTypeTags;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.HoverEvent;
Expand Down Expand Up @@ -263,6 +264,10 @@ public ActionResult onBreakBlock(BlockPos pos) {
}

public ActionResult onDamaged(PlayerEntry target, DamageSource source, float amount) {
if (source.isIn(DamageTypeTags.IS_FIRE) && !this.isDamagedByFire()) {
return ActionResult.FAIL;
}

return ActionResult.PASS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.github.haykam821.microbattle.game.win.WinManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.item.BlockItem;
Expand Down Expand Up @@ -205,23 +206,17 @@ private void tick() {
}

private SoundEvent playDeathSound(LivingEntity entity, SoundEvent defaultSound) {
if (entity instanceof ServerPlayerEntity) {
PlayerEntry entry = this.getEntryFromPlayer((ServerPlayerEntity) entity);
if (entry != null) {
return entry.getKit().getDeathSound();
}
}
return null;
PlayerEntry entry = this.getEntryFromEntity(entity);
if (entry == null) return null;

return entry.getKit().getDeathSound();
}

private SoundEvent playHurtSound(LivingEntity entity, DamageSource source, SoundEvent defaultSound) {
if (entity instanceof ServerPlayerEntity) {
PlayerEntry entry = this.getEntryFromPlayer((ServerPlayerEntity) entity);
if (entry != null) {
return entry.getKit().getHurtSound(source);
}
}
return null;
PlayerEntry entry = this.getEntryFromEntity(entity);
if (entry == null) return null;

return entry.getKit().getHurtSound(source);
}

public GameSpace getGameSpace() {
Expand Down Expand Up @@ -287,6 +282,14 @@ private PlayerEntry getEntryFromPlayer(ServerPlayerEntity player) {
return null;
}

private PlayerEntry getEntryFromEntity(Entity entity) {
if (entity instanceof ServerPlayerEntity player) {
return this.getEntryFromPlayer(player);
}

return null;
}

private ActionResult onUseBlock(ServerPlayerEntity player, Hand hand, BlockHitResult hitResult) {
PlayerEntry entry = this.getEntryFromPlayer(player);
if (entry != null) {
Expand All @@ -302,11 +305,9 @@ private ActionResult onPlayerDeath(ServerPlayerEntity player, DamageSource sourc
ActionResult kitResult = this.applyToKit(entry, kit -> kit.onDeath(source));
if (kitResult != ActionResult.PASS) return kitResult;

if (source.getAttacker() instanceof ServerPlayerEntity) {
PlayerEntry killer = this.getEntryFromPlayer((ServerPlayerEntity) source.getAttacker());
ActionResult killerKitResult = this.applyToKit(killer, kit -> kit.onKilledPlayer(entry, source));
if (killerKitResult != ActionResult.PASS) return killerKitResult;
}
PlayerEntry killer = this.getEntryFromEntity(source.getAttacker());
ActionResult killerKitResult = this.applyToKit(killer, kit -> kit.onKilledPlayer(entry, source));
if (killerKitResult != ActionResult.PASS) return killerKitResult;

if (entry == null) {
MicroBattleActivePhase.spawn(this.world, this.map, player);
Expand Down Expand Up @@ -372,21 +373,21 @@ private ActionResult onBreakBlock(ServerPlayerEntity player, ServerWorld world,
private ActionResult onPlayerDamage(ServerPlayerEntity player, DamageSource source, float amount) {
PlayerEntry target = this.getEntryFromPlayer(player);
if (target == null) return ActionResult.PASS;
if (source.isIn(DamageTypeTags.IS_FIRE) && target.getKit() != null && !target.getKit().isDamagedByFire()) {
return ActionResult.FAIL;
}

if (!(source.getAttacker() instanceof ServerPlayerEntity)) return ActionResult.PASS;
PlayerEntry attacker = this.getEntryFromPlayer((ServerPlayerEntity) source.getAttacker());
PlayerEntry attacker = this.getEntryFromEntity(source.getAttacker());

// Prevent attacks from teammates
if (attacker != null && target.isSameTeam(attacker)) {
return ActionResult.FAIL;
}

// Dispatch handling to kits
if (target.getKit() != null) {
ActionResult damagedResult = target.getKit().onDamaged(attacker, source, amount);
if (damagedResult != ActionResult.PASS) return damagedResult;
ActionResult result = target.getKit().onDamaged(attacker, source, amount);
if (result != ActionResult.PASS) return result;
}
if (attacker.getKit() != null) {

if (attacker != null && attacker.getKit() != null) {
return attacker.getKit().onDealDamage(target, source, amount);
}

Expand Down

0 comments on commit 09f4b3b

Please sign in to comment.