File size: 8,665 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
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();
// Unlike everything else, pistons need a block entity packet to convey motion
// TODO: Doesn't register on chunk load; needs to be interacted with first
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) {
// Bells - needed to show ring from other players
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: // north
bedrockRingDirection = 0;
break;
case 4: // east
bedrockRingDirection = 1;
break;
case 5: // west
bedrockRingDirection = 3;
break;
default: // south (2) is identical
bedrockRingDirection = bellValue.getValue();
}
builder.putInt("Direction", bedrockRingDirection);
builder.putByte("Ringing", (byte) 1);
builder.putInt("Ticks", 0);
blockEntityPacket.setData(builder.build());
session.sendUpstreamPacket(blockEntityPacket);
}
}
/**
* Emulating a piston extending
* @param session GeyserSession
* @param position Block position
* @param progress How far the piston is
* @param lastProgress How far the piston last was
*/
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);
}
}
/**
* Emulate a piston retracting.
* @param session GeyserSession
* @param position Block position
* @param progress Current progress of piston
* @param lastProgress Last progress of piston
*/
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);
}
}
/**
* Build a piston tag
* @param position Piston position
* @param progress Current progress of piston
* @param lastProgress Last progress of piston
* @param state
* @return Bedrock CompoundTag of piston
*/
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();
}
}
|