Skip to content

Commit

Permalink
Merge pull request #4 from GeyserMC/master
Browse files Browse the repository at this point in the history
up
  • Loading branch information
zimzaza4 authored Dec 9, 2021
2 parents d7abe86 + d5e69e5 commit 3b45559
Show file tree
Hide file tree
Showing 25 changed files with 324 additions and 106 deletions.
10 changes: 7 additions & 3 deletions bootstrap/standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</parent>
<artifactId>bootstrap-standalone</artifactId>

<properties>
<log4j.version>2.15.0-20211206.182754-101</log4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.geysermc</groupId>
Expand Down Expand Up @@ -58,17 +62,17 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.1</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
<version>2.13.1</version>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<dependency>
<groupId>com.github.GeyserMC</groupId>
<artifactId>MCProtocolLib</artifactId>
<version>6970991</version>
<version>a991afe</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public static void updateTag(GeyserSession session, Entity interactEntity) {
ItemMapping mapping = session.getPlayerInventory().getItemInHand().getMapping(session);
String javaIdentifierStripped = mapping.getJavaIdentifier().replace("minecraft:", "");
EntityType entityType = interactEntity.getDefinition().entityType();
if (entityType == null) {
// Likely a technical entity; we don't need to worry about this
return;
}

InteractiveTag interactiveTag = InteractiveTag.NONE;

Expand All @@ -90,7 +94,7 @@ public static void updateTag(GeyserSession session, Entity interactEntity) {
// This animal can be fed
interactiveTag = InteractiveTag.FEED;
} else {
switch (interactEntity.getDefinition().entityType()) {
switch (entityType) {
case BOAT:
if (interactEntity.getPassengers().size() < 2) {
interactiveTag = InteractiveTag.BOARD_BOAT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.github.steveice10.mc.protocol.data.game.inventory.VillagerTrade;
import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory.ClientboundMerchantOffersPacket;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.geyser.entity.type.Entity;
Expand All @@ -36,6 +37,7 @@
public class MerchantContainer extends Container {
private Entity villager;
private VillagerTrade[] villagerTrades;
private ClientboundMerchantOffersPacket pendingOffersPacket;

public MerchantContainer(String title, int id, int size, ContainerType containerType, PlayerInventory playerInventory) {
super(title, id, size, containerType, playerInventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,32 @@ public BedrockPong onQuery(InetSocketAddress inetSocketAddress) {
}

@Override
public void onSessionCreation(BedrockServerSession bedrockServerSession) {
bedrockServerSession.setLogging(true);
bedrockServerSession.setCompressionLevel(geyser.getConfig().getBedrock().getCompressionLevel());
bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(geyser, new GeyserSession(geyser, bedrockServerSession, eventLoopGroup.next())));
// Set the packet codec to default just in case we need to send disconnect packets.
bedrockServerSession.setPacketCodec(MinecraftProtocol.DEFAULT_BEDROCK_CODEC);
public void onSessionCreation(@Nonnull BedrockServerSession bedrockServerSession) {
try {
bedrockServerSession.setPacketCodec(MinecraftProtocol.DEFAULT_BEDROCK_CODEC);
bedrockServerSession.setLogging(true);
bedrockServerSession.setCompressionLevel(geyser.getConfig().getBedrock().getCompressionLevel());
bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(geyser, new GeyserSession(geyser, bedrockServerSession, eventLoopGroup.next())));
// Set the packet codec to default just in case we need to send disconnect packets.
} catch (Throwable e) {
// Error must be caught or it will be swallowed
geyser.getLogger().error("Error occurred while initializing player!", e);
bedrockServerSession.disconnect(e.getMessage());
}
}

@Override
public void onUnhandledDatagram(@Nonnull ChannelHandlerContext ctx, DatagramPacket packet) {
ByteBuf content = packet.content();
if (QueryPacketHandler.isQueryPacket(content)) {
new QueryPacketHandler(geyser, packet.sender(), content);
public void onUnhandledDatagram(@Nonnull ChannelHandlerContext ctx, @Nonnull DatagramPacket packet) {
try {
ByteBuf content = packet.content();
if (QueryPacketHandler.isQueryPacket(content)) {
new QueryPacketHandler(geyser, packet.sender(), content);
}
} catch (Throwable e) {
// Error must be caught or it will be swallowed
if (geyser.getConfig().isDebugMode()) {
geyser.getLogger().error("Error occurred during unhandled datagram!", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public boolean handle(LoginPacket loginPacket) {

LoginEncryptionUtils.encryptPlayerConnection(session, loginPacket);

if (session.isClosed()) {
// Can happen if Xbox validation fails
return true;
}

PlayStatusPacket playStatus = new PlayStatusPacket();
playStatus.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS);
session.sendUpstreamPacket(playStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public class BlockRegistries {
*/
public static final MappedRegistry<String, Integer, Object2IntBiMap<String>> JAVA_IDENTIFIERS = MappedRegistry.create(RegistryLoaders.empty(Object2IntBiMap::new));

/**
* A registry which stores unique Java IDs to its clean identifier
* This is used in the statistics form.
*/
public static final ArrayRegistry<String> CLEAN_JAVA_IDENTIFIERS = ArrayRegistry.create(RegistryLoaders.empty(() -> new String[] {}));

/**
* A registry containing all the waterlogged blockstates.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.github.steveice10.mc.protocol.data.game.level.particle.ParticleType;
import com.github.steveice10.mc.protocol.data.game.recipe.Recipe;
import com.github.steveice10.mc.protocol.data.game.recipe.RecipeType;
import com.github.steveice10.mc.protocol.data.game.statistic.CustomStatistic;
import com.github.steveice10.packetlib.packet.Packet;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.protocol.bedrock.BedrockPacket;
Expand Down Expand Up @@ -61,6 +62,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.IntFunction;

/**
* Holds all the common registries in Geyser.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@

import java.io.DataInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.BiFunction;
import java.util.zip.GZIPInputStream;

Expand Down Expand Up @@ -216,7 +213,7 @@ private static void registerJavaBlocks() {

BlockRegistries.JAVA_BLOCKS.set(new BlockMapping[blocksJson.size()]); // Set array size to number of blockstates

Set<String> cleanIdentifiers = new HashSet<>();
Deque<String> cleanIdentifiers = new ArrayDeque<>();

int javaRuntimeId = -1;
int bellBlockId = -1;
Expand Down Expand Up @@ -281,7 +278,7 @@ private static void registerJavaBlocks() {
String cleanJavaIdentifier = BlockUtils.getCleanIdentifier(entry.getKey());
String bedrockIdentifier = entry.getValue().get("bedrock_identifier").asText();

if (!cleanIdentifiers.contains(cleanJavaIdentifier)) {
if (!cleanJavaIdentifier.equals(cleanIdentifiers.peekLast())) {
uniqueJavaId++;
cleanIdentifiers.add(cleanJavaIdentifier.intern());
}
Expand Down Expand Up @@ -360,6 +357,8 @@ private static void registerJavaBlocks() {
}
BlockStateValues.JAVA_WATER_ID = waterRuntimeId;

BlockRegistries.CLEAN_JAVA_IDENTIFIERS.set(cleanIdentifiers.toArray(new String[0]));

BLOCKS_JSON = blocksJson;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public static void populate() {
entries.put("geysermc:furnace_minecart", new StartGamePacket.ItemEntry("geysermc:furnace_minecart", (short) furnaceMinecartId, true));

mappings.put(javaFurnaceMinecartId, ItemMapping.builder()
.javaIdentifier("geysermc:furnace_minecart")
.javaIdentifier("minecraft:furnace_minecart")
.bedrockIdentifier("geysermc:furnace_minecart")
.javaId(javaFurnaceMinecartId)
.bedrockId(furnaceMinecartId)
Expand Down
45 changes: 43 additions & 2 deletions core/src/main/java/org/geysermc/geyser/session/GeyserSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.game.recipe.Recipe;
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.level.ServerboundAcceptTeleportationPacket;
Expand Down Expand Up @@ -461,6 +462,8 @@ public class GeyserSession implements GeyserConnection, CommandSender {
@Setter
private boolean waitingForStatistics = false;

private final Set<String> fogNameSpaces = new HashSet<>();

private final Set<UUID> emotes;

/**
Expand Down Expand Up @@ -1009,11 +1012,11 @@ protected void tick() {
// Set the mood
if (!isInWorldBorderWarningArea) {
isInWorldBorderWarningArea = true;
WorldBorder.sendFog(this, "minecraft:fog_crimson_forest");
sendFog("minecraft:fog_crimson_forest");
}
} else if (isInWorldBorderWarningArea) {
// Clear fog as we are outside the world border now
WorldBorder.removeFog(this);
removeFog("minecraft:fog_crimson_forest");
isInWorldBorderWarningArea = false;
}

Expand Down Expand Up @@ -1468,6 +1471,12 @@ public void sendAdventureSettings() {
* @param statistics Updated statistics values
*/
public void updateStatistics(@NonNull Map<Statistic, Integer> statistics) {
if (this.statistics.isEmpty()) {
// Initialize custom statistics to 0, so that they appear in the form
for (CustomStatistic customStatistic : CustomStatistic.values()) {
this.statistics.put(customStatistic, 0);
}
}
this.statistics.putAll(statistics);
}

Expand All @@ -1487,4 +1496,36 @@ public void refreshEmotes(List<UUID> emotes) {
player.sendUpstreamPacket(emoteList);
}
}

/**
* Send the following fog IDs, as well as the cached ones, to the client.
*
* Fog IDs can be found here:
* https://wiki.bedrock.dev/documentation/fog-ids.html
*
* @param fogNameSpaces the fog ids to add
*/
public void sendFog(String... fogNameSpaces) {
this.fogNameSpaces.addAll(Arrays.asList(fogNameSpaces));

PlayerFogPacket packet = new PlayerFogPacket();
packet.getFogStack().addAll(this.fogNameSpaces);
sendUpstreamPacket(packet);
}

/**
* Removes the following fog IDs from the client and the cache.
*
* @param fogNameSpaces the fog ids to remove
*/
public void removeFog(String... fogNameSpaces) {
if (fogNameSpaces.length == 0) {
this.fogNameSpaces.clear();
} else {
this.fogNameSpaces.removeAll(Arrays.asList(fogNameSpaces));
}
PlayerFogPacket packet = new PlayerFogPacket();
packet.getFogStack().addAll(this.fogNameSpaces);
sendUpstreamPacket(packet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,4 @@ private void sendWorldBorderParticle(float x, float y, float z) {
effectPacket.setType(WORLD_BORDER_PARTICLE);
session.getUpstream().sendPacket(effectPacket);
}

/**
* Send the following fog IDs to the client
*/
public static void sendFog(GeyserSession session, String... fogNameSpaces) {
PlayerFogPacket packet = new PlayerFogPacket();
Collections.addAll(packet.getFogStack(), fogNameSpaces);
session.sendUpstreamPacket(packet);
}

/**
* Clear any additional fog sent to the client
*/
public static void removeFog(GeyserSession session) {
session.sendUpstreamPacket(new PlayerFogPacket());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import it.unimi.dsi.fastutil.ints.*;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.CartographyContainer;
import org.geysermc.geyser.inventory.GeyserItemStack;
Expand All @@ -65,11 +66,8 @@
public abstract class InventoryTranslator {

public static final InventoryTranslator PLAYER_INVENTORY_TRANSLATOR = new PlayerInventoryTranslator();
public static final Map<ContainerType, InventoryTranslator> INVENTORY_TRANSLATORS = new HashMap<>() {
private static final Map<ContainerType, InventoryTranslator> INVENTORY_TRANSLATORS = new EnumMap<>(ContainerType.class) {
{
/* Player Inventory */
put(null, PLAYER_INVENTORY_TRANSLATOR);

/* Chest UIs */
put(ContainerType.GENERIC_9X1, new SingleChestInventoryTranslator(9));
put(ContainerType.GENERIC_9X2, new SingleChestInventoryTranslator(18));
Expand Down Expand Up @@ -393,6 +391,7 @@ public ItemStackResponsePacket.Response translateRequest(GeyserSession session,
}
break;
}
case CRAFT_RECIPE: // Called by stonecutters 1.18+
case CRAFT_RECIPE_AUTO: // Called by villagers
case CRAFT_NON_IMPLEMENTED_DEPRECATED: // Tends to be called for UI inventories
case CRAFT_RESULTS_DEPRECATED: // Tends to be called for UI inventories
Expand Down Expand Up @@ -878,6 +877,22 @@ protected static boolean isCursor(StackRequestSlotInfoData slotInfoData) {
return slotInfoData.getContainer() == ContainerSlotType.CURSOR;
}

/**
* Gets the {@link InventoryTranslator} for the given {@link ContainerType}.
* Returns {@link #PLAYER_INVENTORY_TRANSLATOR} if type is null.
*
* @param type the type
* @return the InventoryType for the given ContainerType.
*/
@Nullable
public static InventoryTranslator inventoryTranslator(@Nullable ContainerType type) {
if (type == null) {
return PLAYER_INVENTORY_TRANSLATOR;
}

return INVENTORY_TRANSLATORS.get(type);
}

protected enum CraftState {
START,
RECIPE_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public StonecutterInventoryTranslator() {

@Override
public boolean shouldHandleRequestFirst(StackRequestActionData action, Inventory inventory) {
return action.getType() == StackRequestActionType.CRAFT_NON_IMPLEMENTED_DEPRECATED;
// First is pre-1.18. TODO remove after 1.17.40 support is dropped and refactor stonecutter support to use CraftRecipeStackRequestActionData's recipe ID
return action.getType() == StackRequestActionType.CRAFT_NON_IMPLEMENTED_DEPRECATED || action.getType() == StackRequestActionType.CRAFT_RECIPE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.translator.protocol.java.inventory.JavaMerchantOffersTranslator;
import org.geysermc.geyser.util.InventoryUtils;

@Translator(packet = ContainerClosePacket.class)
Expand Down Expand Up @@ -59,6 +60,10 @@ public void translate(GeyserSession session, ContainerClosePacket packet) {
} else if (openInventory.isPending()) {
InventoryUtils.displayInventory(session, openInventory);
openInventory.setPending(false);

if (openInventory instanceof MerchantContainer merchantContainer && merchantContainer.getPendingOffersPacket() != null) {
JavaMerchantOffersTranslator.openMerchant(session, merchantContainer.getPendingOffersPacket(), merchantContainer);
}
}
}
}
Expand Down
Loading

0 comments on commit 3b45559

Please sign in to comment.