Skip to content

Commit

Permalink
Change sleep potion buff code (GTNewHorizons#3076)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
Co-authored-by: boubou19 <[email protected]>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent 07a668f commit 4552bd7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 102 deletions.
5 changes: 3 additions & 2 deletions src/main/java/gregtech/mixin/Mixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public enum Mixin {
.setApplyIf(() -> ConfigHandler.enabledPatches[3])
.setPhase(Phase.EARLY)
.setSide(Side.BOTH)),
CraftingRecipeAccessorMixin(new Builder("Add accessors to crafting recipe types")
VanillaAccessors(new Builder("Adds various accessors")
.addMixinClasses(
"minecraft.VanillaShapedRecipeMixin",
"minecraft.VanillaShapelessRecipeMixin",
"minecraft.ForgeShapedRecipeMixin",
"minecraft.ForgeShapelessRecipeMixin")
"minecraft.ForgeShapelessRecipeMixin",
"minecraft.PotionMixin")
.addTargetedMod(VANILLA)
.setApplyIf(() -> true)
.setPhase(Phase.EARLY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kubatech.api;
package gregtech.mixin.interfaces;

import net.minecraft.block.Block;

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/gregtech/mixin/interfaces/PotionExt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gregtech.mixin.interfaces;

public interface PotionExt {

boolean gt5u$isBadEffect();

}
6 changes: 2 additions & 4 deletions src/main/java/gtPlusPlus/core/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ public CommonProxy() {

public void preInit(final FMLPreInitializationEvent e) {
Logger.INFO("Doing some house cleaning.");
if (!PreloaderCore.DEBUG_MODE) {
Logger.WARNING("Development mode not enabled.");
} else if (PreloaderCore.DEBUG_MODE) {
if (PreloaderCore.DEBUG_MODE) {
Logger.INFO("Development mode enabled.");
} else {
Logger.WARNING("Development mode not set.");
Logger.WARNING("Development mode not enabled.");
}

AddToCreativeTab.initialiseTabs();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,109 +1,77 @@
package gtPlusPlus.core.handler.events;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
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.event.entity.player.PlayerSleepInBedEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.potion.GtPotionEffect;
import gtPlusPlus.core.util.Utils;
import gregtech.mixin.interfaces.PotionExt;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.PlayerUtils;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import gtPlusPlus.preloader.PreloaderCore;

public class PlayerSleepEventHandler {

private static Field sEffectDuration = ReflectionUtils
.getField(PotionEffect.class, !PreloaderCore.DEV_ENVIRONMENT ? "field_76460_b" : "duration");
private static ArrayList<Potion> sPositiveEffects = new ArrayList<>();
private static ArrayList<Potion> sNegativeEffects = new ArrayList<>();
private static final ArrayList<Potion> potionBuffs = new ArrayList<>();

public static void init() {
Utils.registerEvent(new PlayerSleepEventHandler());
sPositiveEffects.add(Potion.moveSpeed);
sPositiveEffects.add(Potion.waterBreathing);
sPositiveEffects.add(Potion.resistance);
sPositiveEffects.add(Potion.regeneration);
sPositiveEffects.add(Potion.damageBoost);
sPositiveEffects.add(Potion.digSpeed);
sPositiveEffects.add(Potion.fireResistance);
sPositiveEffects.add(Potion.field_76434_w); // Health Boost
sPositiveEffects.add(Potion.field_76444_x); // Absorption
sNegativeEffects.add(Potion.blindness);
sNegativeEffects.add(Potion.confusion);
sNegativeEffects.add(Potion.digSlowdown);
sNegativeEffects.add(Potion.harm);
sNegativeEffects.add(Potion.hunger);
sNegativeEffects.add(Potion.moveSlowdown);
sNegativeEffects.add(Potion.poison);
sNegativeEffects.add(Potion.weakness);
sNegativeEffects.add(Potion.wither);
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 sleep(PlayerSleepInBedEvent event) {}

@SubscribeEvent
public void wake(PlayerWakeUpEvent event) {
EntityPlayer aPlayer = event.entityPlayer;
if (aPlayer != null && !aPlayer.worldObj.isRemote) {
if (event.entityPlayer.getEntityWorld()
.getWorldTime() % 24000 != 0) {
return;
}
boolean aRemovedBad = false;
try {
Collection<PotionEffect> aActive = aPlayer.getActivePotionEffects();
for (PotionEffect aEffect : aActive) {
for (Potion aBadPotion : sNegativeEffects) {
if (aEffect.getPotionID() == aBadPotion.getId()) {
ReflectionUtils.setField(aEffect, sEffectDuration, 1);
aRemovedBad = true;
Logger.INFO("Set duration of " + aEffect.getEffectName() + " to 1 tick");
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
if (aRemovedBad) {
messagePlayer(aPlayer, "sleep.event.downsides");
} else {
// Try Heal
float aCurrentHP = aPlayer.getHealth();
float aMaxHP = aPlayer.getMaxHealth();
if (aCurrentHP < aMaxHP) {
float aDamage = aMaxHP - aCurrentHP;
float aToHeal = MathUtils.randFloat(1, aDamage);
if (aToHeal > 0) {
aPlayer.heal(aToHeal);
messagePlayer(aPlayer, (aToHeal >= aDamage / 2 ? "sleep.event.good" : "sleep.event.okay"));
}
}
// Already healed, try give a buff
else {
int aRandomBuff = MathUtils.randInt(0, sPositiveEffects.size() - 1);
Potion aPotionToApply = sPositiveEffects.get(aRandomBuff);
if (aPotionToApply != null) {
aPlayer.addPotionEffect(
new GtPotionEffect(aPotionToApply.id, MathUtils.randInt(60, 180), MathUtils.randInt(0, 2)));
messagePlayer(aPlayer, "sleep.event.wellrested");
}
}
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 PotionExt && ((PotionExt) 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 aPlayer, String aChatKey) {
PlayerUtils.messagePlayer(aPlayer, new ChatComponentTranslation(aChatKey, new Object[0]));
private static void messagePlayer(EntityPlayer player, String aChatKey) {
player.addChatComponentMessage(new ChatComponentTranslation(aChatKey));
}
}
10 changes: 0 additions & 10 deletions src/main/java/gtPlusPlus/core/potion/GtPotionEffect.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.IPlantable;

import kubatech.api.IBlockStemAccesor;
import gregtech.mixin.interfaces.IBlockStemAccesor;
import kubatech.api.eig.EIGBucket;
import kubatech.api.eig.EIGDropTable;
import kubatech.api.eig.IEIGBucketFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import kubatech.api.IBlockStemAccesor;
import gregtech.mixin.interfaces.IBlockStemAccesor;

@Mixin(value = BlockStem.class)
public class BlockStemMixin implements IBlockStemAccesor {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gregtech.mixin.mixins.early.minecraft;

import net.minecraft.potion.Potion;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import gregtech.mixin.interfaces.PotionExt;

@Mixin(Potion.class)
public class PotionMixin implements PotionExt {

@Shadow
@Final
private boolean isBadEffect;

@Override
public boolean gt5u$isBadEffect() {
return this.isBadEffect;
}

}

0 comments on commit 4552bd7

Please sign in to comment.