mod for the islanders smp
1package net.radsteve.islanders.mixin;
2
3import kotlin.Pair;
4import net.minecraft.entity.LivingEntity;
5import net.minecraft.entity.damage.DamageSource;
6import net.minecraft.entity.damage.DamageTypes;
7import net.minecraft.entity.player.PlayerEntity;
8import net.minecraft.entity.player.PlayerInventory;
9import net.minecraft.item.ItemStack;
10import net.minecraft.scoreboard.Scoreboard;
11import net.minecraft.server.world.ServerWorld;
12import net.radsteve.islanders.Islanders;
13import net.radsteve.islanders.ItemEffects;
14import net.radsteve.islanders.SpecialItem;
15import org.spongepowered.asm.mixin.Final;
16import org.spongepowered.asm.mixin.Mixin;
17import org.spongepowered.asm.mixin.Shadow;
18import org.spongepowered.asm.mixin.injection.At;
19import org.spongepowered.asm.mixin.injection.Inject;
20import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
21
22@Mixin(PlayerEntity.class)
23public abstract class PlayerEntityMixin {
24 @Shadow
25 @Final
26 PlayerInventory inventory;
27
28 @Shadow
29 public abstract Scoreboard getScoreboard();
30
31 @Shadow
32 public abstract String getNameForScoreboard();
33
34 @Inject(method = "dropInventory", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;dropAll()V"))
35 private void islanders$dropInventory(ServerWorld world, CallbackInfo ci) {
36 final LivingEntity entity = LivingEntity.class.cast(this);
37 final DamageSource recentDamageSource = entity.getRecentDamageSource();
38
39 if (recentDamageSource != null && entity.getRecentDamageSource().isOf(DamageTypes.OUT_OF_WORLD)) {
40 Islanders.getLogger().info("{} died to the void with special items on them, dropping items", entity.getNameForScoreboard());
41 return;
42 }
43
44 if (entity.getPrimeAdversary() instanceof PlayerEntity killer) {
45 Islanders.getLogger().info("{} died to {} with special items on them, dropping items", entity.getNameForScoreboard(), killer.getNameForScoreboard());
46 return;
47 }
48
49 Islanders.getLogger().info("{} died with special items on them without a killer, giving back on respawn", entity.getNameForScoreboard());
50
51 for (int idx = 0; idx < this.inventory.size(); idx++) {
52 ItemStack stack = this.inventory.getStack(idx);
53 if (SpecialItem.Companion.find(stack) != null) {
54 ItemEffects.INSTANCE.getItemsToRegive().add(new Pair<>(stack, entity.getUuid()));
55 inventory.removeStack(idx);
56 }
57 }
58
59 SpecialItem.getEntries().forEach(item -> {
60 if (item.getTeam().getPlayerList().contains(getNameForScoreboard())) {
61 getScoreboard().removeScoreHolderFromTeam(getNameForScoreboard(), item.getTeam());
62 }
63 });
64 }
65}