aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java
blob: 41755d6abc78343ce780d64670a477e0b770f5de (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
package gtPlusPlus.core.handler.events;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import gregtech.mixin.interfaces.accessors.PotionAccessor;
import gtPlusPlus.core.util.math.MathUtils;

public class PlayerSleepEventHandler {

    private static final ArrayList<Potion> potionBuffs = new ArrayList<>();

    public static void init() {
        MinecraftForge.EVENT_BUS.register(new PlayerSleepEventHandler());
        potionBuffs.add(Potion.moveSpeed);
        potionBuffs.add(Potion.waterBreathing);
        potionBuffs.add(Potion.resistance);
        potionBuffs.add(Potion.regeneration);
        potionBuffs.add(Potion.damageBoost);
        potionBuffs.add(Potion.digSpeed);
        potionBuffs.add(Potion.fireResistance);
        potionBuffs.add(Potion.field_76434_w); // Health Boost
        potionBuffs.add(Potion.field_76444_x); // Absorption
        potionBuffs.trimToSize();
    }

    @SubscribeEvent
    public void onPlayerWakeUp(PlayerWakeUpEvent event) {
        EntityPlayer player = event.entityPlayer;
        if (player == null || player.worldObj.isRemote) return;
        if (player.getEntityWorld()
            .getWorldTime() % 24000 != 0) {
            return;
        }
        final List<Integer> potionToRemove = new ArrayList<>();
        for (PotionEffect potionEffect : player.getActivePotionEffects()) {
            final Potion potion = Potion.potionTypes[potionEffect.getPotionID()];
            if (potion instanceof PotionAccessor && ((PotionAccessor) potion).gt5u$isBadEffect()) {
                potionToRemove.add(potion.id);
            }
        }
        for (Integer i : potionToRemove) {
            player.removePotionEffect(i);
        }
        if (!potionToRemove.isEmpty()) {
            messagePlayer(player, "sleep.event.downsides");
            return;
        }
        // Try Heal
        float currentHP = player.getHealth();
        float maxHP = player.getMaxHealth();
        if (currentHP < maxHP) {
            float missingHP = maxHP - currentHP;
            float heal = MathUtils.randFloat(1, missingHP);
            player.heal(heal);
            messagePlayer(player, (heal >= missingHP / 2 ? "sleep.event.good" : "sleep.event.okay"));
            return;
        }
        // Try give a buff
        Potion aPotionToApply = potionBuffs.get(MathUtils.randInt(0, potionBuffs.size() - 1));
        player.addPotionEffect(
            new PotionEffect(aPotionToApply.id, MathUtils.randInt(60, 180) * 20, MathUtils.randInt(0, 2)));
        messagePlayer(player, "sleep.event.wellrested");
    }

    private static void messagePlayer(EntityPlayer player, String aChatKey) {
        player.addChatComponentMessage(new ChatComponentTranslation(aChatKey));
    }
}