consolidate java structure sending into one method

This commit is contained in:
onebeastchris 2024-04-16 15:42:59 +02:00
parent c34a711aca
commit 5f68dd8749
3 changed files with 55 additions and 79 deletions

View file

@ -27,17 +27,13 @@ package org.geysermc.geyser.translator.protocol.bedrock;
import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockAction; import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockAction;
import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockMode; import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockMode;
import com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror;
import com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetStructureBlockPacket;
import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureBlockType; import org.cloudburstmc.protocol.bedrock.data.structure.StructureBlockType;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureEditorData; import org.cloudburstmc.protocol.bedrock.data.structure.StructureEditorData;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureSettings;
import org.cloudburstmc.protocol.bedrock.packet.StructureBlockUpdatePacket; import org.cloudburstmc.protocol.bedrock.packet.StructureBlockUpdatePacket;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.StructureBlockUtils;
@Translator(packet = StructureBlockUpdatePacket.class) @Translator(packet = StructureBlockUpdatePacket.class)
public class BedrockStructureBlockUpdateTranslator extends PacketTranslator<StructureBlockUpdatePacket> { public class BedrockStructureBlockUpdateTranslator extends PacketTranslator<StructureBlockUpdatePacket> {
@ -45,7 +41,6 @@ public class BedrockStructureBlockUpdateTranslator extends PacketTranslator<Stru
@Override @Override
public void translate(GeyserSession session, StructureBlockUpdatePacket packet) { public void translate(GeyserSession session, StructureBlockUpdatePacket packet) {
StructureEditorData data = packet.getEditorData(); StructureEditorData data = packet.getEditorData();
StructureSettings settings = data.getSettings();
UpdateStructureBlockAction action = UpdateStructureBlockAction.UPDATE_DATA; UpdateStructureBlockAction action = UpdateStructureBlockAction.UPDATE_DATA;
if (packet.isPowered()) { if (packet.isPowered()) {
@ -63,42 +58,8 @@ public class BedrockStructureBlockUpdateTranslator extends PacketTranslator<Stru
default -> UpdateStructureBlockMode.SAVE; default -> UpdateStructureBlockMode.SAVE;
}; };
StructureMirror mirror = switch (data.getSettings().getMirror()) { StructureBlockUtils.sendJavaStructurePacket(session, packet.getBlockPosition(), data.getSettings().getSize(), mode, action, data.getSettings(),
case X -> StructureMirror.FRONT_BACK; data.isBoundingBoxVisible(), data.getName());
case Z -> StructureMirror.LEFT_RIGHT;
default -> StructureMirror.NONE;
};
StructureRotation rotation = switch (settings.getRotation()) {
case ROTATE_90 -> StructureRotation.CLOCKWISE_90;
case ROTATE_180 -> StructureRotation.CLOCKWISE_180;
case ROTATE_270 -> StructureRotation.COUNTERCLOCKWISE_90;
default -> StructureRotation.NONE;
};
Vector3i offset = settings.getOffset();
if (session.getStructureBlockCache().getBedrockOffset() != null) {
offset = settings.getOffset().sub(session.getStructureBlockCache().getBedrockOffset());
}
ServerboundSetStructureBlockPacket structureBlockPacket = new ServerboundSetStructureBlockPacket(
packet.getBlockPosition(),
action,
mode,
data.getName(),
offset,
settings.getSize(),
mirror,
rotation,
"",
settings.getIntegrityValue(),
settings.getIntegritySeed(),
settings.isIgnoringEntities(),
false,
data.isBoundingBoxVisible()
);
session.getStructureBlockCache().clear(); session.getStructureBlockCache().clear();
session.sendDownstreamPacket(structureBlockPacket);
} }
} }

View file

@ -27,14 +27,10 @@ package org.geysermc.geyser.translator.protocol.bedrock;
import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockAction; import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockAction;
import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockMode; import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockMode;
import com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror;
import com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetStructureBlockPacket;
import org.cloudburstmc.math.vector.Vector3i; import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureSettings; import org.cloudburstmc.protocol.bedrock.data.structure.StructureSettings;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureTemplateRequestOperation; import org.cloudburstmc.protocol.bedrock.data.structure.StructureTemplateRequestOperation;
import org.cloudburstmc.protocol.bedrock.packet.StructureTemplateDataRequestPacket; import org.cloudburstmc.protocol.bedrock.packet.StructureTemplateDataRequestPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
@ -79,41 +75,15 @@ public class BedrockStructureTemplateDataRequestTranslator extends PacketTransla
// See the block entity translator for more info // See the block entity translator for more info
session.getStructureBlockCache().setCurrentStructureBlock(packet.getPosition()); session.getStructureBlockCache().setCurrentStructureBlock(packet.getPosition());
StructureRotation rotation = switch (settings.getRotation()) { StructureBlockUtils.sendJavaStructurePacket(session,
case ROTATE_90 -> StructureRotation.CLOCKWISE_90;
case ROTATE_180 -> StructureRotation.CLOCKWISE_180;
case ROTATE_270 -> StructureRotation.COUNTERCLOCKWISE_90;
default -> StructureRotation.NONE;
};
StructureMirror mirror = switch (settings.getMirror()) {
case X -> StructureMirror.FRONT_BACK;
case Z -> StructureMirror.LEFT_RIGHT;
default -> StructureMirror.NONE;
};
Vector3i offset = settings.getOffset();
if (currentStructureName != null && session.getStructureBlockCache().getBedrockOffset() != null) {
offset = offset.sub(session.getStructureBlockCache().getBedrockOffset());
}
ServerboundSetStructureBlockPacket structureBlockPacket = new ServerboundSetStructureBlockPacket(
packet.getPosition(), packet.getPosition(),
UpdateStructureBlockAction.LOAD_STRUCTURE, Vector3i.ZERO, // We expect the Java server to tell us the size
UpdateStructureBlockMode.LOAD, UpdateStructureBlockMode.LOAD,
packet.getName(), UpdateStructureBlockAction.LOAD_STRUCTURE,
offset, settings,
Vector3i.ZERO, // We expect the Java server to tell us the correct size true,
mirror, packet.getName()
rotation,
"",
settings.getIntegrityValue(),
settings.getIntegritySeed(),
settings.isIgnoringEntities(),
false,
true
); );
session.sendDownstreamPacket(structureBlockPacket);
} else { } else {
StructureBlockUtils.sendEmptyStructureData(session); StructureBlockUtils.sendEmptyStructureData(session);
} }

View file

@ -25,6 +25,9 @@
package org.geysermc.geyser.util; package org.geysermc.geyser.util;
import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockAction;
import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockMode;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetStructureBlockPacket;
import org.cloudburstmc.math.vector.Vector3i; import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.nbt.NbtList; import org.cloudburstmc.nbt.NbtList;
import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMap;
@ -32,6 +35,7 @@ import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType; import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureMirror; import org.cloudburstmc.protocol.bedrock.data.structure.StructureMirror;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureRotation; import org.cloudburstmc.protocol.bedrock.data.structure.StructureRotation;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureSettings;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureTemplateResponseType; import org.cloudburstmc.protocol.bedrock.data.structure.StructureTemplateResponseType;
import org.cloudburstmc.protocol.bedrock.packet.StructureTemplateDataResponsePacket; import org.cloudburstmc.protocol.bedrock.packet.StructureTemplateDataResponsePacket;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
@ -82,7 +86,7 @@ public class StructureBlockUtils {
switch (structureMirror) { switch (structureMirror) {
case NONE -> newOffsetX -= sizeZ - 1; case NONE -> newOffsetX -= sizeZ - 1;
case X -> { case X -> {
newOffsetZ -= sizeZ; newOffsetZ -= sizeX - 1;
newOffsetX -= sizeZ - 1; newOffsetX -= sizeZ - 1;
} }
} }
@ -116,4 +120,45 @@ public class StructureBlockUtils {
return Vector3i.from(newOffsetX, 0, newOffsetZ); return Vector3i.from(newOffsetX, 0, newOffsetZ);
} }
public static void sendJavaStructurePacket(GeyserSession session, Vector3i blockPosition, Vector3i size, UpdateStructureBlockMode mode, UpdateStructureBlockAction action,
StructureSettings settings, boolean boundingBoxVisible, String structureName) {
com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror mirror = switch (settings.getMirror()) {
case X -> com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror.FRONT_BACK;
case Z -> com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror.LEFT_RIGHT;
default -> com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror.NONE;
};
com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation rotation = switch (settings.getRotation()) {
case ROTATE_90 -> com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation.CLOCKWISE_90;
case ROTATE_180 -> com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation.CLOCKWISE_180;
case ROTATE_270 -> com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation.COUNTERCLOCKWISE_90;
default -> com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation.NONE;
};
Vector3i offset = settings.getOffset();
if (session.getStructureBlockCache().getBedrockOffset() != null) {
offset = settings.getOffset().sub(session.getStructureBlockCache().getBedrockOffset());
}
ServerboundSetStructureBlockPacket structureBlockPacket = new ServerboundSetStructureBlockPacket(
blockPosition,
action,
mode,
structureName,
offset,
settings.getSize(),
mirror,
rotation,
"",
settings.getIntegrityValue(),
settings.getIntegritySeed(),
settings.isIgnoringEntities(),
false,
boundingBoxVisible
);
session.sendDownstreamPacket(structureBlockPacket);
}
} }