Merge remote-tracking branch 'origin/master' into floodgate-2.0

This commit is contained in:
Tim203 2021-04-21 19:49:49 +02:00
commit 085c122d30
No known key found for this signature in database
GPG Key ID: 064EE9F5BF7C3EE8
16 changed files with 197 additions and 25 deletions

View File

@ -169,12 +169,13 @@ public class GeyserConnector {
if (config.isDebugMode()) {
ex.printStackTrace();
}
config.getRemote().setAddress(InetAddress.getLoopbackAddress().getHostAddress());
}
}
String remoteAddress = config.getRemote().getAddress();
int remotePort = config.getRemote().getPort();
// Filters whether it is not an IP address or localhost, because otherwise it is not possible to find out an SRV entry.
if (!remoteAddress.matches(IP_REGEX) && !remoteAddress.equalsIgnoreCase("localhost")) {
int remotePort;
try {
// Searches for a server address and a port from a SRV record of the specified host name
InitialDirContext ctx = new InitialDirContext();

View File

@ -111,6 +111,8 @@ public interface GeyserConfiguration {
String getServerName();
int getCompressionLevel();
boolean isEnableProxyProtocol();
List<String> getProxyProtocolWhitelistedIPs();

View File

@ -147,6 +147,13 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration
@JsonProperty("server-name")
private String serverName = GeyserConnector.NAME;
@JsonProperty("compression-level")
private int compressionLevel = 6;
public int getCompressionLevel() {
return Math.max(-1, Math.min(compressionLevel, 9));
}
@JsonProperty("enable-proxy-protocol")
private boolean enableProxyProtocol = false;

View File

@ -32,6 +32,7 @@ import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
import org.geysermc.connector.entity.attribute.AttributeType;
import org.geysermc.connector.entity.living.animal.AnimalEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
@ -44,6 +45,10 @@ public class AbstractHorseEntity extends AnimalEntity {
// Specifies the size of the entity's inventory. Required to place slots in the entity.
metadata.put(EntityData.CONTAINER_BASE_SIZE, 2);
// Add dummy health attribute since LivingEntity updates the attribute for us
attributes.put(AttributeType.HEALTH, AttributeType.HEALTH.getAttribute(20, 20));
// Add horse jump strength attribute to allow donkeys and mules to jump
attributes.put(AttributeType.HORSE_JUMP_STRENGTH, AttributeType.HORSE_JUMP_STRENGTH.getAttribute(0.5f, 2));
}
@Override
@ -85,9 +90,15 @@ public class AbstractHorseEntity extends AnimalEntity {
}
// Needed to control horses
metadata.getFlags().setFlag(EntityFlag.CAN_POWER_JUMP, true);
boolean canPowerJump = entityType != EntityType.LLAMA && entityType != EntityType.TRADER_LLAMA;
metadata.getFlags().setFlag(EntityFlag.CAN_POWER_JUMP, canPowerJump);
metadata.getFlags().setFlag(EntityFlag.WASD_CONTROLLED, true);
super.updateBedrockMetadata(entityMetadata, session);
if (entityMetadata.getId() == 8) {
// Update the health attribute
updateBedrockAttributes(session);
}
}
}

View File

@ -32,7 +32,7 @@ import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import com.nukkitx.protocol.bedrock.packet.MobArmorEquipmentPacket;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
import org.geysermc.connector.network.translators.item.ItemRegistry;
public class LlamaEntity extends ChestedHorseEntity {
@ -52,16 +52,13 @@ public class LlamaEntity extends ChestedHorseEntity {
if (entityMetadata.getId() == 20) {
// Bedrock treats llama decoration as armor
MobArmorEquipmentPacket equipmentPacket = new MobArmorEquipmentPacket();
equipmentPacket.setRuntimeEntityId(getGeyserId());
equipmentPacket.setRuntimeEntityId(geyserId);
// -1 means no armor
if ((int) entityMetadata.getValue() != -1) {
// The damage value is the dye color that Java sends us
int carpetIndex = (int) entityMetadata.getValue();
if (carpetIndex > -1 && carpetIndex <= 15) {
// The damage value is the dye color that Java sends us, for pre-1.16.220
// The item is always going to be a carpet
equipmentPacket.setChestplate(ItemData.builder()
.id(BlockTranslator.CARPET)
.damage((int) entityMetadata.getValue())
.count(1)
.build());
equipmentPacket.setChestplate(ItemRegistry.CARPETS.get(carpetIndex));
} else {
equipmentPacket.setChestplate(ItemData.AIR);
}

View File

@ -28,7 +28,6 @@ package org.geysermc.connector.inventory;
import com.nukkitx.protocol.bedrock.data.inventory.EnchantData;
import com.nukkitx.protocol.bedrock.data.inventory.EnchantOptionData;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.Arrays;
@ -38,7 +37,6 @@ import java.util.List;
/**
* A mutable "wrapper" around {@link EnchantOptionData}
*/
@Setter
public class GeyserEnchantOption {
private static final List<EnchantData> EMPTY = Collections.emptyList();
/**
@ -57,6 +55,12 @@ public class GeyserEnchantOption {
@Getter
private final int javaIndex;
/**
* Whether the enchantment details have actually changed.
* Used to mitigate weird packet spamming pre-1.14, causing the net ID to always update.
*/
private boolean hasChanged;
private int xpCost = 0;
private int javaEnchantIndex = -1;
private int bedrockEnchantIndex = -1;
@ -67,8 +71,35 @@ public class GeyserEnchantOption {
}
public EnchantOptionData build(GeyserSession session) {
this.hasChanged = false;
return new EnchantOptionData(xpCost, javaIndex + 16, EMPTY,
enchantLevel == -1 ? EMPTY : Collections.singletonList(new EnchantData(bedrockEnchantIndex, enchantLevel)), EMPTY,
javaEnchantIndex == -1 ? "unknown" : ENCHANT_NAMES.get(javaEnchantIndex), enchantLevel == -1 ? 0 : session.getNextItemNetId());
}
public boolean hasChanged() {
return hasChanged;
}
public void setXpCost(int xpCost) {
if (this.xpCost != xpCost) {
hasChanged = true;
this.xpCost = xpCost;
}
}
public void setEnchantIndex(int javaEnchantIndex, int bedrockEnchantIndex) {
if (this.javaEnchantIndex != javaEnchantIndex) {
hasChanged = true;
this.javaEnchantIndex = javaEnchantIndex;
this.bedrockEnchantIndex = bedrockEnchantIndex;
}
}
public void setEnchantLevel(int enchantLevel) {
if (this.enchantLevel != enchantLevel) {
hasChanged = true;
this.enchantLevel = enchantLevel;
}
}
}

View File

@ -159,6 +159,7 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
@Override
public void onSessionCreation(BedrockServerSession bedrockServerSession) {
bedrockServerSession.setLogging(true);
bedrockServerSession.setCompressionLevel(connector.getConfig().getBedrock().getCompressionLevel());
bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(connector, new GeyserSession(connector, bedrockServerSession)));
// Set the packet codec to default just in case we need to send disconnect packets.
bedrockServerSession.setPacketCodec(BedrockProtocol.DEFAULT_BEDROCK_CODEC);

View File

@ -352,6 +352,12 @@ public class GeyserSession implements CommandSender {
@Setter
private long lastMovementTimestamp = System.currentTimeMillis();
/**
* Used to send a ClientVehicleMovePacket for every PlayerInputPacket after idling on a boat/horse for more than 100ms
*/
@Setter
private long lastVehicleMoveTimestamp = System.currentTimeMillis();
/**
* Controls whether the daylight cycle gamerule has been sent to the client, so the sun/moon remain motionless.
*/

View File

@ -41,6 +41,8 @@ public class BedrockMoveEntityAbsoluteTranslator extends PacketTranslator<MoveEn
@Override
public void translate(MoveEntityAbsolutePacket packet, GeyserSession session) {
session.setLastVehicleMoveTimestamp(System.currentTimeMillis());
float y = packet.getPosition().getY();
if (session.getRidingVehicleEntity() instanceof BoatEntity) {
// Remove some Y position to prevents boats from looking like they're floating in water

View File

@ -26,7 +26,15 @@
package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientSteerVehiclePacket;
import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientVehicleMovePacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.packet.PlayerInputPacket;
import org.geysermc.connector.entity.BoatEntity;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
import org.geysermc.connector.entity.living.animal.horse.LlamaEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
@ -44,5 +52,42 @@ public class BedrockPlayerInputTranslator extends PacketTranslator<PlayerInputPa
);
session.sendDownstreamPacket(clientSteerVehiclePacket);
// Bedrock only sends movement vehicle packets while moving
// This allows horses to take damage while standing on magma
Entity vehicle = session.getRidingVehicleEntity();
boolean sendMovement = false;
if (vehicle instanceof AbstractHorseEntity && !(vehicle instanceof LlamaEntity)) {
sendMovement = vehicle.isOnGround();
} else if (vehicle instanceof BoatEntity) {
if (vehicle.getPassengers().size() == 1) {
// The player is the only rider
sendMovement = true;
} else {
// Check if the player is the front rider
Vector3f seatPos = session.getPlayerEntity().getMetadata().getVector3f(EntityData.RIDER_SEAT_POSITION, null);
if (seatPos != null && seatPos.getX() > 0) {
sendMovement = true;
}
}
}
if (sendMovement) {
long timeSinceVehicleMove = System.currentTimeMillis() - session.getLastVehicleMoveTimestamp();
if (timeSinceVehicleMove >= 100) {
Vector3f vehiclePosition = vehicle.getPosition();
Vector3f vehicleRotation = vehicle.getRotation();
if (vehicle instanceof BoatEntity) {
// Remove some Y position to prevents boats flying up
vehiclePosition = vehiclePosition.down(EntityType.BOAT.getOffset());
}
ClientVehicleMovePacket clientVehicleMovePacket = new ClientVehicleMovePacket(
vehiclePosition.getX(), vehiclePosition.getY(), vehiclePosition.getZ(),
vehicleRotation.getX() - 90, vehicleRotation.getY()
);
session.sendDownstreamPacket(clientVehicleMovePacket);
}
}
}
}

View File

@ -0,0 +1,47 @@
/*
* 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.bedrock.entity.player;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerStatePacket;
import com.nukkitx.protocol.bedrock.packet.RiderJumpPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
@Translator(packet = RiderJumpPacket.class)
public class BedrockRiderJumpTranslator extends PacketTranslator<RiderJumpPacket> {
@Override
public void translate(RiderJumpPacket packet, GeyserSession session) {
Entity vehicle = session.getRidingVehicleEntity();
if (vehicle instanceof AbstractHorseEntity) {
ClientPlayerStatePacket playerStatePacket = new ClientPlayerStatePacket((int) vehicle.getEntityId(), PlayerState.START_HORSE_JUMP, packet.getJumpStrength());
session.sendDownstreamPacket(playerStatePacket);
}
}
}

View File

@ -34,6 +34,7 @@ import com.nukkitx.protocol.bedrock.data.inventory.stackrequestactions.StackRequ
import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket;
import com.nukkitx.protocol.bedrock.packet.PlayerEnchantOptionsPacket;
import org.geysermc.connector.inventory.EnchantingContainer;
import org.geysermc.connector.inventory.GeyserEnchantOption;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.inventory.PlayerInventory;
import org.geysermc.connector.network.session.GeyserSession;
@ -67,18 +68,20 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
case 6:
// Enchantment type
slotToUpdate = key - 4;
int index = value;
if (index != -1) {
Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + JavaEnchantment.values()[index].name().toLowerCase());
// "value" here is the Java enchant ordinal, so that does not need to be changed
// The Bedrock index might need changed, so let's look it up and see.
int bedrockIndex = value;
if (bedrockIndex != -1) {
Enchantment enchantment = Enchantment.getByJavaIdentifier("minecraft:" + JavaEnchantment.values()[bedrockIndex].name().toLowerCase());
if (enchantment != null) {
// Convert the Java enchantment index to Bedrock's
index = enchantment.ordinal();
bedrockIndex = enchantment.ordinal();
} else {
index = -1;
// There is no Bedrock enchantment equivalent
bedrockIndex = -1;
}
}
enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].setJavaEnchantIndex(value);
enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].setBedrockEnchantIndex(index);
enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].setEnchantIndex(value, bedrockIndex);
break;
case 7:
case 8:
@ -91,8 +94,9 @@ public class EnchantingInventoryTranslator extends AbstractBlockInventoryTransla
default:
return;
}
if (shouldUpdate) {
enchantingInventory.getEnchantOptions()[slotToUpdate] = enchantingInventory.getGeyserEnchantOptions()[slotToUpdate].build(session);
GeyserEnchantOption enchantOption = enchantingInventory.getGeyserEnchantOptions()[slotToUpdate];
if (shouldUpdate && enchantOption.hasChanged()) {
enchantingInventory.getEnchantOptions()[slotToUpdate] = enchantOption.build(session);
PlayerEnchantOptionsPacket packet = new PlayerEnchantOptionsPacket();
packet.getOptions().addAll(Arrays.asList(enchantingInventory.getEnchantOptions()));
session.sendUpstreamPacket(packet);

View File

@ -92,6 +92,10 @@ public class ItemRegistry {
* Bucket item entries (excluding the milk bucket), used in BedrockInventoryTransactionTranslator.java
*/
public static final IntSet BUCKETS = new IntArraySet();
/**
* Carpet item data, used in LlamaEntity.java
*/
public static final List<ItemData> CARPETS = new ArrayList<>(16);
/**
* Crossbow item entry, used in PillagerEntity.java
*/
@ -452,6 +456,13 @@ public class ItemRegistry {
BOATS.add(entry.getValue().get("bedrock_id").intValue());
} else if (entry.getKey().contains("bucket") && !entry.getKey().contains("milk")) {
BUCKETS.add(entry.getValue().get("bedrock_id").intValue());
} else if (entry.getKey().contains("_carpet")) {
// This should be the numerical order Java sends as an integer value for llamas
CARPETS.add(ItemData.builder()
.id(itemEntry.getBedrockId())
.damage(itemEntry.getBedrockData())
.count(1)
.blockRuntimeId(itemEntry.getBedrockBlockId()).build());
}
itemNames.add(entry.getKey());

View File

@ -26,6 +26,7 @@
package org.geysermc.connector.network.translators.java.entity;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
@ -47,6 +48,12 @@ public class JavaEntityVelocityTranslator extends PacketTranslator<ServerEntityV
entity.setMotion(Vector3f.from(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ()));
if (entity == session.getRidingVehicleEntity() && entity instanceof AbstractHorseEntity) {
// Horses for some reason teleport back when a SetEntityMotionPacket is sent while
// a player is riding on them. Java clients seem to ignore it anyways.
return;
}
SetEntityMotionPacket entityMotionPacket = new SetEntityMotionPacket();
entityMotionPacket.setRuntimeEntityId(entity.getGeyserId());
entityMotionPacket.setMotion(entity.getMotion());

View File

@ -74,9 +74,6 @@ public abstract class BlockTranslator {
private final Object2IntMap<NbtMap> itemFrames = new Object2IntOpenHashMap<>();
private final Map<String, NbtMap> flowerPotBlocks = new HashMap<>();
// Bedrock carpet ID, used in LlamaEntity.java for decoration
public static final int CARPET = 171;
public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap();
public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap();
public static final Int2ObjectMap<String> JAVA_RUNTIME_ID_TO_TOOL_TYPE = new Int2ObjectOpenHashMap<>();

View File

@ -23,6 +23,9 @@ bedrock:
motd2: "Another Geyser server."
# The Server Name that will be sent to Minecraft: Bedrock Edition clients. This is visible in both the pause menu and the settings menu.
server-name: "Geyser"
# How much to compress network traffic to the Bedrock client. The higher the number, the more CPU usage used, but
# the smaller the bandwidth used. Does not have any effect below -1 or above 9. Set to -1 to disable.
compression-level: 6
# Whether to enable PROXY protocol or not for clients. You DO NOT WANT this feature unless you run UDP reverse proxy
# in front of your Geyser instance.
enable-proxy-protocol: false