use of net.minecraft.world.entity.decoration.ItemFrame in project SpongeCommon by SpongePowered.
the class EntityTickPhaseState method postBlockTransactionApplication.
@Override
public void postBlockTransactionApplication(final EntityTickContext context, final BlockChange blockChange, final BlockTransactionReceipt transaction) {
if (blockChange == BlockChange.BREAK) {
final Entity tickingEntity = context.getSource(Entity.class).get();
final BlockPos blockPos = VecHelper.toBlockPos(transaction.originalBlock().position());
final List<HangingEntity> hangingEntities = ((ServerLevel) tickingEntity.world()).getEntitiesOfClass(HangingEntity.class, new AABB(blockPos, blockPos).inflate(1.1D, 1.1D, 1.1D), entityIn -> {
if (entityIn == null) {
return false;
}
final BlockPos entityPos = entityIn.getPos();
// Hanging Neighbor Entity
if (entityPos.equals(blockPos.offset(0, 1, 0))) {
return true;
}
// Check around source block
final Direction entityFacing = entityIn.getDirection();
if (entityFacing == Direction.NORTH) {
return entityPos.equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_NORTH));
} else if (entityFacing == Direction.SOUTH) {
return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_SOUTH));
} else if (entityFacing == Direction.WEST) {
return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_WEST));
} else if (entityFacing == Direction.EAST) {
return entityIn.getPos().equals(blockPos.offset(Constants.Entity.HANGING_OFFSET_EAST));
}
return false;
});
for (final HangingEntity entityHanging : hangingEntities) {
if (entityHanging instanceof ItemFrame) {
final ItemFrame itemFrame = (ItemFrame) entityHanging;
if (!itemFrame.removed) {
((ItemFrameAccessor) itemFrame).invoker$dropItem((net.minecraft.world.entity.Entity) tickingEntity, true);
}
itemFrame.remove();
}
}
}
}
use of net.minecraft.world.entity.decoration.ItemFrame in project MC-Prefab by Brian-Wuest.
the class Structure method ScanStructure.
public static void ScanStructure(Level world, BlockPos originalPos, BlockPos cornerPos1, BlockPos cornerPos2, String fileLocation, BuildClear clearedSpace, Direction playerFacing, boolean includeAir, boolean excludeWater) {
Structure scannedStructure = new Structure();
scannedStructure.setClearSpace(clearedSpace);
for (BlockPos currentPos : BlockPos.betweenClosed(cornerPos1, cornerPos2)) {
if (world.isEmptyBlock(currentPos) && !includeAir) {
continue;
}
BlockState currentState = world.getBlockState(currentPos);
Block currentBlock = currentState.getBlock();
if (currentState.getMaterial() == Material.WATER && excludeWater) {
continue;
}
BuildBlock buildBlock = Structure.createBuildBlockFromBlockState(currentState, currentBlock, currentPos, originalPos);
if (currentBlock instanceof DoorBlock) {
DoubleBlockHalf blockHalf = currentState.getValue(DoorBlock.HALF);
if (blockHalf == DoubleBlockHalf.LOWER) {
BlockState upperHalfState = world.getBlockState(currentPos.above());
if (upperHalfState.getBlock() instanceof DoorBlock) {
Block upperBlock = upperHalfState.getBlock();
BuildBlock upperHalf = Structure.createBuildBlockFromBlockState(upperHalfState, upperBlock, currentPos.above(), originalPos);
buildBlock.setSubBlock(upperHalf);
}
} else {
// Don't process upper door halves. These were already done.
continue;
}
} else if (currentBlock instanceof BedBlock) {
BedPart bedPart = currentState.getValue(BedBlock.PART);
if (bedPart == BedPart.HEAD) {
BlockState bedFoot = null;
boolean foundFoot = false;
Direction facing = Direction.NORTH;
while (!foundFoot) {
bedFoot = world.getBlockState(currentPos.relative(facing));
if (bedFoot.getBlock() instanceof BedBlock && bedFoot.getValue(BedBlock.PART) == BedPart.FOOT) {
foundFoot = true;
break;
}
facing = facing.getClockWise();
if (facing == Direction.NORTH) {
// Got back to north, break out to avoid infinite loop.
break;
}
}
if (foundFoot) {
Block footBedBlock = bedFoot.getBlock();
BuildBlock bed = Structure.createBuildBlockFromBlockState(bedFoot, footBedBlock, currentPos.relative(facing), originalPos);
buildBlock.setSubBlock(bed);
}
} else {
// Don't process foot of bed, it was already done.
continue;
}
}
scannedStructure.getBlocks().add(buildBlock);
BlockEntity tileEntity = world.getBlockEntity(currentPos);
if (tileEntity != null) {
// Don't write data for empty tile entities.
if ((tileEntity instanceof ChestBlockEntity && ((ChestBlockEntity) tileEntity).isEmpty()) || (tileEntity instanceof FurnaceBlockEntity && ((FurnaceBlockEntity) tileEntity).isEmpty())) {
continue;
}
ResourceLocation resourceLocation = ForgeRegistries.BLOCK_ENTITIES.getKey(tileEntity.getType());
CompoundTag tagCompound = tileEntity.saveWithFullMetadata();
BuildTileEntity buildTileEntity = new BuildTileEntity();
assert resourceLocation != null;
buildTileEntity.setEntityDomain(resourceLocation.getNamespace());
buildTileEntity.setEntityName(resourceLocation.getPath());
buildTileEntity.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(currentPos, originalPos));
buildTileEntity.setEntityNBTData(tagCompound);
scannedStructure.tileEntities.add(buildTileEntity);
}
}
int x_radiusRangeBegin = Math.min(cornerPos1.getX(), cornerPos2.getX());
int x_radiusRangeEnd = Math.max(cornerPos1.getX(), cornerPos2.getX());
int y_radiusRangeBegin = Math.min(cornerPos1.getY(), cornerPos2.getY());
int y_radiusRangeEnd = Math.max(cornerPos1.getY(), cornerPos2.getY());
int z_radiusRangeBegin = Math.min(cornerPos1.getZ(), cornerPos2.getZ());
int z_radiusRangeEnd = Math.max(cornerPos1.getZ(), cornerPos2.getZ());
AABB axis = new AABB(cornerPos1, cornerPos2);
for (Entity entity : world.getEntities(null, axis)) {
// TODO: This was the "getPosition" method.
BlockPos entityPos = entity.blockPosition();
if (entity instanceof HangingEntity) {
// Use the HangingEntity getPos function instead since it is more accurate for itemframes and paintings.
entityPos = ((HangingEntity) entity).getPos();
}
if (entityPos.getX() >= x_radiusRangeBegin && entityPos.getX() <= x_radiusRangeEnd && entityPos.getZ() >= z_radiusRangeBegin && entityPos.getZ() <= z_radiusRangeEnd && entityPos.getY() >= y_radiusRangeBegin && entityPos.getY() <= y_radiusRangeEnd) {
BuildEntity buildEntity = new BuildEntity();
buildEntity.setEntityResourceString(ForgeRegistries.ENTITIES.getKey(entity.getType()));
buildEntity.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(entityPos, originalPos));
// The function calls below get the following fields from the "entity" class. posX, posY, posZ.
// This will probably have to change when the mappings get updated.
buildEntity.entityXAxisOffset = entityPos.getX() - entity.getX();
buildEntity.entityYAxisOffset = entityPos.getY() - entity.getY();
buildEntity.entityZAxisOffset = entityPos.getZ() - entity.getZ();
if (entity instanceof ItemFrame) {
buildEntity.entityYAxisOffset = buildEntity.entityYAxisOffset * -1;
}
if (entity instanceof HangingEntity) {
buildEntity.entityFacing = entity.getDirection();
}
CompoundTag entityTagCompound = new CompoundTag();
entity.saveAsPassenger(entityTagCompound);
buildEntity.setEntityNBTData(entityTagCompound);
scannedStructure.entities.add(buildEntity);
}
}
Structure.CreateStructureFile(scannedStructure, fileLocation);
}
use of net.minecraft.world.entity.decoration.ItemFrame in project MC-Prefab by Brian-Wuest.
the class StructureEventHandler method processStructureEntities.
private static void processStructureEntities() {
for (Tuple<Structure, BuildEntity> entityRecords : StructureEventHandler.entitiesToGenerate) {
BuildEntity buildEntity = entityRecords.getSecond();
Structure structure = entityRecords.getFirst();
Optional<EntityType<?>> entityType = EntityType.byString(buildEntity.getEntityResourceString());
if (entityType.isPresent()) {
Entity entity = entityType.get().create(structure.world);
if (entity != null) {
CompoundTag tagCompound = buildEntity.getEntityDataTag();
BlockPos entityPos = buildEntity.getStartingPosition().getRelativePosition(structure.originalPos, structure.getClearSpace().getShape().getDirection(), structure.configuration.houseFacing);
if (tagCompound != null) {
if (tagCompound.hasUUID("UUID")) {
tagCompound.putUUID("UUID", UUID.randomUUID());
}
ListTag nbttaglist = new ListTag();
nbttaglist.add(DoubleTag.valueOf(entityPos.getX()));
nbttaglist.add(DoubleTag.valueOf(entityPos.getY()));
nbttaglist.add(DoubleTag.valueOf(entityPos.getZ()));
tagCompound.put("Pos", nbttaglist);
entity.load(tagCompound);
}
// Set item frame facing and rotation here.
if (entity instanceof ItemFrame) {
entity = StructureEventHandler.setItemFrameFacingAndRotation((ItemFrame) entity, buildEntity, entityPos, structure);
} else if (entity instanceof Painting) {
entity = StructureEventHandler.setPaintingFacingAndRotation((Painting) entity, buildEntity, entityPos, structure);
} else if (entity instanceof AbstractMinecart) {
// Minecarts need to be slightly higher to account for the rails; otherwise they will fall through the rail and the block below the rail.
buildEntity.entityYAxisOffset = buildEntity.entityYAxisOffset + .2;
entity = StructureEventHandler.setEntityFacingAndRotation(entity, buildEntity, entityPos, structure);
} else {
// Other entities
entity = StructureEventHandler.setEntityFacingAndRotation(entity, buildEntity, entityPos, structure);
}
structure.world.addFreshEntity(entity);
}
}
}
// All entities generated; clear out the list.
StructureEventHandler.entitiesToGenerate.clear();
}
Aggregations