Skip to content

Commit

Permalink
Compiles; provide your own mappings for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed May 25, 2022
1 parent 5339127 commit bbf45b6
Show file tree
Hide file tree
Showing 30 changed files with 310 additions and 132 deletions.
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
</dependency>
<dependency>
<groupId>com.github.CloudburstMC.Protocol</groupId>
<artifactId>bedrock-v503</artifactId>
<version>297567d</version>
<artifactId>bedrock-beta</artifactId>
<version>49323e0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void execute(GeyserSession session, CommandSender sender, String[] args)
}

ServerboundPlayerActionPacket releaseItemPacket = new ServerboundPlayerActionPacket(PlayerAction.SWAP_HANDS, BlockUtils.POSITION_ZERO,
Direction.DOWN);
Direction.DOWN, session.getNextSequence());
session.sendDownstreamPacket(releaseItemPacket);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public final class EntityDefinitions {
.type(EntityType.LLAMA_SPIT)
.heightAndWidth(0.25f)
.build();
PAINTING = EntityDefinition.<PaintingEntity>inherited(null, entityBase)
PAINTING = EntityDefinition.inherited(PaintingEntity::new, entityBase)
.type(EntityType.PAINTING)
.build();
SHULKER_BULLET = EntityDefinition.inherited(ThrowableEntity::new, entityBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,27 @@

import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.packet.AddPaintingPacket;
import org.geysermc.geyser.entity.EntityDefinitions;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.entity.EntityDefinition;
import org.geysermc.geyser.level.PaintingType;
import org.geysermc.geyser.session.GeyserSession;

import java.util.UUID;

// TODO 1.19
public class PaintingEntity extends Entity {
private static final double OFFSET = -0.46875;
private final PaintingType paintingName;
private final int direction;
private PaintingType paintingName;
private int direction;

public PaintingEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, Vector3f position, PaintingType paintingName, int direction) {
super(session, entityId, geyserId, uuid, EntityDefinitions.PAINTING, position, Vector3f.ZERO, 0f, 0f, 0f);
this.paintingName = paintingName;
this.direction = direction;
public PaintingEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
}

@Override
public void spawnEntity() {
// Wait until we get the metadata needed
}

public void paintingtodo() {
AddPaintingPacket addPaintingPacket = new AddPaintingPacket();
addPaintingPacket.setUniqueEntityId(geyserId);
addPaintingPacket.setRuntimeEntityId(geyserId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setImmuneToZombification(BooleanEntityMetadata entityMetadata) {

@Override
protected boolean isShaking() {
return (!isImmuneToZombification && !session.isDimensionPiglinSafe()) || super.isShaking();
return (!isImmuneToZombification && !session.getDimensionType().piglinSafe()) || super.isShaking();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public void setImmuneToZombification(BooleanEntityMetadata entityMetadata) {

@Override
protected boolean isShaking() {
return (!isImmuneToZombification && !session.isDimensionPiglinSafe()) || super.isShaking();
return (!isImmuneToZombification && !session.getDimensionType().piglinSafe()) || super.isShaking();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@

import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.game.setting.Difficulty;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtMapBuilder;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.geysermc.geyser.level.block.BlockStateValues;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.ChunkCache;
import org.geysermc.geyser.translator.inventory.LecternInventoryTranslator;
import org.geysermc.geyser.level.block.BlockStateValues;

import java.util.Locale;

Expand Down Expand Up @@ -83,7 +82,7 @@ public boolean shouldExpectLecternHandled() {

@Override
public void setGameRule(GeyserSession session, String name, Object value) {
session.sendDownstreamPacket(new ServerboundChatPacket("/gamerule " + name + " " + value));
session.sendCommand("gamerule " + name + " " + value);
gameruleCache.put(name, String.valueOf(value));
}

Expand All @@ -109,12 +108,12 @@ public int getGameRuleInt(GeyserSession session, GameRule gameRule) {

@Override
public void setPlayerGameMode(GeyserSession session, GameMode gameMode) {
session.sendDownstreamPacket(new ServerboundChatPacket("/gamemode " + gameMode.name().toLowerCase(Locale.ROOT)));
session.sendCommand("gamemode " + gameMode.name().toLowerCase(Locale.ROOT));
}

@Override
public void setDifficulty(GeyserSession session, Difficulty difficulty) {
session.sendDownstreamPacket(new ServerboundChatPacket("/difficulty " + difficulty.name().toLowerCase(Locale.ROOT)));
session.sendCommand("difficulty " + difficulty.name().toLowerCase(Locale.ROOT));
}

@Override
Expand Down
24 changes: 19 additions & 5 deletions core/src/main/java/org/geysermc/geyser/level/JavaDimension.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,31 @@
package org.geysermc.geyser.level;

import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import org.geysermc.geyser.util.JavaCodecEntry;

import java.util.Map;

/**
* Represents the information we store from the current Java dimension
* @param piglinSafe Whether piglins and hoglins are safe from conversion in this dimension.
* This controls if they have the shaking effect applied in the dimension.
*/
public record JavaDimension(int minY, int maxY, boolean piglinSafe) {
public record JavaDimension(int minY, int maxY, boolean piglinSafe, double worldCoordinateScale) {

public static void load(CompoundTag tag, Map<String, JavaDimension> map) {
for (CompoundTag dimension : JavaCodecEntry.iterateOverTag(tag.get("minecraft:dimension_type"))) {
CompoundTag elements = dimension.get("element");
int minY = ((IntTag) elements.get("min_y")).getValue();
int maxY = ((IntTag) elements.get("height")).getValue();
// Logical height can be ignored probably - seems to be for artificial limits like the Nether.

// Set if piglins/hoglins should shake
boolean piglinSafe = ((Number) elements.get("piglin_safe").getValue()).byteValue() != (byte) 0;
// Load world coordinate scale for the world border
double coordinateScale = ((Number) elements.get("coordinate_scale").getValue()).doubleValue();

public static JavaDimension load(CompoundTag tag) {
// int minY = ((IntTag) dimensionTag.get("min_y")).getValue();
// int maxY = ((IntTag) dimensionTag.get("height")).getValue();
return new JavaDimension(0, 0, false);
map.put((String) dimension.get("name").getValue(), new JavaDimension(minY, maxY, piglinSafe, coordinateScale));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.github.steveice10.mc.protocol.codec.MinecraftCodec;
import com.github.steveice10.mc.protocol.codec.PacketCodec;
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
import com.nukkitx.protocol.bedrock.v475.Bedrock_v475;
import com.nukkitx.protocol.bedrock.v486.Bedrock_v486;
import com.nukkitx.protocol.bedrock.v503.Bedrock_v503;

import java.util.ArrayList;
Expand Down Expand Up @@ -58,10 +56,6 @@ public final class MinecraftProtocol {
private static final PacketCodec DEFAULT_JAVA_CODEC = MinecraftCodec.CODEC;

static {
SUPPORTED_BEDROCK_CODECS.add(Bedrock_v475.V475_CODEC.toBuilder().minecraftVersion("1.18.0/1.18.1/1.18.2").build());
SUPPORTED_BEDROCK_CODECS.add(Bedrock_v486.V486_CODEC.toBuilder()
.minecraftVersion("1.18.10/1.18.12") // 1.18.11 is also supported, but was only on Switch and since that auto-updates it's not needed
.build());
SUPPORTED_BEDROCK_CODECS.add(DEFAULT_BEDROCK_CODEC.toBuilder()
.minecraftVersion("1.18.30/1.18.31")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ private static void registerBedrockBlocks() {

int bedrockRuntimeId = blockStateOrderedMap.getOrDefault(buildBedrockState(entry.getValue(), stateVersion, stateMapper), -1);
if (bedrockRuntimeId == -1) {
throw new RuntimeException("Unable to find " + javaId + " Bedrock runtime ID! Built NBT tag: \n" +
buildBedrockState(entry.getValue(), stateVersion, stateMapper));
bedrockRuntimeId = 0;
//TODO remove
//throw new RuntimeException("Unable to find " + javaId + " Bedrock runtime ID! Built NBT tag: \n" +
// buildBedrockState(entry.getValue(), stateVersion, stateMapper));
}

switch (javaId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ public static void populate() {

int bedrockId = bedrockIdentifierToId.getInt(bedrockIdentifier);
if (bedrockId == Short.MIN_VALUE) {
throw new RuntimeException("Missing Bedrock ID in mappings: " + bedrockIdentifier);
bedrockId = 0;
//TODO remove
//throw new RuntimeException("Missing Bedrock ID in mappings: " + bedrockIdentifier);
}
int stackSize = mappingItem.getStackSize();

Expand Down
37 changes: 33 additions & 4 deletions core/src/main/java/org/geysermc/geyser/session/GeyserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.ProtocolState;
import com.github.steveice10.mc.protocol.data.UnexpectedEncryptionException;
import com.github.steveice10.mc.protocol.data.game.MessageType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose;
import com.github.steveice10.mc.protocol.data.game.entity.object.Direction;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
Expand All @@ -47,6 +48,8 @@
import com.github.steveice10.mc.protocol.data.game.statistic.CustomStatistic;
import com.github.steveice10.mc.protocol.data.game.statistic.Statistic;
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundChatPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundClientInformationPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundMovePlayerPosPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerAbilitiesPacket;
Expand All @@ -69,6 +72,7 @@
import com.nukkitx.protocol.bedrock.packet.*;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import it.unimi.dsi.fastutil.bytes.ByteArrays;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
Expand All @@ -81,6 +85,7 @@
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.common.value.qual.IntRange;
import org.geysermc.common.PlatformType;
import org.geysermc.cumulus.Form;
Expand Down Expand Up @@ -125,6 +130,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -319,11 +325,16 @@ public class GeyserSession implements GeyserConnection, CommandSender {
*/
@Setter
private String dimension = DimensionUtils.OVERWORLD;
@MonotonicNonNull
@Setter
private JavaDimension dimensionType = null;
/**
* All dimensions that the client could possibly connect to.
*/
private final Map<String, JavaDimension> dimensions = new Object2ObjectOpenHashMap<>(3);

private final Map<MessageType, Object> chatTypes = new EnumMap<>(MessageType.class);

@Setter
private int breakingBlock;

Expand Down Expand Up @@ -1260,9 +1271,9 @@ private boolean attemptToBlock() {

ServerboundUseItemPacket useItemPacket;
if (playerInventory.getItemInHand().getJavaId() == shield.getJavaId()) {
useItemPacket = new ServerboundUseItemPacket(Hand.MAIN_HAND, 0); //TODO
useItemPacket = new ServerboundUseItemPacket(Hand.MAIN_HAND, getNextSequence());
} else if (playerInventory.getOffhand().getJavaId() == shield.getJavaId()) {
useItemPacket = new ServerboundUseItemPacket(Hand.OFF_HAND, 0);
useItemPacket = new ServerboundUseItemPacket(Hand.OFF_HAND, getNextSequence());
} else {
// No blocking
return false;
Expand Down Expand Up @@ -1291,7 +1302,7 @@ public void activateArmAnimationTicking() {
private boolean disableBlocking() {
if (playerEntity.getFlag(EntityFlag.BLOCKING)) {
ServerboundPlayerActionPacket releaseItemPacket = new ServerboundPlayerActionPacket(PlayerAction.RELEASE_USE_ITEM,
BlockUtils.POSITION_ZERO, Direction.DOWN, 0); //TODO
BlockUtils.POSITION_ZERO, Direction.DOWN, getNextSequence());
sendDownstreamPacket(releaseItemPacket);
playerEntity.setFlag(EntityFlag.BLOCKING, false);
return true;
Expand Down Expand Up @@ -1357,7 +1368,21 @@ public boolean isConsole() {
@Override
public String getLocale() {
return clientData.getLanguageCode();
}
}

/**
* Sends a chat message to the Java server.
*/
public void sendChat(String message) {
sendDownstreamPacket(new ServerboundChatPacket(message, Instant.now().toEpochMilli(), 0L, ByteArrays.EMPTY_ARRAY, false));
}

/**
* Sends a command to the Java server.
*/
public void sendCommand(String command) {
sendDownstreamPacket(new ServerboundChatCommandPacket(command, Instant.now().toEpochMilli(), 0L, Collections.emptyMap(), false));
}

public void setServerRenderDistance(int renderDistance) {
renderDistance = GenericMath.ceil(++renderDistance * MathUtils.SQRT_OF_TWO); //square to circle
Expand Down Expand Up @@ -1636,6 +1661,10 @@ public void sendJavaClientSettings() {
sendDownstreamPacket(clientSettingsPacket);
}

public int getNextSequence() {
return 0;
}

/**
* Used for updating statistic values since we only get changes from the server
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public TagCache() {
}

public void loadPacket(GeyserSession session, ClientboundUpdateTagsPacket packet) {
System.out.println(packet);
Map<String, int[]> blockTags = packet.getTags().get("minecraft:block");
this.leaves = IntList.of(blockTags.get("minecraft:leaves"));
this.wool = IntList.of(blockTags.get("minecraft:wool"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.InventoryUtils;

import java.util.OptionalInt;

public class BeaconInventoryTranslator extends AbstractBlockInventoryTranslator {
public BeaconInventoryTranslator() {
super(1, new BlockInventoryHolder("minecraft:beacon", com.nukkitx.protocol.bedrock.data.inventory.ContainerType.BEACON) {
Expand Down Expand Up @@ -111,11 +113,15 @@ protected boolean shouldHandleRequestFirst(StackRequestActionData action, Invent
public ItemStackResponsePacket.Response translateSpecialRequest(GeyserSession session, Inventory inventory, ItemStackRequest request) {
// Input a beacon payment
BeaconPaymentStackRequestActionData beaconPayment = (BeaconPaymentStackRequestActionData) request.getActions()[0];
ServerboundSetBeaconPacket packet = new ServerboundSetBeaconPacket(beaconPayment.getPrimaryEffect(), beaconPayment.getSecondaryEffect());
ServerboundSetBeaconPacket packet = new ServerboundSetBeaconPacket(toJava(beaconPayment.getPrimaryEffect()), toJava(beaconPayment.getSecondaryEffect()));
session.sendDownstreamPacket(packet);
return acceptRequest(request, makeContainerEntries(session, inventory, IntSets.emptySet()));
}

private OptionalInt toJava(int effectChoice) {
return effectChoice == -1 ? OptionalInt.empty() : OptionalInt.of(effectChoice);
}

@Override
public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) {
if (slotInfoData.getContainer() == ContainerSlotType.BEACON_PAYMENT) {
Expand Down
Loading

0 comments on commit bbf45b6

Please sign in to comment.