forked from GTNewHorizons/GT5-Unofficial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change sleep potion buff code (GTNewHorizons#3076)
Co-authored-by: Martin Robertz <[email protected]> Co-authored-by: boubou19 <[email protected]>
- Loading branch information
1 parent
07a668f
commit 4552bd7
Showing
9 changed files
with
89 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../java/kubatech/api/IBlockStemAccesor.java → ...h/mixin/interfaces/IBlockStemAccesor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package gregtech.mixin.interfaces; | ||
|
||
public interface PotionExt { | ||
|
||
boolean gt5u$isBadEffect(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 51 additions & 83 deletions
134
src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/mixin/java/gregtech/mixin/mixins/early/minecraft/PotionMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |