Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. package seraphaestus.historicizedmedicine.Effect;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.UUID;
  9.  
  10. import net.minecraft.client.Minecraft;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.EntityLiving;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.potion.Potion;
  17. import net.minecraft.potion.PotionEffect;
  18. import net.minecraft.util.DamageSource;
  19. import net.minecraft.util.FoodStats;
  20. import net.minecraft.util.math.AxisAlignedBB;
  21. import net.minecraft.util.text.TextComponentString;
  22. import net.minecraftforge.event.entity.living.LivingEvent;
  23. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  24. import net.minecraftforge.fml.relauncher.ReflectionHelper;
  25. import seraphaestus.historicizedmedicine.Config;
  26. import seraphaestus.historicizedmedicine.HardCodedValues;
  27. import seraphaestus.historicizedmedicine.Block.RegistryHandler;
  28. import seraphaestus.historicizedmedicine.Item.ItemMask;
  29. import seraphaestus.historicizedmedicine.Mob.PlagueDoctor.EntityPlagueDoctor;
  30. import seraphaestus.historicizedmedicine.Mob.Rat.EntityRat;
  31.  
  32. public class EntityUpdate
  33. {
  34. private static final int plagueParticleEveryXTicks = 6;
  35. private static HashMap<UUID, Integer> currentBleedDur = new HashMap<UUID, Integer>();
  36.  
  37. @SubscribeEvent
  38. public void onEntityUpdate(LivingEvent.LivingUpdateEvent event)
  39. {
  40. if(event.getEntityLiving() instanceof EntityPlayer)
  41. {
  42. EntityPlayer player = (EntityPlayer) event.getEntityLiving();
  43.  
  44. Iterator<ItemStack> armor = player.getArmorInventoryList().iterator();
  45. boolean wearingMask = false;
  46. while(armor.hasNext()) {
  47. if(armor.next().getItem() instanceof ItemMask) {
  48. wearingMask = true;
  49. break;
  50. }
  51. }
  52. if(wearingMask) {
  53. player.addPotionEffect(new PotionEffect(RegisterEffects.plagueImmunity, 10, 0, true, false));
  54. }
  55.  
  56. //nbt tag handling
  57.  
  58. NBTTagCompound nbt = player.getEntityData();
  59. for(String id : RegistryHandler.statues) {
  60. int cooldown = nbt.getInteger(id + "_cooldown");
  61. if(cooldown != 0) {
  62. nbt.setInteger(id + "_cooldown", cooldown - 1);
  63. }
  64. }
  65.  
  66.  
  67. //potion effects implementation:
  68.  
  69. if(player.isPotionActive(RegisterEffects.bleeding) && !player.capabilities.isCreativeMode){
  70. //effect: reduces health by a constant amount, at even intervals over the duration of the effect
  71. PotionEffect pot = player.getActivePotionEffect(RegisterEffects.bleeding);
  72. if(currentBleedDur.get(player.getUniqueID()) == null) {
  73. currentBleedDur.put(player.getUniqueID(), pot.getDuration());
  74. }
  75. Integer bleedTick = currentBleedDur.get(player.getUniqueID());
  76. if(bleedTick != null) {
  77. bleedTick = bleedTick / (Config.bleedTotalAmount * (pot.getAmplifier() + 1));
  78. if(pot.getDuration() % bleedTick == 0) {
  79. //do effect
  80. player.setHealth(player.getHealth() - 1); //setHealth clamps between 0 and maxHealth so no need to verify
  81. }
  82. }
  83. } else {
  84. currentBleedDur.remove(player.getUniqueID());
  85. }
  86.  
  87. if(player.isPotionActive(RegisterEffects.pain) && !player.capabilities.isCreativeMode){
  88. //effect: gives you weakness and slowness 2 while in effect
  89. //PotionEffect pot = player.getActivePotionEffect(RegisterEffects.pain);
  90. //note: this constructor allows you to set the potion effect to be ambient, as in like a beacon effect
  91. player.addPotionEffect(new PotionEffect(Potion.getPotionById(2), 5, 1, true, false)); //slowness 2
  92. player.addPotionEffect(new PotionEffect(Potion.getPotionById(18), 5, 0, true, false)); //weakness
  93.  
  94. }
  95.  
  96. if(player.isPotionActive(RegisterEffects.infection) && !player.capabilities.isCreativeMode){
  97. //effect: disable natural health regeneration while the effect is active
  98. //does not work on peaceful difficulty
  99. //PotionEffect pot = player.getActivePotionEffect(RegisterEffects.infection);
  100. //effect here
  101. resetFoodTimer(player.getFoodStats());
  102. }
  103.  
  104. if(player.isPotionActive(RegisterEffects.plague) && !player.capabilities.isCreativeMode){
  105. //spreading plague
  106. for(Entity entity : player.getEntityWorld().loadedEntityList) {
  107. if(HardCodedValues.catchesPlague(entity) && entity instanceof EntityLiving) {
  108. if(!((EntityLiving) entity).isPotionActive(RegisterEffects.plague) && !((EntityLiving) entity).isPotionActive(RegisterEffects.plagueImmunity)) {
  109. if(player.getDistanceToEntity(entity) <= Config.plagueRange){
  110. ((EntityLiving) entity).addPotionEffect(new PotionEffect(RegisterEffects.plague, Config.plagueDuration));
  111. if(entity.hasCustomName() && !player.getEntityWorld().isRemote) {
  112. TextComponentString message = new TextComponentString(entity.getCustomNameTag() + " has caught " + Config.plagueName);
  113. List<EntityPlayer> players = player.getEntityWorld().playerEntities;
  114. for(EntityPlayer p : players) {
  115. p.sendMessage(message);
  116. }
  117. }
  118. }
  119. }
  120. }
  121.  
  122. }
  123.  
  124. }
  125.  
  126. } else {
  127. //not instance of player
  128. EntityLiving entity = (EntityLiving) event.getEntityLiving();
  129. if(entity.isPotionActive(RegisterEffects.plague) && Config.enablePlague) {
  130. int plagueDuration = entity.getActivePotionEffect(RegisterEffects.plague).getDuration();
  131. if(entity instanceof EntityPlagueDoctor) {
  132. entity.removeActivePotionEffect(RegisterEffects.plague);
  133. }
  134. if(plagueDuration != 0) {
  135. if(plagueDuration == 1 && !(entity instanceof EntityRat)) {
  136. //death
  137. entity.attackEntityFrom(new DamageSource("HMedPlague"), entity.getHealth() + 1);
  138. if(entity.hasCustomName() && !entity.getEntityWorld().isRemote) {
  139. TextComponentString message = new TextComponentString(entity.getCustomNameTag() + " succumbed to " + Config.plagueName);
  140. List<EntityPlayer> players = entity.getEntityWorld().playerEntities;
  141. for(EntityPlayer p : players) {
  142. p.sendMessage(message);
  143. }
  144. }
  145. }
  146. Random rnd = entity.getRNG();
  147. if(plagueDuration % plagueParticleEveryXTicks == 0) {
  148. AxisAlignedBB box = entity.getEntityBoundingBox();
  149. Minecraft.getMinecraft().effectRenderer.addEffect(new PlagueEffect(entity.getEntityWorld(), entity.posX + (box.maxX - box.minX) * (rnd.nextFloat() - 0.5f), entity.posY + (box.maxY - box.minY) * (rnd.nextFloat()), entity.posZ + (box.maxZ - box.minZ) * (rnd.nextFloat() - 0.5f), 0.0f, 0.0f, 0.0f));
  150. }
  151. }
  152. }
  153. }
  154. }
  155.  
  156. private static Field foodTimerField = null;
  157. private void resetFoodTimer(FoodStats foodStats) {
  158. if (foodTimerField == null)
  159. foodTimerField = ReflectionHelper.findField(FoodStats.class, "field_75123_d", "foodTimer");
  160. if (foodStats.getFoodLevel() > 0) {
  161. try { foodTimerField.set(foodStats, 0); }
  162. catch (Exception ex) { throw new RuntimeException(ex); }
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement