[API] Nojang api for Simple RPC
This commit is contained in:
@@ -1,25 +1,24 @@
|
||||
package com.hypherionmc.craterlib.api.events.client;
|
||||
|
||||
import com.hypherionmc.craterlib.core.event.CraterEvent;
|
||||
import com.hypherionmc.craterlib.nojang.client.gui.BridgedScreen;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
|
||||
// TODO NOJANG
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class ScreenEvent extends CraterEvent {
|
||||
|
||||
private final Screen screen;
|
||||
private final BridgedScreen screen;
|
||||
|
||||
@Getter
|
||||
public static class Opening extends ScreenEvent {
|
||||
|
||||
private final Screen currentScreen;
|
||||
@Setter private Screen newScreen;
|
||||
private final BridgedScreen currentScreen;
|
||||
@Setter private BridgedScreen newScreen;
|
||||
|
||||
public Opening(Screen currentScreen, Screen newScreen) {
|
||||
public Opening(BridgedScreen currentScreen, BridgedScreen newScreen) {
|
||||
super(newScreen);
|
||||
this.currentScreen = currentScreen;
|
||||
this.newScreen = newScreen;
|
||||
|
@@ -2,6 +2,7 @@ package com.hypherionmc.craterlib.mixin.events.client;
|
||||
|
||||
import com.hypherionmc.craterlib.api.events.client.ScreenEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.hypherionmc.craterlib.nojang.client.gui.BridgedScreen;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -11,7 +12,6 @@ import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
// TODO NOJANG
|
||||
@Mixin(Minecraft.class)
|
||||
public class MinecraftMixin {
|
||||
|
||||
@@ -23,7 +23,7 @@ public class MinecraftMixin {
|
||||
private void injectScreenOpeningEvent(Screen screen, CallbackInfo ci) {
|
||||
Screen old = this.screen;
|
||||
if (screen != null) {
|
||||
ScreenEvent.Opening opening = new ScreenEvent.Opening(old, screen);
|
||||
ScreenEvent.Opening opening = new ScreenEvent.Opening(BridgedScreen.of(old), BridgedScreen.of(screen));
|
||||
CraterEventBus.INSTANCE.postEvent(opening);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,84 @@
|
||||
package com.hypherionmc.craterlib.nojang.client;
|
||||
|
||||
import com.hypherionmc.craterlib.nojang.client.multiplayer.BridgedClientLevel;
|
||||
import com.hypherionmc.craterlib.nojang.client.multiplayer.BridgedServerData;
|
||||
import com.hypherionmc.craterlib.nojang.client.server.BridgedIntegratedServer;
|
||||
import com.hypherionmc.craterlib.nojang.world.entity.player.BridgedPlayer;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BridgedMinecraft {
|
||||
|
||||
@Getter
|
||||
private static final BridgedMinecraft instance = new BridgedMinecraft();
|
||||
private final Minecraft internal = Minecraft.getInstance();
|
||||
|
||||
public File getGameDirectory() {
|
||||
return internal.gameDirectory;
|
||||
}
|
||||
|
||||
public BridgedOptions getOptions() {
|
||||
return BridgedOptions.of(internal.options);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BridgedClientLevel getLevel() {
|
||||
if (internal.level == null)
|
||||
return null;
|
||||
|
||||
return BridgedClientLevel.of(internal.level);
|
||||
}
|
||||
|
||||
public boolean isRealmServer() {
|
||||
return internal.getCurrentServer() != null && internal.getCurrentServer().isRealm();
|
||||
}
|
||||
|
||||
public boolean isSinglePlayer() {
|
||||
return internal.hasSingleplayerServer();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BridgedPlayer getPlayer() {
|
||||
if (internal.player == null)
|
||||
return null;
|
||||
|
||||
return BridgedPlayer.of(internal.player);
|
||||
}
|
||||
|
||||
public String getGameVersion() {
|
||||
return SharedConstants.getCurrentVersion().getName();
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return internal.getUser().getName();
|
||||
}
|
||||
|
||||
public UUID getPlayerId() {
|
||||
return internal.getUser().getProfileId();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BridgedServerData getCurrentServer() {
|
||||
if (internal.getCurrentServer() == null)
|
||||
return null;
|
||||
|
||||
return BridgedServerData.of(internal.getCurrentServer());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BridgedIntegratedServer getSinglePlayerServer() {
|
||||
return BridgedIntegratedServer.of(internal.getSingleplayerServer());
|
||||
}
|
||||
|
||||
public int getServerPlayerCount () {
|
||||
if (internal.getConnection() == null)
|
||||
return 0;
|
||||
|
||||
return internal.getConnection().getOnlinePlayers().size();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,36 @@
|
||||
package com.hypherionmc.craterlib.nojang.client.gui;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.client.gui.screens.LevelLoadingScreen;
|
||||
import net.minecraft.client.gui.screens.ReceivingLevelScreen;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.client.gui.screens.TitleScreen;
|
||||
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
|
||||
import net.minecraft.realms.RealmsScreen;
|
||||
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public class BridgedScreen {
|
||||
|
||||
private final Screen internal;
|
||||
|
||||
public boolean isTitleScreen() {
|
||||
return internal instanceof TitleScreen;
|
||||
}
|
||||
|
||||
public boolean isRealmsScreen() {
|
||||
return internal instanceof RealmsScreen;
|
||||
}
|
||||
|
||||
public boolean isServerBrowserScreen() {
|
||||
return internal instanceof JoinMultiplayerScreen;
|
||||
}
|
||||
|
||||
public boolean isLoadingScreen() {
|
||||
return internal instanceof LevelLoadingScreen || internal instanceof ReceivingLevelScreen;
|
||||
}
|
||||
|
||||
public Screen toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
||||
}
|
@@ -1,11 +1,59 @@
|
||||
package com.hypherionmc.craterlib.nojang.client.multiplayer;
|
||||
|
||||
import com.hypherionmc.craterlib.nojang.core.BridgedBlockPos;
|
||||
import com.hypherionmc.craterlib.nojang.resources.ResourceIdentifier;
|
||||
import com.hypherionmc.craterlib.utils.ChatUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public class BridgedClientLevel {
|
||||
|
||||
private final ClientLevel internal;
|
||||
|
||||
public boolean isClientSide() {
|
||||
return internal.isClientSide();
|
||||
}
|
||||
|
||||
public long getGameTime() {
|
||||
return internal.getGameTime();
|
||||
}
|
||||
|
||||
public long getDayTime() {
|
||||
return internal.getDayTime();
|
||||
}
|
||||
|
||||
public long dayTime() {
|
||||
return internal.dayTime();
|
||||
}
|
||||
|
||||
public boolean isRaining() {
|
||||
return internal.isRaining();
|
||||
}
|
||||
|
||||
public boolean isThundering() {
|
||||
return internal.isThundering();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ResourceIdentifier getDimensionKey() {
|
||||
return ResourceIdentifier.fromMojang(internal.dimension().location());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ResourceIdentifier getBiomeIdentifier(BridgedBlockPos onPos) {
|
||||
AtomicReference<ResourceIdentifier> identifier = new AtomicReference<>(null);
|
||||
internal.getBiome(onPos.toMojang()).unwrap().ifLeft(b -> identifier.set(ResourceIdentifier.fromMojang(b.location())));
|
||||
return identifier.get();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Component getDifficulty() {
|
||||
return ChatUtils.mojangToAdventure(internal.getDifficulty().getDisplayName());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,40 @@
|
||||
package com.hypherionmc.craterlib.nojang.client.multiplayer;
|
||||
|
||||
import com.hypherionmc.craterlib.utils.ChatUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minecraft.client.multiplayer.ServerData;
|
||||
import net.minecraft.client.multiplayer.ServerStatusPinger;
|
||||
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public class BridgedServerData {
|
||||
|
||||
private final ServerData internal;
|
||||
|
||||
public String name() {
|
||||
return internal.name;
|
||||
}
|
||||
|
||||
public String ip() {
|
||||
return internal.ip;
|
||||
}
|
||||
|
||||
public Component motd() {
|
||||
return ChatUtils.mojangToAdventure(internal.motd);
|
||||
}
|
||||
|
||||
public int getMaxPlayers() {
|
||||
if (!internal.pinged || internal.players == null) {
|
||||
try {
|
||||
new ServerStatusPinger().pingServer(internal, () -> {});
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
return internal.players == null ? 0 : internal.players.max();
|
||||
}
|
||||
|
||||
public ServerData toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.hypherionmc.craterlib.nojang.client.server;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.client.server.IntegratedServer;
|
||||
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public class BridgedIntegratedServer {
|
||||
|
||||
private final IntegratedServer internal;
|
||||
|
||||
public String getLevelName() {
|
||||
return internal.getWorldData().getLevelName();
|
||||
}
|
||||
|
||||
public IntegratedServer toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.hypherionmc.craterlib.nojang.core;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.core.BlockPos;
|
||||
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public class BridgedBlockPos {
|
||||
|
||||
private final BlockPos internal;
|
||||
|
||||
public int getX() {
|
||||
return internal.getX();
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return internal.getY();
|
||||
}
|
||||
|
||||
public int getZ() {
|
||||
return internal.getZ();
|
||||
}
|
||||
|
||||
public BlockPos toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
||||
}
|
@@ -18,6 +18,15 @@ public class BridgedFriendlyByteBuf {
|
||||
return BridgedFriendlyByteBuf.of(internal);
|
||||
}
|
||||
|
||||
public BridgedFriendlyByteBuf writeUtf(String value) {
|
||||
internal.writeUtf(value);
|
||||
return BridgedFriendlyByteBuf.of(internal);
|
||||
}
|
||||
|
||||
public String readUtf() {
|
||||
return internal.readUtf();
|
||||
}
|
||||
|
||||
public FriendlyByteBuf toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.hypherionmc.craterlib.nojang.realmsclient.dto;
|
||||
|
||||
import com.mojang.realmsclient.dto.PlayerInfo;
|
||||
import com.mojang.realmsclient.dto.RealmsServer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@@ -8,4 +9,32 @@ public class BridgedRealmsServer {
|
||||
|
||||
private final RealmsServer internal;
|
||||
|
||||
public String getName() {
|
||||
return internal.getName();
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return internal.getDescription();
|
||||
}
|
||||
|
||||
public String getWorldType() {
|
||||
return internal.worldType.name();
|
||||
}
|
||||
|
||||
public String getMinigameName() {
|
||||
return internal.getMinigameName();
|
||||
}
|
||||
|
||||
public String getMinigameImage() {
|
||||
return internal.minigameImage;
|
||||
}
|
||||
|
||||
public long getPlayerCount() {
|
||||
return internal.players.stream().filter(PlayerInfo::getOnline).count();
|
||||
}
|
||||
|
||||
public RealmsServer toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -26,6 +26,10 @@ public class ResourceIdentifier {
|
||||
return internal.toString();
|
||||
}
|
||||
|
||||
public static ResourceIdentifier fromMojang(ResourceLocation location) {
|
||||
return new ResourceIdentifier(location.getNamespace(), location.getPath());
|
||||
}
|
||||
|
||||
public ResourceLocation toMojang() {
|
||||
return internal;
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.hypherionmc.craterlib.nojang.world.entity.player;
|
||||
|
||||
import com.hypherionmc.craterlib.nojang.authlib.BridgedGameProfile;
|
||||
import com.hypherionmc.craterlib.nojang.core.BridgedBlockPos;
|
||||
import com.hypherionmc.craterlib.utils.ChatUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -44,6 +45,10 @@ public class BridgedPlayer {
|
||||
return internal;
|
||||
}
|
||||
|
||||
public BridgedBlockPos getOnPos() {
|
||||
return BridgedBlockPos.of(internal.getOnPos());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ServerGamePacketListenerImpl getConnection() {
|
||||
if (isServerPlayer()) {
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package com.hypherionmc.craterlib.utils;
|
||||
|
||||
import com.hypherionmc.craterlib.nojang.resources.ResourceIdentifier;
|
||||
import me.hypherionmc.mcdiscordformatter.discord.DiscordSerializer;
|
||||
import me.hypherionmc.mcdiscordformatter.minecraft.MinecraftSerializer;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.Style;
|
||||
|
||||
@@ -77,4 +79,11 @@ public class ChatUtils {
|
||||
public static net.kyori.adventure.text.Component makeComponent(String text) {
|
||||
return net.kyori.adventure.text.Component.translatable(text);
|
||||
}
|
||||
|
||||
public static net.kyori.adventure.text.Component getBiomeName(ResourceIdentifier identifier) {
|
||||
if (identifier == null)
|
||||
return net.kyori.adventure.text.Component.text("Unknown");
|
||||
|
||||
return mojangToAdventure(Component.translatable(Util.makeDescriptionId("biome", identifier.toMojang())));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user