Advertisement
Guest User

Untitled

a guest
Sep 30th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. package com.halestormxv.mysterium.tileentity;
  2.  
  3. import com.halestormxv.mysterium.MysteriumMain;
  4. import com.halestormxv.mysterium.init.ModTileEntityTypes;
  5. import com.halestormxv.mysterium.init.PotionInit;
  6. import net.minecraft.entity.LivingEntity;
  7. import net.minecraft.entity.player.PlayerEntity;
  8. import net.minecraft.entity.player.ServerPlayerEntity;
  9. import net.minecraft.nbt.*;
  10. import net.minecraft.network.play.server.SChangeGameStatePacket;
  11. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  12. import net.minecraft.potion.Effect;
  13. import net.minecraft.potion.EffectInstance;
  14. import net.minecraft.tileentity.ITickableTileEntity;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraft.tileentity.TileEntityType;
  17. import net.minecraft.util.math.AxisAlignedBB;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.util.math.Vec3d;
  20. import net.minecraft.world.server.ServerWorld;
  21.  
  22. import javax.annotation.Nullable;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Random;
  26.  
  27.  
  28. public class StructureCoreTileEntity extends TileEntity implements ITickableTileEntity {
  29.  
  30.     private AxisAlignedBB containmentField;
  31.  
  32.     public StructureCoreTileEntity(TileEntityType<?> typeIn) {
  33.         super(typeIn);
  34.     }
  35.  
  36.     public StructureCoreTileEntity() {
  37.         this(ModTileEntityTypes.STRUCTURE_CORE_TE.get());
  38.     }
  39.  
  40.     @Override
  41.     public void setPos(BlockPos posIn) {
  42.         super.setPos(posIn);
  43.         initContainmentField(posIn);
  44.     }
  45.  
  46.     private void initContainmentField(BlockPos pos) {
  47.         //double r = 16;
  48.         //this.containmentField = new AxisAlignedBB(-r, -r, -r, r, r, r);//.offset(getCentre(pos));
  49.         this.containmentField = getBoundsAround(pos, 16);
  50.     }
  51.  
  52.     @Override
  53.     public void tick() {
  54.         if (this.world != null && !this.world.isRemote) {
  55.             //ServerWorld serverWorld = (ServerWorld) world;
  56.             //this.afflictNearbyTargets();
  57.             List<LivingEntity> entities = world.getEntitiesWithinAABB(LivingEntity.class, containmentField, e -> e instanceof PlayerEntity);
  58.             for(LivingEntity entity : entities) {
  59.                 entity.addPotionEffect(new EffectInstance(PotionInit.WATCHFUL_EYE_EFFECT.get(), 200, 1));
  60.                 MysteriumMain.LOGGER.info("I am being called.");
  61.                 boolean soundChance = new Random().nextInt(10) == 0;
  62.                 if (soundChance) {
  63.                     int soundSelection = getRandomNumberInRange(1, 4);
  64.                     if (soundSelection == 1) {
  65.                         //serverWorld.playSound((double) pos.getX() + 0.5D, pos.getY(), (double) pos.getZ() + 0.5D, SoundInit.CREEPY_HUM.get(), SoundCategory.BLOCKS, 1.5F, 1.0F, true);
  66.                     }
  67.                     if (soundSelection == 2) {
  68.                         //serverWorld.playSound((double) pos.getX() + 0.5D, pos.getY(), (double) pos.getZ() + 0.5D, SoundInit.CREEPY_LALA.get(), SoundCategory.BLOCKS, 1.5F, 1.0F, true);
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     //DISABLED BECAUSE IT WORKS BUT THEN NEVER REMOVES THE DEBUFF - FIND THE PROPER WAY TO DO IT WITH THE getEntitiesWithinAABB\\\\\\\\\\\\\\
  76.     private void afflictNearbyTargets() {
  77.         Effect effect = PotionInit.WATCHFUL_EYE_EFFECT.get();
  78.         List<ServerPlayerEntity> list = ((ServerWorld) this.world).getPlayers((playersInList) -> this.getDistanceSq(this.pos.getX(), this.pos.getY(), this.pos.getZ()) < 1250.0D && playersInList.interactionManager.survivalOrAdventure());
  79.         Iterator var7 = list.iterator();
  80.         label28:
  81.         while (true) {
  82.             ServerPlayerEntity serverplayerentity;
  83.             do {
  84.                 if (!var7.hasNext()) {
  85.                     break label28;
  86.                 }
  87.                 serverplayerentity = (ServerPlayerEntity) var7.next();
  88.             } while (serverplayerentity.isPotionActive(effect) && serverplayerentity.getActivePotionEffect(effect).getAmplifier() >= 2 && serverplayerentity.getActivePotionEffect(effect).getDuration() >= 800);
  89.  
  90.             serverplayerentity.connection.sendPacket(new SChangeGameStatePacket(10, 0.0F));
  91.             serverplayerentity.addPotionEffect(new EffectInstance(effect, 1400, 1));
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     @Nullable
  97.     public SUpdateTileEntityPacket getUpdatePacket() {
  98.         CompoundNBT nbtTagCompound = new CompoundNBT();
  99.         write(nbtTagCompound);
  100.         int tileEntityType = 42;
  101.         return new SUpdateTileEntityPacket(this.pos, tileEntityType, nbtTagCompound);
  102.     }
  103.  
  104.     //REFERENCE CODE\\
  105.     /*private void afflictNearbyTargets() {
  106.         List<LivingEntity> entities = world.getEntitiesWithinAABB(LivingEntity.class, containmentField, e -> e instanceof PlayerEntity);
  107.         for(LivingEntity entity : entities){
  108.             entity.addPotionEffect(new EffectInstance(PotionInit.WATCHFUL_EYE_EFFECT.get(), 200, 1));
  109.             MysteriumMain.LOGGER.info("I am being called.");
  110.         }
  111.     }*/
  112.  
  113.     //---------------------------UTILITIES TO MOVE TO SEPARATE CLASS EVENTUALLY----------------------------------\\
  114.     public static AxisAlignedBB getBoundsAround(Vec3d pos, double range) {
  115.         return getBoundsAround(pos.x, pos.y, pos.z, range);
  116.     }
  117.  
  118.     public static AxisAlignedBB getBoundsAround(BlockPos pos, int range) {
  119.         return getBoundsAround(pos.getX(), pos.getY(), pos.getZ(), range);
  120.     }
  121.  
  122.     public static AxisAlignedBB getBoundsAround(double x, double y, double z, double range) {
  123.         return new AxisAlignedBB(x - range, y - range, z - range, x + range, y + range, z + range);
  124.     }
  125.  
  126.     private static int getRandomNumberInRange(int min, int max) {
  127.         if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); }
  128.         Random r = new Random();return r.nextInt((max - min) + 1) + min;
  129.     }
  130.  
  131.     public static Vec3d getCentre(BlockPos pos) {
  132.         return new Vec3d(pos).add(0.5, 0.5, 0.5);
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement