Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to Schematic Command relating to Entities #2623

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void run(ReplaceableTagEvent event) {
// This takes an optional duration as "fake_duration" for how long the fake blocks should remain.
//
// The "create" and "paste" options allow the "entities" argument to be specified - when used, entities will be copied or pasted.
// At current time, entity types included will be: Paintings, ItemFrames, ArmorStands.
// At current time, entity types included will be: Paintings, ItemFrames, GlowItemFrames, ArmorStands, and DisplayEntities.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is DisplayEntities implemented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point... Display Entities were already included in the types to be copied so the meta update for it was just to document what already existed. However after adding display entities to the test schematic I'm seeing them getting updated in unexpected ways, losing transformation data, and text_displays losing text.

After disabling the thrown exception on EntityHelper.setStepHeight (not all entity mechanisms are applied otherwise) it does actually spawn display entities, however rotation transformation is still completely lost - wondering if this might be a v1.20.6 issue? I'm not able to spawn in display entities with pitch/yaw ever applied either.

Want me to remove the DisplayEntities note for the moment?

//
// The "create" option allows the "flags" argument to be specified - when used, block location flags will be copied.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ public static BlockFace rotateFaceOne(BlockFace face) {
case SOUTH_EAST:
return BlockFace.NORTH_EAST;
default:
return BlockFace.SELF;
return face;
}
}

public static HashSet<EntityType> copyTypes = new HashSet<>(Arrays.asList(EntityType.PAINTING, EntityType.ITEM_FRAME, EntityType.ARMOR_STAND));
public static HashSet<EntityType> copyTypes = new HashSet<>(Arrays.asList(EntityType.PAINTING, EntityType.ITEM_FRAME, EntityType.ARMOR_STAND, EntityType.GLOW_ITEM_FRAME));

static {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {
Expand All @@ -218,7 +218,6 @@ public void buildEntities(AreaContainmentObject area, Location center) {
}
MapTag data = new MapTag();
data.putObject("entity", entTag.describe(null));
data.putObject("rotation", new ElementTag(0));
Vector offset = ent.getLocation().toVector().subtract(center.toVector());
data.putObject("offset", new LocationTag((String) null, offset.getX(), offset.getY(), offset.getZ(), ent.getLocation().getYaw(), ent.getLocation().getPitch()));
entities.addObject(data);
Expand All @@ -234,38 +233,17 @@ public void pasteEntities(LocationTag relative) {
for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
try {
LocationTag offset = data.getObjectAs("offset", LocationTag.class, CoreUtilities.noDebugContext);
int rotation = data.getElement("rotation").asInt();
EntityTag entity = data.getObjectAs("entity", EntityTag.class, CoreUtilities.noDebugContext);
if (entity == null || offset == null) {
continue;
}
entity = entity.duplicate();
offset = offset.clone();
if (rotation != 0) {
ArrayList<Mechanism> mechs = new ArrayList<>(entity.getWaitingMechanisms().size());
for (Mechanism mech : entity.getWaitingMechanisms()) {
if (mech.getName().equals("rotation")) {
String rotationName = mech.getValue().asString();
BlockFace face = BlockFace.valueOf(rotationName.toUpperCase());
for (int i = 0; i < rotation; i += 90) {
face = rotateFaceOne(face);
}
offset.add(face.getDirection().multiply(0.1)); // Compensate for hanging locations being very stupid
mechs.add(new Mechanism("rotation", new ElementTag(face), CoreUtilities.noDebugContext));
}
else {
mechs.add(new Mechanism(mech.getName(), mech.value, CoreUtilities.noDebugContext));
}
}
entity.mechanisms = mechs;
}
else {
for (Mechanism mechanism : entity.mechanisms) {
mechanism.context = CoreUtilities.noDebugContext;
}
for (Mechanism mechanism : entity.mechanisms) {
mechanism.context = CoreUtilities.noDebugContext;
}
Location spawnLoc = relative.clone().add(offset);
spawnLoc.setYaw(offset.getYaw() - rotation);
spawnLoc.setYaw(offset.getYaw());
spawnLoc.setPitch(offset.getPitch());
entity.spawnAt(spawnLoc);
}
Expand Down Expand Up @@ -349,15 +327,24 @@ public void rotateEntitiesOne() {
ListTag outEntities = new ListTag();
for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
LocationTag offset = data.getObjectAs("offset", LocationTag.class, CoreUtilities.noDebugContext);
int rotation = data.getElement("rotation").asInt();
offset = new LocationTag((String) null, offset.getZ(), offset.getY(), -offset.getX() + 1, offset.getYaw(), offset.getPitch());
rotation += 90;
while (rotation >= 360) {
rotation -= 360;
EntityTag entity = data.getObjectAs("entity", EntityTag.class, CoreUtilities.noDebugContext);
offset = new LocationTag((String) null, offset.getZ(), offset.getY(), -offset.getX() + 1, normalizeYaw(offset.getYaw() - 90), offset.getPitch());
ArrayList<Mechanism> mechs = new ArrayList<>(entity.getWaitingMechanisms().size());
for (Mechanism mech : entity.getWaitingMechanisms()) {
if (mech.getName().equals("rotation")) {
String rotationName = mech.getValue().asString();
BlockFace face = BlockFace.valueOf(rotationName.toUpperCase());
face = rotateFaceOne(face);
mechs.add(new Mechanism("rotation", new ElementTag(face), CoreUtilities.noDebugContext));
}
else {
mechs.add(new Mechanism(mech.getName(), mech.value, CoreUtilities.noDebugContext));
}
}
entity.mechanisms = mechs;
data = data.duplicate();
data.putObject("offset", offset);
data.putObject("rotation", new ElementTag(rotation));
data.putObject("entity", entity);
outEntities.addObject(data);
}
entities = outEntities;
Expand All @@ -383,39 +370,81 @@ public void rotateOne() {
blocks = bd;
}

public void flipEntities(int offsetMultiplier_X, int offsetMultiplier_Z) {
public void flipEntitiesX() {
if (entities == null) {
return;
}
ListTag outEntities = new ListTag();
for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
int rotation = data.getElement("rotation").asInt();
EntityTag entity = data.getObjectAs("entity", EntityTag.class, CoreUtilities.noDebugContext);
LocationTag offset = data.getObjectAs("offset", LocationTag.class, CoreUtilities.noDebugContext);
float newYaw = offset.getYaw();
if (offsetMultiplier_X == -1) {
newYaw = -1 * (offset.getYaw() - 90) + 270;
} else if (offsetMultiplier_Z == -1) {
newYaw = 180 - offset.getYaw();
offset.setYaw(normalizeYaw(180 - (offset.getYaw() - 90) + 90));
offset.setX(-offset.getX() + 1);
ArrayList<Mechanism> mechs = new ArrayList<>(entity.getWaitingMechanisms().size());
for (Mechanism mech : entity.getWaitingMechanisms()) {
if (mech.getName().equals("rotation")) {
String rotationName = mech.getValue().asString();
BlockFace face = BlockFace.valueOf(rotationName.toUpperCase());
face = FullBlockData.flipFaceX(face);
mechs.add(new Mechanism("rotation", new ElementTag(face), CoreUtilities.noDebugContext));
}
else {
mechs.add(new Mechanism(mech.getName(), mech.value, CoreUtilities.noDebugContext));
}
}
while (newYaw < 0 || newYaw >= 360) {
if (newYaw >= 360) {
newYaw -= 360;
} else {
newYaw += 360;
entity.mechanisms = mechs;
data = data.duplicate();
data.putObject("offset", offset);
data.putObject("entity", entity);
outEntities.addObject(data);
}
entities = outEntities;
}

public void flipEntitiesZ() {
if (entities == null) {
return;
}
ListTag outEntities = new ListTag();
for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
EntityTag entity = data.getObjectAs("entity", EntityTag.class, CoreUtilities.noDebugContext);
LocationTag offset = data.getObjectAs("offset", LocationTag.class, CoreUtilities.noDebugContext);
offset.setZ(-offset.getZ() + 1);
offset.setYaw(normalizeYaw(180 - offset.getYaw()));
ArrayList<Mechanism> mechs = new ArrayList<>(entity.getWaitingMechanisms().size());
for (Mechanism mech : entity.getWaitingMechanisms()) {
if (mech.getName().equals("rotation")) {
String rotationName = mech.getValue().asString();
BlockFace face = BlockFace.valueOf(rotationName.toUpperCase());
face = FullBlockData.flipFaceZ(face);
mechs.add(new Mechanism("rotation", new ElementTag(face), CoreUtilities.noDebugContext));
}
else {
mechs.add(new Mechanism(mech.getName(), mech.value, CoreUtilities.noDebugContext));
}
}
rotation += (offset.getYaw() - newYaw);
offset = new LocationTag((String) null, offset.getX() * offsetMultiplier_X, offset.getY(), offset.getZ() * offsetMultiplier_Z, newYaw, offset.getPitch());
entity.mechanisms = mechs;
data = data.duplicate();
data.putObject("offset", offset);
data.putObject("rotation", new ElementTag(rotation));
data.putObject("entity", entity);
outEntities.addObject(data);
}
entities = outEntities;
}

public float normalizeYaw(float yaw){
while (yaw < 0 || yaw >= 360) {
if (yaw >= 360) {
yaw -= 360;
} else {
yaw += 360;
}
}
return yaw;
}

public void flipX() {
flipEntities(-1, 1);
flipEntitiesX();
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
center_x = x_width - center_x - 1;
Expand Down Expand Up @@ -444,7 +473,7 @@ public void flipY() {
}

public void flipZ() {
flipEntities(1, -1);
flipEntitiesZ();
FullBlockData[] bd = new FullBlockData[blocks.length];
int index = 0;
center_z = z_height - center_z - 1;
Expand Down