Skip to content

Commit

Permalink
Merge branch 'feature/plugins' into bleeding
Browse files Browse the repository at this point in the history
  • Loading branch information
bundabrg committed Aug 28, 2020
2 parents 581f326 + 9787ab7 commit 5ac8717
Show file tree
Hide file tree
Showing 29 changed files with 481 additions and 93 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Take a look [here](https://github.com/GeyserMC/Geyser/wiki#Setup) for how to set
- [ ] Command Block
- [ ] Structure Block
- [ ] Horse Inventory
- [ ] Loom
- [ ] Smithing Table
- Some Entity Flags

## Compiling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public String getMotd1() {
public String getMotd2() {
return node.getNode("motd2").getString("GeyserMC");
}

@Override
public String getServerName() {
return node.getNode("server-name").getString("Geyser");
}
}

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.UUID;

public class GeyserStandaloneBootstrap implements GeyserBootstrap {
Expand All @@ -63,22 +64,61 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {

@Getter
private boolean useGui = System.console() == null && !isHeadless();
private String configFilename = "config.yml";

private GeyserConnector connector;


public static void main(String[] args) {
for (String arg : args) {
GeyserStandaloneBootstrap bootstrap = new GeyserStandaloneBootstrap();
// Set defaults
boolean useGuiOpts = bootstrap.useGui;
String configFilenameOpt = bootstrap.configFilename;

for (int i = 0; i < args.length; i++) {
// By default, standalone Geyser will check if it should open the GUI based on if the GUI is null
// Optionally, you can force the use of a GUI or no GUI by specifying args
if (arg.equals("gui")) {
new GeyserStandaloneBootstrap().onEnable(true);
return;
} else if (arg.equals("nogui")) {
new GeyserStandaloneBootstrap().onEnable(false);
return;
// Allows gui and nogui without options, for backwards compatibility
String arg = args[i];
switch (arg) {
case "--gui":
case "gui":
useGuiOpts = true;
break;
case "--nogui":
case "nogui":
useGuiOpts = false;
break;
case "--config":
case "-c":
if (i >= args.length - 1) {
System.err.println(MessageFormat.format(LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.confignotspecified"), "-c"));
return;
}
configFilenameOpt = args[i+1]; i++;
System.out.println(MessageFormat.format(LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.configspecified"), configFilenameOpt));
break;
case "--help":
case "-h":
System.out.println(MessageFormat.format(LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.usage"), "[java -jar] Geyser.jar [opts]"));
System.out.println(" " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.options"));
System.out.println(" -c, --config [file] " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.config"));
System.out.println(" -h, --help " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.help"));
System.out.println(" --gui, --nogui " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.gui"));
return;
default:
String badArgMsg = LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.unrecognised");
System.err.println(MessageFormat.format(badArgMsg, arg));
return;
}
}
new GeyserStandaloneBootstrap().onEnable();
bootstrap.onEnable(useGuiOpts, configFilenameOpt);
}

public void onEnable(boolean useGui, String configFilename) {
this.configFilename = configFilename;
this.useGui = useGui;
this.onEnable();
}

public void onEnable(boolean useGui) {
Expand Down Expand Up @@ -107,7 +147,7 @@ public void onEnable() {
LoopbackUtil.checkLoopback(geyserLogger);

try {
File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
File configFile = FileUtils.fileOrCopiedFromResource(new File(configFilename), "config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
geyserConfig = FileUtils.loadConfig(configFile, GeyserStandaloneConfiguration.class);
if (this.geyserConfig.getRemote().getAddress().equalsIgnoreCase("auto")) {
geyserConfig.setAutoconfiguredRemote(true); // Doesn't really need to be set but /shrug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.geysermc.connector.network.translators.item.ItemRegistry;
import org.geysermc.connector.network.translators.item.ItemTranslator;
import org.geysermc.connector.network.translators.item.PotionMixRegistry;
import org.geysermc.connector.network.translators.item.RecipeRegistry;
import org.geysermc.connector.network.translators.sound.SoundHandlerRegistry;
import org.geysermc.connector.network.translators.sound.SoundRegistry;
import org.geysermc.connector.network.translators.world.WorldManager;
Expand Down Expand Up @@ -145,6 +146,7 @@ private GeyserConnector(PlatformType platformType, GeyserBootstrap bootstrap) {
ItemTranslator.init();
LocaleUtils.init();
PotionMixRegistry.init();
RecipeRegistry.init();
SoundRegistry.init();
SoundHandlerRegistry.init();

Expand Down Expand Up @@ -233,6 +235,10 @@ private GeyserConnector(PlatformType platformType, GeyserBootstrap bootstrap) {
message += LanguageUtils.getLocaleStringLog("geyser.core.finish.console");
}
logger.info(message);

if (platformType == PlatformType.STANDALONE) {
logger.warning(LanguageUtils.getLocaleStringLog("geyser.core.movement_warn"));
}
}

public void shutdown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ interface IBedrockConfiguration {
String getMotd1();

String getMotd2();

String getServerName();
}

interface IRemoteConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.common.serializer.AsteriskSerializer;

import java.nio.file.Path;
Expand Down Expand Up @@ -121,6 +122,9 @@ public static class BedrockConfiguration implements IBedrockConfiguration {

private String motd1;
private String motd2;

@JsonProperty("server-name")
private String serverName = GeyserConnector.NAME;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ public class GeyserSession implements CommandSender {
@Setter
private Vector3i lastInteractionPosition;

@Setter
private boolean switchingDimension = false;
private boolean manyDimPackets = false;
private ServerRespawnPacket lastDimPacket = null;

Expand Down Expand Up @@ -661,8 +659,10 @@ private void startGame() {
startGamePacket.setFromWorldTemplate(false);
startGamePacket.setWorldTemplateOptionLocked(false);

startGamePacket.setLevelId("world");
startGamePacket.setLevelName("world");
String serverName = connector.getConfig().getBedrock().getServerName();
startGamePacket.setLevelId(serverName);
startGamePacket.setLevelName(serverName);

startGamePacket.setPremiumWorldTemplateId("00000000-0000-0000-0000-000000000000");
// startGamePacket.setCurrentTick(0);
startGamePacket.setEnchantmentSeed(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
package org.geysermc.connector.network.translators.bedrock.entity.player;

import com.nukkitx.protocol.bedrock.packet.EmotePacket;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
Expand All @@ -37,9 +37,12 @@ public class BedrockEmoteTranslator extends PacketTranslator<EmotePacket> {
@Override
public void translate(EmotePacket packet, GeyserSession session) {
long javaId = session.getPlayerEntity().getEntityId();
for (GeyserSession otherSession : GeyserConnector.getInstance().getPlayers()) {
for (GeyserSession otherSession : session.getConnector().getPlayers()) {
if (otherSession != session) {
packet.setRuntimeEntityId(otherSession.getEntityCache().getEntityByJavaId(javaId).getGeyserId());
if (otherSession.isClosed()) continue;
Entity otherEntity = otherSession.getEntityCache().getEntityByJavaId(javaId);
if (otherEntity == null) continue;
packet.setRuntimeEntityId(otherEntity.getGeyserId());
otherSession.sendUpstreamPacket(packet);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,8 @@ public static void init() {
int netId = 1;
List<ItemData> creativeItems = new ArrayList<>();
for (JsonNode itemNode : creativeItemEntries) {
try {
short damage = 0;
NbtMap tag = null;
if (itemNode.has("damage")) {
damage = itemNode.get("damage").numberValue().shortValue();
}
if (itemNode.has("nbt_b64")) {
byte[] bytes = Base64.getDecoder().decode(itemNode.get("nbt_b64").asText());
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
tag = (NbtMap) NbtUtils.createReaderLE(bais).readTag();
}
creativeItems.add(ItemData.fromNet(netId++, itemNode.get("id").asInt(), damage, 1, tag));
} catch (IOException e) {
e.printStackTrace();
}
ItemData item = getBedrockItemFromJson(itemNode);
creativeItems.add(ItemData.fromNet(netId++, item.getId(), item.getDamage(), item.getCount(), item.getTag()));
}
CREATIVE_ITEMS = creativeItems.toArray(new ItemData[0]);
}
Expand Down Expand Up @@ -233,4 +220,31 @@ public static ItemEntry getItemEntry(String javaIdentifier) {
return JAVA_IDENTIFIER_MAP.computeIfAbsent(javaIdentifier, key -> ITEM_ENTRIES.values()
.stream().filter(itemEntry -> itemEntry.getJavaIdentifier().equals(key)).findFirst().orElse(null));
}

/**
* Gets a Bedrock {@link ItemData} from a {@link JsonNode}
* @param itemNode the JSON node that contains ProxyPass-compatible Bedrock item data
* @return
*/
public static ItemData getBedrockItemFromJson(JsonNode itemNode) {
int count = 1;
short damage = 0;
NbtMap tag = null;
if (itemNode.has("damage")) {
damage = itemNode.get("damage").numberValue().shortValue();
}
if (itemNode.has("count")) {
count = itemNode.get("count").asInt();
}
if (itemNode.has("nbt_b64")) {
byte[] bytes = Base64.getDecoder().decode(itemNode.get("nbt_b64").asText());
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try {
tag = (NbtMap) NbtUtils.createReaderLE(bais).readTag();
} catch (IOException e) {
e.printStackTrace();
}
}
return ItemData.of(itemNode.get("id").asInt(), damage, count, tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack
if (nbt != null) {
for (NbtItemStackTranslator translator : NBT_TRANSLATORS) {
if (translator.acceptItem(bedrockItem)) {
translator.translateToBedrock(nbt, bedrockItem);
translator.translateToBedrock(session, nbt, bedrockItem);
}
}
}

translateDisplayProperties(session, nbt);

ItemData itemData;
ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.get(bedrockItem.getJavaId());
if (itemStackTranslator != null) {
Expand All @@ -155,39 +157,6 @@ public static ItemData translateToBedrock(GeyserSession session, ItemStack stack
itemData = DEFAULT_TRANSLATOR.translateToBedrock(itemStack, bedrockItem);
}


// Get the display name of the item
NbtMap tag = itemData.getTag();
if (tag != null) {
NbtMap display = tag.getCompound("display");
if (display != null && !display.isEmpty() && display.containsKey("Name")) {
String name = display.getString("Name");

// If its not a message convert it
if (!MessageUtils.isMessage(name)) {
TextComponent component = LegacyComponentSerializer.legacySection().deserialize(name);
name = GsonComponentSerializer.gson().serialize(component);
}

// Check if its a message to translate
if (MessageUtils.isMessage(name)) {
// Get the translated name
name = MessageUtils.getTranslatedBedrockMessage(MessageSerializer.fromString(name), session.getClientData().getLanguageCode());

// Build the new display tag
NbtMapBuilder displayBuilder = display.toBuilder();
displayBuilder.putString("Name", name);

// Build the new root tag
NbtMapBuilder builder = tag.toBuilder();
builder.put("display", displayBuilder.build());

// Create a new item with the original data + updated name
itemData = ItemData.of(itemData.getId(), itemData.getDamage(), itemData.getCount(), builder.build());
}
}
}

return itemData;
}

Expand Down Expand Up @@ -379,6 +348,38 @@ private com.github.steveice10.opennbt.tag.builtin.Tag translateToJavaNBT(String
return null;
}

/**
* Translates the display name of the item
* @param session the Bedrock client's session
* @param tag the tag to translate
*/
public static void translateDisplayProperties(GeyserSession session, CompoundTag tag) {
if (tag != null) {
CompoundTag display = tag.get("display");
if (display != null && !display.isEmpty() && display.contains("Name")) {
String name = ((StringTag) display.get("Name")).getValue();

// If its not a message convert it
if (!MessageUtils.isMessage(name)) {
TextComponent component = LegacyComponentSerializer.legacySection().deserialize(name);
name = GsonComponentSerializer.gson().serialize(component);
}

// Check if its a message to translate
if (MessageUtils.isMessage(name)) {
// Get the translated name
name = MessageUtils.getTranslatedBedrockMessage(MessageSerializer.fromString(name), session.getClientData().getLanguageCode());

// Add the new name tag
display.put(new StringTag("Name", name));

// Add to the new root tag
tag.put(display);
}
}
}
}

/**
* Checks if an {@link ItemStack} is equal to another item stack
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,33 @@
package org.geysermc.connector.network.translators.item;

import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.geysermc.connector.network.session.GeyserSession;

public class NbtItemStackTranslator {

public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
/**
* Translate the item NBT to Bedrock
* @param session the client's current session
* @param itemTag the item's CompoundTag
* @param itemEntry Geyser's item entry
*/
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemEntry itemEntry) {

}

/**
* Translate the item NBT to Java.
* @param itemTag the item's CompoundTag
* @param itemEntry Geyser's item entry
*/
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {

}

/**
* @param itemEntry Geyser's item entry
* @return if the item should be processed under this class
*/
public boolean acceptItem(ItemEntry itemEntry) {
return true;
}
Expand Down
Loading

0 comments on commit 5ac8717

Please sign in to comment.