Search in sources :

Example 1 with AbstractSpawner

use of net.minecraft.world.spawner.AbstractSpawner in project ChaosAwakens by ChaosAwakens.

the class SpawnEggItemMixin method useOn.

@Inject(method = "useOn(Lnet/minecraft/item/ItemUseContext;)Lnet/minecraft/util/ActionResultType;", at = @At("HEAD"), cancellable = true)
public void useOn(ItemUseContext itemUseContext, CallbackInfoReturnable<ActionResultType> cir) {
    World world = itemUseContext.getLevel();
    if (!(world instanceof ServerWorld)) {
        cir.setReturnValue(ActionResultType.SUCCESS);
    } else {
        ItemStack itemstack = itemUseContext.getItemInHand();
        BlockPos blockpos = itemUseContext.getClickedPos();
        Direction direction = itemUseContext.getClickedFace();
        BlockState blockstate = world.getBlockState(blockpos);
        PlayerEntity player = itemUseContext.getPlayer();
        if (blockstate.is(Blocks.SPAWNER)) {
            if ((CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 0) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 1 && player.isCreative()) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 2 && player.isCreative() && itemstack.getItem().getRegistryName().getNamespace().equals("chaosawakens")) || (CAConfig.COMMON.spawnEggsSpawnersSurvival.get() == 2 && !itemstack.getItem().getRegistryName().getNamespace().equals("chaosawakens"))) {
                TileEntity tileentity = world.getBlockEntity(blockpos);
                if (tileentity instanceof MobSpawnerTileEntity) {
                    AbstractSpawner abstractspawner = ((MobSpawnerTileEntity) tileentity).getSpawner();
                    EntityType<?> entitytype1 = this.getType(itemstack.getTag());
                    abstractspawner.setEntityId(entitytype1);
                    tileentity.setChanged();
                    world.sendBlockUpdated(blockpos, blockstate, blockstate, 3);
                    itemstack.shrink(1);
                    cir.setReturnValue(ActionResultType.CONSUME);
                }
            }
        }
        BlockPos blockpos1;
        if (blockstate.getCollisionShape(world, blockpos).isEmpty()) {
            blockpos1 = blockpos;
        } else {
            blockpos1 = blockpos.relative(direction);
        }
        EntityType<?> entitytype = this.getType(itemstack.getTag());
        if (!blockstate.is(Blocks.SPAWNER)) {
            if (entitytype.spawn((ServerWorld) world, itemstack, itemUseContext.getPlayer(), blockpos1, SpawnReason.SPAWN_EGG, true, !Objects.equals(blockpos, blockpos1) && direction == Direction.UP) != null) {
                itemstack.shrink(1);
            }
        }
        cir.setReturnValue(ActionResultType.CONSUME);
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) AbstractSpawner(net.minecraft.world.spawner.AbstractSpawner) BlockPos(net.minecraft.util.math.BlockPos) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with AbstractSpawner

use of net.minecraft.world.spawner.AbstractSpawner in project ChocolateQuestRepoured by TeamChocoQuest.

the class PreparableSpawnerInfo method vanillaSpawnerReadFromNBT.

private void vanillaSpawnerReadFromNBT(World world, DungeonPlacement placement, BlockPos pos, MobSpawnerTileEntity tileEntity) {
    AbstractSpawner spawnerBaseLogic = tileEntity.getSpawnerBaseLogic();
    CompoundNBT compound = new CompoundNBT();
    compound.setShort("Delay", (short) 20);
    if (this.tileEntityData.hasKey("MinSpawnDelay", Constants.NBT.TAG_ANY_NUMERIC)) {
        compound.setShort("MinSpawnDelay", this.tileEntityData.getShort("MinSpawnDelay"));
        compound.setShort("MaxSpawnDelay", this.tileEntityData.getShort("MaxSpawnDelay"));
        compound.setShort("SpawnCount", this.tileEntityData.getShort("SpawnCount"));
    }
    if (this.tileEntityData.hasKey("MaxNearbyEntities", Constants.NBT.TAG_ANY_NUMERIC)) {
        compound.setShort("MaxNearbyEntities", this.tileEntityData.getShort("MaxNearbyEntities"));
        compound.setShort("RequiredPlayerRange", this.tileEntityData.getShort("RequiredPlayerRange"));
    }
    if (this.tileEntityData.hasKey("SpawnRange", Constants.NBT.TAG_ANY_NUMERIC)) {
        compound.setShort("SpawnRange", this.tileEntityData.getShort("SpawnRange"));
    }
    ListNBT nbttaglist = new ListNBT();
    ListNBT items = this.tileEntityData.getCompoundTag("inventory").getTagList("Items", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < items.tagCount(); i++) {
        CompoundNBT itemTag = items.getCompoundTagAt(i);
        CompoundNBT entityTag = itemTag.getCompoundTag("tag").getCompoundTag("EntityIn");
        Entity entity = this.createEntityFromTag(world, placement, pos, entityTag);
        if (entity != null) {
            CompoundNBT newEntityTag = new CompoundNBT();
            entity.writeToNBTAtomically(newEntityTag);
            newEntityTag.removeTag("UUIDLeast");
            newEntityTag.removeTag("UUIDMost");
            newEntityTag.removeTag("Pos");
            if (nbttaglist.isEmpty()) {
                compound.setTag("SpawnData", newEntityTag);
            }
            nbttaglist.appendTag(new WeightedSpawnerEntity(itemTag.getByte("Count"), newEntityTag).toCompoundTag());
        }
    }
    compound.setTag("SpawnPotentials", nbttaglist);
    spawnerBaseLogic.readFromNBT(compound);
}
Also used : WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity) MobEntity(net.minecraft.entity.MobEntity) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) ListNBT(net.minecraft.nbt.ListNBT) AbstractSpawner(net.minecraft.world.spawner.AbstractSpawner) CompoundNBT(net.minecraft.nbt.CompoundNBT) WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity)

Example 3 with AbstractSpawner

use of net.minecraft.world.spawner.AbstractSpawner in project Arclight by IzzelAliz.

the class AbstractSpawnerMixin method tick.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void tick() {
    if (!this.isActivated()) {
        this.prevMobRotation = this.mobRotation;
    } else {
        World world = this.getWorld();
        BlockPos blockpos = this.getSpawnerPosition();
        if (world.isRemote) {
            double d3 = (double) blockpos.getX() + (double) world.rand.nextFloat();
            double d4 = (double) blockpos.getY() + (double) world.rand.nextFloat();
            double d5 = (double) blockpos.getZ() + (double) world.rand.nextFloat();
            world.addParticle(ParticleTypes.SMOKE, d3, d4, d5, 0.0D, 0.0D, 0.0D);
            world.addParticle(ParticleTypes.FLAME, d3, d4, d5, 0.0D, 0.0D, 0.0D);
            if (this.spawnDelay > 0) {
                --this.spawnDelay;
            }
            this.prevMobRotation = this.mobRotation;
            this.mobRotation = (this.mobRotation + (double) (1000.0F / ((float) this.spawnDelay + 200.0F))) % 360.0D;
        } else {
            if (this.spawnDelay == -1) {
                this.resetTimer();
            }
            if (this.spawnDelay > 0) {
                --this.spawnDelay;
                return;
            }
            boolean flag = false;
            for (int i = 0; i < this.spawnCount; ++i) {
                CompoundNBT compoundnbt = this.spawnData.getNbt();
                Optional<EntityType<?>> optional = EntityType.readEntityType(compoundnbt);
                if (!optional.isPresent()) {
                    this.resetTimer();
                    return;
                }
                ListNBT listnbt = compoundnbt.getList("Pos", 6);
                int j = listnbt.size();
                double d0 = j >= 1 ? listnbt.getDouble(0) : (double) blockpos.getX() + (world.rand.nextDouble() - world.rand.nextDouble()) * (double) this.spawnRange + 0.5D;
                double d1 = j >= 2 ? listnbt.getDouble(1) : (double) (blockpos.getY() + world.rand.nextInt(3) - 1);
                double d2 = j >= 3 ? listnbt.getDouble(2) : (double) blockpos.getZ() + (world.rand.nextDouble() - world.rand.nextDouble()) * (double) this.spawnRange + 0.5D;
                if (world.hasNoCollisions(optional.get().getBoundingBoxWithSizeApplied(d0, d1, d2)) && EntitySpawnPlacementRegistry.func_223515_a(optional.get(), world.getWorld(), SpawnReason.SPAWNER, new BlockPos(d0, d1, d2), world.getRandom())) {
                    Entity entity = EntityType.loadEntityAndExecute(compoundnbt, world, (p_221408_6_) -> {
                        p_221408_6_.setLocationAndAngles(d0, d1, d2, p_221408_6_.rotationYaw, p_221408_6_.rotationPitch);
                        return p_221408_6_;
                    });
                    if (entity == null) {
                        this.resetTimer();
                        return;
                    }
                    int k = world.getEntitiesWithinAABB(entity.getClass(), (new AxisAlignedBB((double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), (double) (blockpos.getX() + 1), (double) (blockpos.getY() + 1), (double) (blockpos.getZ() + 1))).grow((double) this.spawnRange)).size();
                    if (k >= this.maxNearbyEntities) {
                        this.resetTimer();
                        return;
                    }
                    entity.setLocationAndAngles(entity.getPosX(), entity.getPosY(), entity.getPosZ(), world.rand.nextFloat() * 360.0F, 0.0F);
                    if (entity instanceof MobEntity) {
                        MobEntity mobentity = (MobEntity) entity;
                        if (!ForgeEventFactory.canEntitySpawnSpawner(mobentity, world, (float) entity.getPosX(), (float) entity.getPosY(), (float) entity.getPosZ(), (AbstractSpawner) (Object) this)) {
                            continue;
                        }
                        if (this.spawnData.getNbt().size() == 1 && this.spawnData.getNbt().contains("id", 8)) {
                            ((MobEntity) entity).onInitialSpawn(world, world.getDifficultyForLocation(new BlockPos(entity)), SpawnReason.SPAWNER, (ILivingEntityData) null, (CompoundNBT) null);
                        }
                        if (((WorldBridge) mobentity.world).bridge$spigotConfig().nerfSpawnerMobs) {
                            ((MobEntityBridge) mobentity).bridge$setAware(false);
                        }
                    }
                    if (CraftEventFactory.callSpawnerSpawnEvent(entity, blockpos).isCancelled()) {
                        Entity vehicle = entity.getRidingEntity();
                        if (vehicle != null) {
                            vehicle.removed = true;
                        }
                        for (final Entity passenger : entity.getRecursivePassengers()) {
                            passenger.removed = true;
                        }
                    }
                    this.func_221409_a(entity);
                    world.playEvent(2004, blockpos, 0);
                    if (entity instanceof MobEntity) {
                        ((MobEntity) entity).spawnExplosionParticle();
                    }
                    flag = true;
                }
            }
            if (flag) {
                this.resetTimer();
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) WeightedSpawnerEntity(net.minecraft.util.WeightedSpawnerEntity) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) CompoundNBT(net.minecraft.nbt.CompoundNBT) World(net.minecraft.world.World) MobEntityBridge(io.izzel.arclight.common.bridge.entity.MobEntityBridge) EntityType(net.minecraft.entity.EntityType) ListNBT(net.minecraft.nbt.ListNBT) AbstractSpawner(net.minecraft.world.spawner.AbstractSpawner) BlockPos(net.minecraft.util.math.BlockPos) MobEntity(net.minecraft.entity.MobEntity) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 4 with AbstractSpawner

use of net.minecraft.world.spawner.AbstractSpawner in project Spackenmobs by ACGaming.

the class CustomSpawnEggItem method useOn.

public ActionResultType useOn(ItemUseContext context) {
    World worldIn = context.getLevel();
    if (!worldIn.isClientSide) {
        ItemStack stack = context.getItemInHand();
        BlockPos pos = context.getClickedPos();
        Direction dir = context.getClickedFace();
        BlockState state = worldIn.getBlockState(pos);
        Block block = state.getBlock();
        if (block == Blocks.SPAWNER) {
            TileEntity tile = worldIn.getBlockEntity(pos);
            if (tile instanceof MobSpawnerTileEntity) {
                AbstractSpawner spawner = ((MobSpawnerTileEntity) tile).getSpawner();
                EntityType<?> type = this.getType(stack.getTag());
                spawner.setEntityId(type);
                tile.setChanged();
                worldIn.sendBlockUpdated(pos, state, state, 3);
                stack.shrink(1);
                return ActionResultType.SUCCESS;
            }
        }
        BlockPos pos2;
        if (state.getCollisionShape(worldIn, pos).isEmpty()) {
            pos2 = pos;
        } else {
            pos2 = pos.relative(dir);
        }
        EntityType<?> type = this.getType(stack.getTag());
        if (type.spawn((ServerWorld) worldIn, stack, context.getPlayer(), pos2, SpawnReason.SPAWN_EGG, true, !Objects.equals(pos, pos2) && dir == Direction.UP) != null) {
            stack.shrink(1);
        }
    }
    return ActionResultType.SUCCESS;
}
Also used : MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ServerWorld(net.minecraft.world.server.ServerWorld) BlockState(net.minecraft.block.BlockState) AbstractSpawner(net.minecraft.world.spawner.AbstractSpawner) Block(net.minecraft.block.Block) FlowingFluidBlock(net.minecraft.block.FlowingFluidBlock) BlockPos(net.minecraft.util.math.BlockPos) MobSpawnerTileEntity(net.minecraft.tileentity.MobSpawnerTileEntity) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction)

Aggregations

AbstractSpawner (net.minecraft.world.spawner.AbstractSpawner)4 MobSpawnerTileEntity (net.minecraft.tileentity.MobSpawnerTileEntity)3 TileEntity (net.minecraft.tileentity.TileEntity)3 BlockPos (net.minecraft.util.math.BlockPos)3 World (net.minecraft.world.World)3 BlockState (net.minecraft.block.BlockState)2 Entity (net.minecraft.entity.Entity)2 MobEntity (net.minecraft.entity.MobEntity)2 ItemStack (net.minecraft.item.ItemStack)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 ListNBT (net.minecraft.nbt.ListNBT)2 Direction (net.minecraft.util.Direction)2 WeightedSpawnerEntity (net.minecraft.util.WeightedSpawnerEntity)2 ServerWorld (net.minecraft.world.server.ServerWorld)2 MobEntityBridge (io.izzel.arclight.common.bridge.entity.MobEntityBridge)1 Block (net.minecraft.block.Block)1 FlowingFluidBlock (net.minecraft.block.FlowingFluidBlock)1 EntityType (net.minecraft.entity.EntityType)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1