package com.halestormxv.mysterium.tileentity; import com.halestormxv.mysterium.MysteriumMain; import com.halestormxv.mysterium.init.ModTileEntityTypes; import com.halestormxv.mysterium.init.PotionInit; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.nbt.*; import net.minecraft.network.play.server.SChangeGameStatePacket; import net.minecraft.network.play.server.SUpdateTileEntityPacket; import net.minecraft.potion.Effect; import net.minecraft.potion.EffectInstance; import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.server.ServerWorld; import javax.annotation.Nullable; import java.util.Iterator; import java.util.List; import java.util.Random; public class StructureCoreTileEntity extends TileEntity implements ITickableTileEntity { private AxisAlignedBB containmentField; public StructureCoreTileEntity(TileEntityType typeIn) { super(typeIn); } public StructureCoreTileEntity() { this(ModTileEntityTypes.STRUCTURE_CORE_TE.get()); } @Override public void setPos(BlockPos posIn) { super.setPos(posIn); initContainmentField(posIn); } private void initContainmentField(BlockPos pos) { //double r = 16; //this.containmentField = new AxisAlignedBB(-r, -r, -r, r, r, r);//.offset(getCentre(pos)); this.containmentField = getBoundsAround(pos, 16); } @Override public void tick() { if (this.world != null && !this.world.isRemote) { //ServerWorld serverWorld = (ServerWorld) world; //this.afflictNearbyTargets(); List entities = world.getEntitiesWithinAABB(LivingEntity.class, containmentField, e -> e instanceof PlayerEntity); for(LivingEntity entity : entities) { entity.addPotionEffect(new EffectInstance(PotionInit.WATCHFUL_EYE_EFFECT.get(), 200, 1)); MysteriumMain.LOGGER.info("I am being called."); boolean soundChance = new Random().nextInt(10) == 0; if (soundChance) { int soundSelection = getRandomNumberInRange(1, 4); if (soundSelection == 1) { //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); } if (soundSelection == 2) { //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); } } } } } //DISABLED BECAUSE IT WORKS BUT THEN NEVER REMOVES THE DEBUFF - FIND THE PROPER WAY TO DO IT WITH THE getEntitiesWithinAABB\\\\\\\\\\\\\\ private void afflictNearbyTargets() { Effect effect = PotionInit.WATCHFUL_EYE_EFFECT.get(); List list = ((ServerWorld) this.world).getPlayers((playersInList) -> this.getDistanceSq(this.pos.getX(), this.pos.getY(), this.pos.getZ()) < 1250.0D && playersInList.interactionManager.survivalOrAdventure()); Iterator var7 = list.iterator(); label28: while (true) { ServerPlayerEntity serverplayerentity; do { if (!var7.hasNext()) { break label28; } serverplayerentity = (ServerPlayerEntity) var7.next(); } while (serverplayerentity.isPotionActive(effect) && serverplayerentity.getActivePotionEffect(effect).getAmplifier() >= 2 && serverplayerentity.getActivePotionEffect(effect).getDuration() >= 800); serverplayerentity.connection.sendPacket(new SChangeGameStatePacket(10, 0.0F)); serverplayerentity.addPotionEffect(new EffectInstance(effect, 1400, 1)); } } @Override @Nullable public SUpdateTileEntityPacket getUpdatePacket() { CompoundNBT nbtTagCompound = new CompoundNBT(); write(nbtTagCompound); int tileEntityType = 42; return new SUpdateTileEntityPacket(this.pos, tileEntityType, nbtTagCompound); } //REFERENCE CODE\\ /*private void afflictNearbyTargets() { List entities = world.getEntitiesWithinAABB(LivingEntity.class, containmentField, e -> e instanceof PlayerEntity); for(LivingEntity entity : entities){ entity.addPotionEffect(new EffectInstance(PotionInit.WATCHFUL_EYE_EFFECT.get(), 200, 1)); MysteriumMain.LOGGER.info("I am being called."); } }*/ //---------------------------UTILITIES TO MOVE TO SEPARATE CLASS EVENTUALLY----------------------------------\\ public static AxisAlignedBB getBoundsAround(Vec3d pos, double range) { return getBoundsAround(pos.x, pos.y, pos.z, range); } public static AxisAlignedBB getBoundsAround(BlockPos pos, int range) { return getBoundsAround(pos.getX(), pos.getY(), pos.getZ(), range); } public static AxisAlignedBB getBoundsAround(double x, double y, double z, double range) { return new AxisAlignedBB(x - range, y - range, z - range, x + range, y + range, z + range); } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random();return r.nextInt((max - min) + 1) + min; } public static Vec3d getCentre(BlockPos pos) { return new Vec3d(pos).add(0.5, 0.5, 0.5); } }