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);
});
});
}
}
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);
}
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);
}
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;
}
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;
}
}
Aggregations