diff options
| author | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2022-01-29 13:07:43 +0000 |
|---|---|---|
| committer | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2022-01-29 13:07:43 +0000 |
| commit | 143a76dd8b380f85dad4fee6359b5a17022feccb (patch) | |
| tree | 53d6fef54ffd249a5b1d7f6a585ef590cac2e761 /src/main/java/gtPlusPlus/core/handler | |
| parent | 9a25f7d7141def59ba11317a8ac4e3b2f72f24fd (diff) | |
| download | GT5-Unofficial-143a76dd8b380f85dad4fee6359b5a17022feccb.tar.gz GT5-Unofficial-143a76dd8b380f85dad4fee6359b5a17022feccb.tar.bz2 GT5-Unofficial-143a76dd8b380f85dad4fee6359b5a17022feccb.zip | |
Added Mob Mentality.
Added perks to sleeping.
Added Magic Feather.
Diffstat (limited to 'src/main/java/gtPlusPlus/core/handler')
| -rw-r--r-- | src/main/java/gtPlusPlus/core/handler/MobMentality.java | 58 | ||||
| -rw-r--r-- | src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java | 124 |
2 files changed, 182 insertions, 0 deletions
diff --git a/src/main/java/gtPlusPlus/core/handler/MobMentality.java b/src/main/java/gtPlusPlus/core/handler/MobMentality.java new file mode 100644 index 0000000000..acc289fecd --- /dev/null +++ b/src/main/java/gtPlusPlus/core/handler/MobMentality.java @@ -0,0 +1,58 @@ +package gtPlusPlus.core.handler; + +import java.util.*; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import gtPlusPlus.core.util.minecraft.EntityUtils; +import gtPlusPlus.core.util.minecraft.PlayerUtils; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.monster.IMob; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.event.entity.living.LivingHurtEvent; + +public class MobMentality { + + public static HashSet<Class<EntityLivingBase>> sIgnoredTypes = new HashSet<Class<EntityLivingBase>>(); + + @SubscribeEvent + public void onEntityDamaged(LivingHurtEvent event) { + final EntityLivingBase target = event.entityLiving; + for (Class<EntityLivingBase> aEntityClass : sIgnoredTypes) { + if (aEntityClass.isInstance(target)) { + return; + } + } + if (target instanceof EntityLivingBase) { + final EntityLivingBase entity = target; + final Entity attacker = event.source.getSourceOfDamage(); + if (/*this.configuration.shouldIgnoreNeutralMobs() && */!(entity instanceof IMob)) { + return; + } + if (attacker == null) { + return; + } + if (attacker instanceof EntityLivingBase && !PlayerUtils.isRealPlayer((EntityLivingBase) attacker)) { + return; + } + if (attacker instanceof EntityPlayer && PlayerUtils.isCreative((EntityPlayer) attacker)) { + return; + } + if (attacker instanceof EntityLivingBase) { + List<Entity> aEntityList = target.worldObj.loadedEntityList; + List<EntityLivingBase> aRangedEntity = new ArrayList<EntityLivingBase>(); + for (Entity aEntity : aEntityList) { + if (target.getClass().isInstance(aEntity)) { + if (EntityUtils.getDistance(target, aEntity) <= 32) { + aRangedEntity.add((EntityLivingBase) aEntity); + } + } + } + for (EntityLivingBase aEntity : aRangedEntity) { + aEntity.setRevengeTarget((EntityLivingBase) attacker); + } + } + } + } + +} diff --git a/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java b/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java new file mode 100644 index 0000000000..d3700e1373 --- /dev/null +++ b/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java @@ -0,0 +1,124 @@ +package gtPlusPlus.core.handler.events; + +import cpw.mods.fml.common.eventhandler.Event.Result; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.*; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.core.potion.GtPotionEffect; +import gtPlusPlus.core.util.Utils; +import gtPlusPlus.core.util.math.MathUtils; +import gtPlusPlus.core.util.minecraft.PlayerUtils; +import gtPlusPlus.core.util.reflect.ReflectionUtils; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; +import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent; +import net.minecraftforge.event.entity.player.PlayerWakeUpEvent; + +public class PlayerSleepEventHandler { + + private static ArrayList<Potion> sPositiveEffects = new ArrayList<Potion>(); + private static ArrayList<Potion> sNegativeEffects = new ArrayList<Potion>(); + + 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); + } + + @SubscribeEvent + public void sleep(PlayerSleepInBedEvent event) { + event.setResult(Result.ALLOW); + } + + @SubscribeEvent + public void wake(PlayerWakeUpEvent event) { + EntityPlayer aPlayer = event.entityPlayer; + if (aPlayer != null && !aPlayer.worldObj.isRemote) { + // 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); + PlayerUtils.messagePlayer(aPlayer, "You slept well and now feel " + (aToHeal >= aDamage / 2 ? "much" : "a little") + " better."); + } + } + // 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))); + PlayerUtils.messagePlayer(aPlayer, "You feel really well rested."); + } + } + boolean aRemovedBad = false; + /*for (Potion aBadPotion : sNegativeEffects) { + if (curePotionEffect(aPlayer, aBadPotion)) { + aRemovedBad = true; + } + }*/ + if (aRemovedBad) { + PlayerUtils.messagePlayer(aPlayer, "The downsides of life no longer effect you."); + } + } + } + + private static Field sActivePotionEffects = ReflectionUtils.getField(EntityPlayer.class, "activePotionsMap"); + private static Method sOnFinishedPotionEffect = ReflectionUtils.getMethod(EntityPlayer.class, "onFinishedPotionEffect", PotionEffect.class); + + public boolean curePotionEffect(EntityPlayer aPlayer, Potion aPotion) { + HashMap activePotionsMap = new HashMap(); + try { + activePotionsMap = ReflectionUtils.getFieldValue(sActivePotionEffects, aPlayer); + } + catch (Throwable t) { + t.printStackTrace(); + } + if (aPlayer != null && aPotion != null && activePotionsMap != null && activePotionsMap.size() > 0) { + Iterator<Integer> potionKey = activePotionsMap.keySet().iterator(); + if (!aPlayer.worldObj.isRemote) { + while (potionKey.hasNext()) { + try { + Integer key = potionKey.next(); + PotionEffect effect = (PotionEffect) activePotionsMap.get(key); + if (effect.getPotionID() == aPotion.getId()) { + potionKey.remove(); + ReflectionUtils.invokeVoid(aPlayer, sOnFinishedPotionEffect, new Object[]{effect}); + return true; + } + } + catch (ConcurrentModificationException e) { + e.printStackTrace(); + } + } + } + } + return false; + } + +} |
