|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.translators.java.world; |
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; |
|
import com.github.steveice10.mc.protocol.data.game.world.block.value.*; |
|
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerBlockValuePacket; |
|
import com.nukkitx.math.vector.Vector3i; |
|
import com.nukkitx.nbt.NbtMap; |
|
import com.nukkitx.nbt.NbtMapBuilder; |
|
import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket; |
|
import com.nukkitx.protocol.bedrock.packet.BlockEventPacket; |
|
import org.geysermc.connector.network.session.GeyserSession; |
|
import org.geysermc.connector.network.translators.PacketTranslator; |
|
import org.geysermc.connector.network.translators.Translator; |
|
import org.geysermc.connector.network.translators.world.block.BlockStateValues; |
|
import org.geysermc.connector.network.translators.world.block.entity.NoteblockBlockEntityTranslator; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
@Translator(packet = ServerBlockValuePacket.class) |
|
public class JavaBlockValueTranslator extends PacketTranslator<ServerBlockValuePacket> { |
|
|
|
@Override |
|
public void translate(GeyserSession session, ServerBlockValuePacket packet) { |
|
BlockEventPacket blockEventPacket = new BlockEventPacket(); |
|
blockEventPacket.setBlockPosition(Vector3i.from(packet.getPosition().getX(), |
|
packet.getPosition().getY(), packet.getPosition().getZ())); |
|
if (packet.getValue() instanceof ChestValue) { |
|
ChestValue value = (ChestValue) packet.getValue() ; |
|
blockEventPacket.setEventType(1); |
|
blockEventPacket.setEventData(value.getViewers() > 0 ? 1 : 0); |
|
session.sendUpstreamPacket(blockEventPacket); |
|
} else if (packet.getValue() instanceof EndGatewayValue) { |
|
blockEventPacket.setEventType(1); |
|
session.sendUpstreamPacket(blockEventPacket); |
|
} else if (packet.getValue() instanceof NoteBlockValue) { |
|
NoteblockBlockEntityTranslator.translate(session, packet.getPosition()); |
|
} else if (packet.getValue() instanceof PistonValue) { |
|
PistonValueType type = (PistonValueType) packet.getType(); |
|
|
|
|
|
|
|
Vector3i position = Vector3i.from(packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ()); |
|
if (type == PistonValueType.PUSHING) { |
|
extendPiston(session, position, 0.0f, 0.0f); |
|
} else { |
|
retractPiston(session, position, 1.0f, 1.0f); |
|
} |
|
} else if (packet.getValue() instanceof MobSpawnerValue) { |
|
blockEventPacket.setEventType(1); |
|
session.sendUpstreamPacket(blockEventPacket); |
|
} else if (packet.getValue() instanceof EndGatewayValue) { |
|
blockEventPacket.setEventType(1); |
|
session.sendUpstreamPacket(blockEventPacket); |
|
} else if (packet.getValue() instanceof GenericBlockValue && packet.getBlockId() == BlockStateValues.JAVA_BELL_ID) { |
|
|
|
GenericBlockValue bellValue = (GenericBlockValue) packet.getValue(); |
|
Position position = packet.getPosition(); |
|
|
|
BlockEntityDataPacket blockEntityPacket = new BlockEntityDataPacket(); |
|
blockEntityPacket.setBlockPosition(Vector3i.from(position.getX(), position.getY(), position.getZ())); |
|
|
|
NbtMapBuilder builder = NbtMap.builder(); |
|
builder.putInt("x", position.getX()); |
|
builder.putInt("y", position.getY()); |
|
builder.putInt("z", position.getZ()); |
|
builder.putString("id", "Bell"); |
|
int bedrockRingDirection; |
|
switch (bellValue.getValue()) { |
|
case 3: |
|
bedrockRingDirection = 0; |
|
break; |
|
case 4: |
|
bedrockRingDirection = 1; |
|
break; |
|
case 5: |
|
bedrockRingDirection = 3; |
|
break; |
|
default: |
|
bedrockRingDirection = bellValue.getValue(); |
|
} |
|
builder.putInt("Direction", bedrockRingDirection); |
|
builder.putByte("Ringing", (byte) 1); |
|
builder.putInt("Ticks", 0); |
|
|
|
blockEntityPacket.setData(builder.build()); |
|
session.sendUpstreamPacket(blockEntityPacket); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void extendPiston(GeyserSession session, Vector3i position, float progress, float lastProgress) { |
|
BlockEntityDataPacket blockEntityDataPacket = new BlockEntityDataPacket(); |
|
blockEntityDataPacket.setBlockPosition(position); |
|
byte state = (byte) ((progress == 1.0f && lastProgress == 1.0f) ? 2 : 1); |
|
blockEntityDataPacket.setData(buildPistonTag(position, progress, lastProgress, state)); |
|
session.sendUpstreamPacket(blockEntityDataPacket); |
|
if (lastProgress != 1.0f) { |
|
session.getConnector().getGeneralThreadPool().schedule(() -> |
|
extendPiston(session, position, (progress >= 1.0f) ? 1.0f : progress + 0.5f, progress), |
|
20, TimeUnit.MILLISECONDS); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void retractPiston(GeyserSession session, Vector3i position, float progress, float lastProgress) { |
|
BlockEntityDataPacket blockEntityDataPacket = new BlockEntityDataPacket(); |
|
blockEntityDataPacket.setBlockPosition(position); |
|
byte state = (byte) ((progress == 0.0f && lastProgress == 0.0f) ? 0 : 3); |
|
blockEntityDataPacket.setData(buildPistonTag(position, progress, lastProgress, state)); |
|
session.sendUpstreamPacket(blockEntityDataPacket); |
|
if (lastProgress != 0.0f) { |
|
session.getConnector().getGeneralThreadPool().schedule(() -> |
|
retractPiston(session, position, (progress <= 0.0f) ? 0.0f : progress - 0.5f, progress), |
|
20, TimeUnit.MILLISECONDS); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private NbtMap buildPistonTag(Vector3i position, float progress, float lastProgress, byte state) { |
|
NbtMapBuilder builder = NbtMap.builder() |
|
.putInt("x", position.getX()) |
|
.putInt("y", position.getY()) |
|
.putInt("z", position.getZ()) |
|
.putFloat("Progress", progress) |
|
.putFloat("LastProgress", lastProgress) |
|
.putString("id", "PistonArm") |
|
.putByte("NewState", state) |
|
.putByte("State", state); |
|
return builder.build(); |
|
} |
|
} |
|
|