Search in sources :

Example 36 with EntityType

use of net.minecraft.entity.EntityType in project Mekanism by mekanism.

the class SpawnHelper method onStructureSpawnListGather.

public static void onStructureSpawnListGather(StructureSpawnListGatherEvent event) {
    // Add special spawns to any structures that have mob spawns for the "parent" types of our mobs
    List<MobSpawnInfo.Spawners> monsterSpawns = event.getEntitySpawns(EntityClassification.MONSTER);
    if (!monsterSpawns.isEmpty()) {
        // Fail quick if no monsters can spawn in this structure anyway
        ResourceLocation structureName = event.getStructure().getRegistryName();
        getSpawnConfigs().filter(spawnConfig -> spawnConfig.shouldSpawn.get() && !spawnConfig.structureBlackList.get().contains(structureName)).forEach(spawnConfig -> {
            EntityType<?> parent = spawnConfig.parentTypeProvider.getEntityType();
            monsterSpawns.stream().filter(monsterSpawn -> monsterSpawn.type == parent).findFirst().ifPresent(parentEntry -> {
                // If the adult mob can spawn in this structure let the baby mob spawn in it
                // Note: We adjust the mob's spawning based on the adult's spawn rates
                MobSpawnInfo.Spawners spawner = getSpawner(spawnConfig, parentEntry);
                event.addEntitySpawn(EntityClassification.MONSTER, spawner);
                Mekanism.logger.debug("Adding spawn rate for '{}' in structure '{}', with weight: {}, minSize: {}, maxSize: {}", spawner.type.getRegistryName(), structureName, spawner.weight, spawner.minCount, spawner.maxCount);
            });
        });
    }
}
Also used : EntityType(net.minecraft.entity.EntityType) Mekanism(mekanism.common.Mekanism) BiomeLoadingEvent(net.minecraftforge.event.world.BiomeLoadingEvent) AdditionsEntityTypes(mekanism.additions.common.registries.AdditionsEntityTypes) ParrotEntity(net.minecraft.entity.passive.ParrotEntity) EntityTypeRegistryObject(mekanism.common.registration.impl.EntityTypeRegistryObject) MobSpawnInfo(net.minecraft.world.biome.MobSpawnInfo) EntityBabyStray(mekanism.additions.common.entity.baby.EntityBabyStray) MobSpawnInfoBuilder(net.minecraftforge.common.world.MobSpawnInfoBuilder) AdditionsCommonConfig(mekanism.additions.common.config.AdditionsCommonConfig) EntityClassification(net.minecraft.entity.EntityClassification) SoundEvents(net.minecraft.util.SoundEvents) List(java.util.List) Stream(java.util.stream.Stream) EntitySpawnPlacementRegistry(net.minecraft.entity.EntitySpawnPlacementRegistry) MekanismAdditionsConfig(mekanism.additions.common.config.MekanismAdditionsConfig) ResourceLocation(net.minecraft.util.ResourceLocation) StructureSpawnListGatherEvent(net.minecraftforge.event.world.StructureSpawnListGatherEvent) MonsterEntity(net.minecraft.entity.monster.MonsterEntity) Heightmap(net.minecraft.world.gen.Heightmap) ResourceLocation(net.minecraft.util.ResourceLocation) MobSpawnInfo(net.minecraft.world.biome.MobSpawnInfo)

Example 37 with EntityType

use of net.minecraft.entity.EntityType in project Structurize by ldtteam.

the class StructurePlacer method getResourceRequirements.

/**
 * This method handles the block placement.
 * When we extract this into another mod, we have to override the method.
 *  @param world          the world.
 * @param worldPos       the world position.
 * @param localPos       the local pos.
 * @param localState     the local state.
 * @param tileEntityData the tileEntity.
 */
public BlockPlacementResult getResourceRequirements(final World world, final BlockPos worldPos, final BlockPos localPos, BlockState localState, final CompoundNBT tileEntityData) {
    final BlockState worldState = world.getBlockState(worldPos);
    boolean sameBlockInWorld = false;
    if (worldState.getBlock() == localState.getBlock()) {
        sameBlockInWorld = true;
    }
    final List<ItemStack> requiredItems = new ArrayList<>();
    for (final CompoundNBT compound : iterator.getBluePrintPositionInfo(localPos).getEntities()) {
        if (compound != null) {
            try {
                final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
                final Optional<EntityType<?>> type = EntityType.by(compound);
                if (type.isPresent()) {
                    final Entity entity = type.get().create(world);
                    if (entity != null) {
                        entity.deserializeNBT(compound);
                        final Vector3d posInWorld = entity.position().add(pos.getX(), pos.getY(), pos.getZ());
                        final List<? extends Entity> list = world.getEntitiesOfClass(entity.getClass(), new AxisAlignedBB(posInWorld.add(1, 1, 1), posInWorld.add(-1, -1, -1)));
                        boolean foundEntity = false;
                        for (Entity worldEntity : list) {
                            if (worldEntity.position().equals(posInWorld)) {
                                foundEntity = true;
                                break;
                            }
                        }
                        if (foundEntity) {
                            continue;
                        }
                        requiredItems.addAll(ItemStackUtils.getListOfStackForEntity(entity, pos));
                    }
                }
            } catch (final RuntimeException e) {
                Log.getLogger().info("Couldn't restore entity", e);
            }
        }
    }
    if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement()) {
        localState = this.handler.getSolidBlockForPos(worldPos);
    }
    if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement()) {
        localState = Blocks.AIR.defaultBlockState();
    }
    for (final IPlacementHandler placementHandler : PlacementHandlers.handlers) {
        if (placementHandler.canHandle(world, worldPos, localState)) {
            if (!sameBlockInWorld) {
                for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false)) {
                    if (!stack.isEmpty() && !this.handler.isStackFree(stack)) {
                        requiredItems.add(stack);
                    }
                }
            }
            return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
        }
    }
    return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) IPlacementHandler(com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler) CompoundNBT(net.minecraft.nbt.CompoundNBT) ArrayList(java.util.ArrayList) EntityType(net.minecraft.entity.EntityType) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 38 with EntityType

use of net.minecraft.entity.EntityType in project Structurize by ldtteam.

the class StructurePlacer method handleBlockPlacement.

/**
 * This method handles the block placement.
 * When we extract this into another mod, we have to override the method.
 * @param world          the world.
 * @param worldPos       the world position.
 * @param localPos       the local pos
 * @param storage        the change storage.
 * @param localState     the local state.
 * @param tileEntityData the tileEntity.
 */
public BlockPlacementResult handleBlockPlacement(final World world, final BlockPos worldPos, final BlockPos localPos, final ChangeStorage storage, BlockState localState, final CompoundNBT tileEntityData) {
    final BlockState worldState = world.getBlockState(worldPos);
    boolean sameBlockInWorld = worldState.getBlock() == localState.getBlock();
    if (!(worldState.getBlock() instanceof AirBlock)) {
        if (!handler.allowReplace()) {
            return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.BREAK_BLOCK);
        }
    }
    for (final CompoundNBT compound : this.iterator.getBluePrintPositionInfo(localPos).getEntities()) {
        if (compound != null) {
            try {
                final BlockPos pos = this.handler.getWorldPos().subtract(handler.getBluePrint().getPrimaryBlockOffset());
                final Optional<EntityType<?>> type = EntityType.by(compound);
                if (type.isPresent()) {
                    final Entity entity = type.get().create(world);
                    if (entity != null) {
                        entity.deserializeNBT(compound);
                        entity.setUUID(UUID.randomUUID());
                        final Vector3d posInWorld = entity.position().add(pos.getX(), pos.getY(), pos.getZ());
                        entity.setPos(posInWorld.x, posInWorld.y, posInWorld.z);
                        final List<? extends Entity> list = world.getEntitiesOfClass(entity.getClass(), new AxisAlignedBB(posInWorld.add(1, 1, 1), posInWorld.add(-1, -1, -1)));
                        boolean foundEntity = false;
                        for (Entity worldEntity : list) {
                            if (worldEntity.position().equals(posInWorld)) {
                                foundEntity = true;
                                break;
                            }
                        }
                        if (foundEntity || (entity instanceof MobEntity && !handler.isCreative())) {
                            continue;
                        }
                        final List<ItemStack> requiredItems = new ArrayList<>();
                        if (!handler.isCreative()) {
                            requiredItems.addAll(ItemStackUtils.getListOfStackForEntity(entity, pos));
                            if (!InventoryUtils.hasRequiredItems(handler.getInventory(), requiredItems)) {
                                return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
                            }
                        }
                        world.addFreshEntity(entity);
                        if (storage != null) {
                            storage.addToBeKilledEntity(entity);
                        }
                        for (final ItemStack tempStack : requiredItems) {
                            if (!ItemStackUtils.isEmpty(tempStack)) {
                                InventoryUtils.consumeStack(tempStack, handler.getInventory());
                            }
                        }
                        this.handler.triggerEntitySuccess(localPos, requiredItems, true);
                    }
                }
            } catch (final RuntimeException e) {
                Log.getLogger().info("Couldn't restore entity", e);
            }
        }
    }
    if (BlockUtils.areBlockStatesEqual(localState, worldState, handler::replaceWithSolidBlock, handler.fancyPlacement(), handler::shouldBlocksBeConsideredEqual)) {
        return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
    }
    if (localState.getBlock() == ModBlocks.blockSolidSubstitution.get() && handler.fancyPlacement()) {
        localState = this.handler.getSolidBlockForPos(worldPos);
    }
    if (localState.getBlock() == ModBlocks.blockTagSubstitution.get() && handler.fancyPlacement()) {
        localState = Blocks.AIR.defaultBlockState();
    }
    for (final IPlacementHandler placementHandler : PlacementHandlers.handlers) {
        if (placementHandler.canHandle(world, worldPos, localState)) {
            final List<ItemStack> requiredItems = new ArrayList<>();
            if (!sameBlockInWorld && !this.handler.isCreative()) {
                for (final ItemStack stack : placementHandler.getRequiredItems(world, worldPos, localState, tileEntityData, false)) {
                    if (!stack.isEmpty() && !this.handler.isStackFree(stack)) {
                        requiredItems.add(stack);
                    }
                }
                if (!this.handler.hasRequiredItems(requiredItems)) {
                    return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.MISSING_ITEMS, requiredItems);
                }
            }
            if (!(worldState.getBlock() instanceof AirBlock)) {
                if (!sameBlockInWorld && worldState.getMaterial() != Material.AIR && !(worldState.getBlock() instanceof DoublePlantBlock && worldState.getValue(DoublePlantBlock.HALF).equals(DoubleBlockHalf.UPPER))) {
                    placementHandler.handleRemoval(handler, world, worldPos, tileEntityData);
                }
            }
            this.handler.prePlacementLogic(worldPos, localState, requiredItems);
            final IPlacementHandler.ActionProcessingResult result = placementHandler.handle(world, worldPos, localState, tileEntityData, !this.handler.fancyPlacement(), this.handler.getWorldPos(), this.handler.getSettings());
            if (result == IPlacementHandler.ActionProcessingResult.DENY) {
                return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.FAIL);
            }
            this.handler.triggerSuccess(localPos, requiredItems, true);
            if (result == IPlacementHandler.ActionProcessingResult.PASS) {
                return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
            }
            if (!this.handler.isCreative() && !sameBlockInWorld) {
                for (final ItemStack tempStack : requiredItems) {
                    if (!ItemStackUtils.isEmpty(tempStack)) {
                        InventoryUtils.consumeStack(tempStack, handler.getInventory());
                    }
                }
            }
            return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.SUCCESS);
        }
    }
    return new BlockPlacementResult(worldPos, BlockPlacementResult.Result.FAIL);
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) IPlacementHandler(com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler) CompoundNBT(net.minecraft.nbt.CompoundNBT) ArrayList(java.util.ArrayList) EntityType(net.minecraft.entity.EntityType) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) MobEntity(net.minecraft.entity.MobEntity)

Example 39 with EntityType

use of net.minecraft.entity.EntityType in project Structurize by ldtteam.

the class ChangeStorage method undo.

/**
 * Reload the previous state of the positions.
 *
 * @param world       the world to manipulate.
 * @param undoStorage
 * @return true if successful.
 */
public boolean undo(final World world, @Nullable final ChangeStorage undoStorage) {
    if (iterator == null) {
        iterator = blocks.entrySet().iterator();
    }
    int count = 0;
    while (iterator.hasNext()) {
        final Map.Entry<BlockPos, BlockChangeData> entry = iterator.next();
        // Only revert block changes which this operation caused
        if (world.getBlockState(entry.getKey()) != entry.getValue().getPostState()) {
            continue;
        }
        if (undoStorage != null) {
            undoStorage.addPreviousDataFor(entry.getKey(), world);
        }
        world.setBlockAndUpdate(entry.getKey(), entry.getValue().getPreState());
        if (entry.getValue().getPreTE() != null) {
            world.setBlockEntity(entry.getKey(), entry.getValue().getPreTE());
        }
        if (undoStorage != null) {
            undoStorage.addPostDataFor(entry.getKey(), world);
        }
        count++;
        if (count >= Structurize.getConfig().getServer().maxOperationsPerTick.get()) {
            return false;
        }
    }
    for (final CompoundNBT data : removedEntities) {
        final Optional<EntityType<?>> type = EntityType.by(data);
        if (type.isPresent()) {
            final Entity entity = type.get().create(world);
            if (entity != null) {
                entity.deserializeNBT(data);
                world.addFreshEntity(entity);
                if (undoStorage != null) {
                    undoStorage.addedEntities.add(entity);
                }
            }
        }
    }
    addedEntities.forEach(Entity::remove);
    if (undoStorage != null) {
        Manager.addToUndoRedoCache(undoStorage);
    }
    return true;
}
Also used : EntityType(net.minecraft.entity.EntityType) Entity(net.minecraft.entity.Entity) CompoundNBT(net.minecraft.nbt.CompoundNBT) BlockPos(net.minecraft.util.math.BlockPos)

Example 40 with EntityType

use of net.minecraft.entity.EntityType in project Structurize by ldtteam.

the class BlueprintUtils method constructEntity.

@Nullable
private static Entity constructEntity(@Nullable final CompoundNBT info, @NotNull final BlueprintBlockAccess blockAccess) {
    if (info == null)
        return null;
    final String entityId = info.getString("id");
    try {
        final CompoundNBT compound = info.copy();
        compound.putUUID("UUID", UUID.randomUUID());
        final Optional<EntityType<?>> type = EntityType.by(compound);
        if (type.isPresent()) {
            final Entity entity = type.get().create(blockAccess);
            if (entity != null) {
                entity.deserializeNBT(compound);
                return entity;
            }
        }
        return null;
    } catch (final Exception ex) {
        Log.getLogger().error("Could not create entity: " + entityId + " with nbt: " + info.toString(), ex);
        // blackListedEntityIds.add(entityId);
        return null;
    }
}
Also used : EntityType(net.minecraft.entity.EntityType) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) CompoundNBT(net.minecraft.nbt.CompoundNBT) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

EntityType (net.minecraft.entity.EntityType)48 Entity (net.minecraft.entity.Entity)16 ResourceLocation (net.minecraft.util.ResourceLocation)13 LivingEntity (net.minecraft.entity.LivingEntity)11 CompoundNBT (net.minecraft.nbt.CompoundNBT)11 BlockPos (net.minecraft.util.math.BlockPos)11 PlayerEntity (net.minecraft.entity.player.PlayerEntity)9 ItemStack (net.minecraft.item.ItemStack)9 HashMap (java.util.HashMap)6 World (net.minecraft.world.World)5 ArrayList (java.util.ArrayList)4 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)4 EffectInstance (net.minecraft.potion.EffectInstance)4 List (java.util.List)3 Nonnull (javax.annotation.Nonnull)3 MobEntity (net.minecraft.entity.MobEntity)3 TileEntity (net.minecraft.tileentity.TileEntity)3 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)3 IPlacementHandler (com.ldtteam.structurize.placement.handlers.placement.IPlacementHandler)2 Random (java.util.Random)2