use of net.minecraft.world.entity.decoration.HangingEntity in project SpongeCommon by SpongePowered.
the class LevelMixin method bridge$createEntity.
@Override
@SuppressWarnings("unchecked")
public <E extends org.spongepowered.api.entity.Entity> E bridge$createEntity(final EntityType<E> type, final Vector3d position, final boolean naturally) throws IllegalArgumentException, IllegalStateException {
if (type == net.minecraft.world.entity.EntityType.PLAYER) {
// Unable to construct these
throw new IllegalArgumentException("A Player cannot be created by the API!");
}
net.minecraft.world.entity.Entity entity = null;
final double x = position.x();
final double y = position.y();
final double z = position.z();
final net.minecraft.world.level.Level thisWorld = (net.minecraft.world.level.Level) (Object) this;
// Not all entities have a single World parameter as their constructor
if (type == net.minecraft.world.entity.EntityType.LIGHTNING_BOLT) {
entity = net.minecraft.world.entity.EntityType.LIGHTNING_BOLT.create(thisWorld);
entity.moveTo(x, y, z);
((LightningBolt) entity).setVisualOnly(false);
}
// TODO - archetypes should solve the problem of calling the correct constructor
if (type == net.minecraft.world.entity.EntityType.ENDER_PEARL) {
final ArmorStand tempEntity = new ArmorStand(thisWorld, x, y, z);
tempEntity.setPos(tempEntity.getX(), tempEntity.getY() - tempEntity.getEyeHeight(), tempEntity.getZ());
entity = new ThrownEnderpearl(thisWorld, tempEntity);
((EnderPearl) entity).offer(Keys.SHOOTER, UnknownProjectileSource.UNKNOWN);
}
// set them is to use the more specialised constructor).
if (type == net.minecraft.world.entity.EntityType.FALLING_BLOCK) {
entity = new FallingBlockEntity(thisWorld, x, y, z, Blocks.SAND.defaultBlockState());
}
if (type == net.minecraft.world.entity.EntityType.ITEM) {
entity = new ItemEntity(thisWorld, x, y, z, new ItemStack(Blocks.STONE));
}
if (entity == null) {
final ResourceKey key = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) type);
try {
entity = ((net.minecraft.world.entity.EntityType) type).create(thisWorld);
entity.moveTo(x, y, z);
} catch (final Exception e) {
throw new RuntimeException("There was an issue attempting to construct " + key, e);
}
}
if (entity instanceof HangingEntity) {
if (!((HangingEntity) entity).survives()) {
throw new IllegalArgumentException("Hanging entity does not survive at the given position: " + position);
}
}
if (naturally && entity instanceof Mob) {
// Adding the default equipment
final DifficultyInstance difficulty = this.shadow$getCurrentDifficultyAt(new BlockPos(x, y, z));
((MobAccessor) entity).invoker$populateDefaultEquipmentSlots(difficulty);
}
if (entity instanceof Painting) {
// This is default when art is null when reading from NBT, could
// choose a random art instead?
((Painting) entity).motive = Motive.KEBAB;
}
return (E) entity;
}
use of net.minecraft.world.entity.decoration.HangingEntity 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.HangingEntity in project MC-Prefab by Brian-Wuest.
the class StructureEventHandler method setItemFrameFacingAndRotation.
private static Entity setItemFrameFacingAndRotation(ItemFrame frame, BuildEntity buildEntity, BlockPos entityPos, Structure structure) {
float yaw = frame.getYRot();
Rotation rotation = Rotation.NONE;
double x_axis_offset = buildEntity.entityXAxisOffset;
double z_axis_offset = buildEntity.entityZAxisOffset;
Direction facing = frame.getDirection();
double y_axis_offset = buildEntity.entityYAxisOffset;
x_axis_offset = x_axis_offset * -1;
z_axis_offset = z_axis_offset * -1;
Direction structureDirection = structure.getClearSpace().getShape().getDirection();
Direction configurationDirection = structure.configuration.houseFacing.getOpposite();
if (facing != Direction.UP && facing != Direction.DOWN) {
if (configurationDirection == structureDirection.getOpposite()) {
rotation = Rotation.CLOCKWISE_180;
facing = facing.getOpposite();
} else if (configurationDirection == structureDirection.getClockWise()) {
rotation = Rotation.CLOCKWISE_90;
facing = facing.getClockWise();
} else if (configurationDirection == structureDirection.getCounterClockWise()) {
rotation = Rotation.COUNTERCLOCKWISE_90;
facing = facing.getCounterClockWise();
} else {
x_axis_offset = 0;
z_axis_offset = 0;
}
}
yaw = frame.rotate(rotation);
CompoundTag compound = new CompoundTag();
((HangingEntity) frame).addAdditionalSaveData(compound);
compound.putByte("Facing", (byte) facing.get3DDataValue());
((HangingEntity) frame).readAdditionalSaveData(compound);
StructureEventHandler.updateEntityHangingBoundingBox(frame);
frame.moveTo(entityPos.getX() + x_axis_offset, entityPos.getY() + y_axis_offset, entityPos.getZ() + z_axis_offset, yaw, frame.getXRot());
StructureEventHandler.updateEntityHangingBoundingBox(frame);
ChunkAccess chunk = structure.world.getChunkAt(entityPos);
chunk.setUnsaved(true);
return frame;
}
use of net.minecraft.world.entity.decoration.HangingEntity in project MC-Prefab by Brian-Wuest.
the class StructureEventHandler method onServerTick.
/**
* This event is primarily used to build 100 blocks for any queued structures for all players.
*
* @param event The event object.
*/
@SubscribeEvent
public static void onServerTick(ServerTickEvent event) {
if (event.phase == TickEvent.Phase.START) {
ArrayList<Player> playersToRemove = new ArrayList<>();
if (StructureEventHandler.entitiesToGenerate.size() > 0) {
StructureEventHandler.ticksSinceLastEntitiesGenerated++;
if (StructureEventHandler.ticksSinceLastEntitiesGenerated > 40) {
// Process any entities.
StructureEventHandler.processStructureEntities();
StructureEventHandler.ticksSinceLastEntitiesGenerated = 0;
}
}
if (StructureEventHandler.structuresToBuild.size() > 0) {
for (Entry<Player, ArrayList<Structure>> entry : StructureEventHandler.structuresToBuild.entrySet()) {
ArrayList<Structure> structuresToRemove = new ArrayList<>();
// Build the first 100 blocks of each structure for this player.
for (Structure structure : entry.getValue()) {
if (!structure.entitiesRemoved) {
// If there is a player there...they will probably die anyways.....
for (BlockPos clearedPos : structure.clearedBlockPos) {
AABB axisPos = Shapes.block().bounds().move(clearedPos);
List<Entity> list = structure.world.getEntities(null, axisPos);
if (!list.isEmpty()) {
for (Entity entity : list) {
// Don't kill living entities.
if (!(entity instanceof LivingEntity)) {
if (entity instanceof HangingEntity) {
structure.BeforeHangingEntityRemoved((HangingEntity) entity);
}
structure.world.removeEntity(entity, false);
}
}
}
}
structure.entitiesRemoved = true;
}
if (structure.airBlocks.size() > 0) {
structure.hasAirBlocks = true;
}
for (int i = 0; i < 10; i++) {
i = StructureEventHandler.setBlock(i, structure, structuresToRemove);
}
// After building the blocks for this tick, find waterlogged blocks and remove them.
StructureEventHandler.removeWaterLogging(structure);
}
// Update the list of structures to remove this structure since it's done building.
StructureEventHandler.removeStructuresFromList(structuresToRemove, entry);
if (entry.getValue().size() == 0) {
playersToRemove.add(entry.getKey());
}
}
}
// Remove each player that has their structure's built.
for (Player player : playersToRemove) {
StructureEventHandler.structuresToBuild.remove(player);
}
}
}
use of net.minecraft.world.entity.decoration.HangingEntity in project MC-Prefab by Brian-Wuest.
the class StructureEventHandler method setPaintingFacingAndRotation.
private static Entity setPaintingFacingAndRotation(Painting entity, BuildEntity buildEntity, BlockPos entityPos, Structure structure) {
float yaw = entity.getYRot();
Rotation rotation = Rotation.NONE;
double x_axis_offset = 0;
double z_axis_offset = 0;
Direction facing = entity.getDirection();
double y_axis_offset = buildEntity.entityYAxisOffset * -1;
Direction structureDirection = structure.getClearSpace().getShape().getDirection();
Direction configurationDirection = structure.configuration.houseFacing.getOpposite();
if (configurationDirection == structureDirection.getOpposite()) {
rotation = Rotation.CLOCKWISE_180;
facing = facing.getOpposite();
} else if (configurationDirection == structureDirection.getClockWise()) {
rotation = Rotation.CLOCKWISE_90;
facing = facing.getClockWise();
} else if (configurationDirection == structureDirection.getCounterClockWise()) {
rotation = Rotation.COUNTERCLOCKWISE_90;
facing = facing.getCounterClockWise();
}
int paintingBlockWidth = entity.motive.getWidth() / 16;
int paintingBlockHeight = entity.motive.getHeight() / 16;
if ((paintingBlockHeight > paintingBlockWidth || paintingBlockHeight > 1) && !(paintingBlockWidth == 4 && paintingBlockHeight == 3)) {
y_axis_offset--;
}
yaw = entity.rotate(rotation);
CompoundTag compound = new CompoundTag();
((HangingEntity) entity).addAdditionalSaveData(compound);
compound.putByte("Facing", (byte) facing.get2DDataValue());
((HangingEntity) entity).readAdditionalSaveData(compound);
StructureEventHandler.updateEntityHangingBoundingBox(entity);
entity.moveTo(entityPos.getX() + x_axis_offset, entityPos.getY() + y_axis_offset, entityPos.getZ() + z_axis_offset, yaw, entity.getXRot());
StructureEventHandler.updateEntityHangingBoundingBox(entity);
ChunkAccess chunk = structure.world.getChunkAt(entityPos);
chunk.setUnsaved(true);
return entity;
}
Aggregations