summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/ohmyvanillamc/mixins/rule/reintroduceLlamaItemDuping/PlayerManagerMixin.java
blob: f4f8becec80e79a65402b338458047519493a414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.keuin.ohmyvanillamc.mixins.rule.reintroduceLlamaItemDuping;

import com.keuin.ohmyvanillamc.OmvmSettings;
import net.minecraft.advancement.PlayerAdvancementTracker;
import net.minecraft.entity.Entity;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.PlayerListS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.stat.ServerStatHandler;
import net.minecraft.stat.Stats;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Reintroduce MC-161754 glitch
 */
@Mixin(PlayerManager.class)
public abstract class PlayerManagerMixin {

    @Shadow
    protected abstract void savePlayerData(ServerPlayerEntity player);

    @Shadow
    @Final
    private static Logger LOGGER;

    @Shadow
    @Final
    private List<ServerPlayerEntity> players;

    @Shadow
    @Final
    private MinecraftServer server;

    @Shadow
    @Final
    private Map<UUID, ServerPlayerEntity> playerMap;

    @Shadow
    @Final
    private Map<UUID, ServerStatHandler> statisticsMap;

    @Shadow
    @Final
    private Map<UUID, PlayerAdvancementTracker> advancementTrackers;

    @Shadow
    public abstract void sendToAll(Packet<?> packet);

    /**
     * @reason re-introduce llama item duplicating glitch.
     * @author trueKeuin
     */
    @Overwrite
    public void remove(ServerPlayerEntity player) {
        ServerWorld serverWorld = player.getServerWorld();
        player.incrementStat(Stats.LEAVE_GAME);
        this.savePlayerData(player);
        if (player.hasVehicle()) {
            Entity entity = player.getRootVehicle();
            if (entity.hasPlayerRider()) {
                LOGGER.debug("Removing player mount");
                player.stopRiding();
                serverWorld.removeEntity(entity);
                // entity.removed = true
                entity.removed = !OmvmSettings.reintroduceLlamaItemDuplicating || entity.removed;

                Entity entity2;
                for (Iterator var4 = entity.getPassengersDeep().iterator(); var4.hasNext(); // entity2.removed = true
                     entity2.removed = !OmvmSettings.reintroduceLlamaItemDuplicating || entity2.removed) {
                    entity2 = (Entity) var4.next();
                    serverWorld.removeEntity(entity2);
                }

                serverWorld.getChunk(player.chunkX, player.chunkZ).markDirty();
            }
        }

        player.detach();
        serverWorld.removePlayer(player);
        player.getAdvancementTracker().clearCriteria();
        this.players.remove(player);
        this.server.getBossBarManager().onPlayerDisconnect(player);
        UUID uUID = player.getUuid();
        ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) this.playerMap.get(uUID);
        if (serverPlayerEntity == player) {
            this.playerMap.remove(uUID);
            this.statisticsMap.remove(uUID);
            this.advancementTrackers.remove(uUID);
        }

        this.sendToAll(new PlayerListS2CPacket(PlayerListS2CPacket.Action.REMOVE_PLAYER, player));
    }
}