Skip to content

Commit

Permalink
Switch core version
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 8, 2024
1 parent cbe4dad commit f9e5168
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 73 deletions.
93 changes: 39 additions & 54 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Expand Up @@ -117,6 +117,7 @@
//$$ import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
//#endif
//$$ import java.util.function.BiFunction;
//#if MC < 1.20.5
//#if NEOFORGE
//$$ import net.neoforged.neoforge.client.ConfigScreenHandler;
//#elseif MC >= 1.19.2
Expand All @@ -126,6 +127,7 @@
//#else
//$$ import net.minecraftforge.fmlclient.ConfigGuiHandler;
//#endif
//#endif
//#if NEOFORGE
//$$ import net.neoforged.neoforge.resource.ResourcePackLoader;
//#elseif MC > 1.17.1
Expand Down Expand Up @@ -229,52 +231,16 @@ public void onInitializeClient() {
//#endif

private static void init() {
wordsForCid =
//#if FABRIC
FabricLoader.getInstance()
.getModContainer(MOD_ID)
.flatMap(c -> c.findPath("assets/world-host/16k.txt"))
.map(path -> {
try {
return Files.lines(path, StandardCharsets.US_ASCII);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
})
//#else
//$$ ResourcePackLoader
//$$ .getPackFor(MOD_ID)
//#if MC >= 1.20.4
//$$ .map(c -> c.openPrimary("worldhost"))
//#endif
//$$ .map(c -> {
//#if MC <= 1.19.2
//$$ try {
//#endif
//$$ return ((PackResources)c).getResource(PackType.CLIENT_RESOURCES, new ResourceLocation("world-host", "16k.txt"));
//#if MC <= 1.19.2
//$$ } catch (IOException e) {
//$$ throw new UncheckedIOException(e);
//$$ }
//#endif
//$$ })
//#if MC > 1.19.2
//$$ .map(i -> {
//$$ try {
//$$ return i.get();
//$$ } catch (IOException e) {
//$$ throw new UncheckedIOException(e);
//$$ }
//$$ })
//#endif
//$$ .map(is -> new InputStreamReader(is, StandardCharsets.US_ASCII))
//$$ .map(BufferedReader::new)
//$$ .map(BufferedReader::lines)
//#endif
.orElseThrow(() -> new IllegalStateException("Unable to find 16k.txt"))
.filter(s -> !s.startsWith("//"))
.toList();

try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
WorldHost.class.getResourceAsStream("/assets/world-host/16k.txt"),
StandardCharsets.US_ASCII
)
)) {
wordsForCid = reader.lines().filter(s -> !s.startsWith("//")).toList();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (wordsForCid.size() != (1 << 14)) {
throw new RuntimeException("Expected WORDS_FOR_CID to have " + (1 << 14) + " elements, but it has " + wordsForCid.size() + " elements.");
}
Expand Down Expand Up @@ -574,20 +540,36 @@ public static FriendlyByteBuf createByteBuf() {

@SuppressWarnings("RedundantThrows")
public static ServerStatus parseServerStatus(FriendlyByteBuf buf) throws IOException {
return new ClientboundStatusResponsePacket(buf)
//#if MC >= 1.19.4
.status();
//#else
//$$ .getStatus();
//#endif
//#if MC >= 1.20.5
return ClientboundStatusResponsePacket.STREAM_CODEC.decode(buf).status();
//#elseif MC >= 1.19.4
//$$ return new ClientboundStatusResponsePacket(buf).status();
//#else
//$$ return new ClientboundStatusResponsePacket(buf).getStatus();
//#endif
}

public static FriendlyByteBuf writeServerStatus(ServerStatus metadata) {
if (metadata == null) {
metadata = WorldHost.createEmptyServerStatus();
}
final FriendlyByteBuf buf = WorldHost.createByteBuf();
//#if MC < 1.20.5
//$$ new ClientboundStatusResponsePacket(metadata).write(buf);
//#else
ClientboundStatusResponsePacket.STREAM_CODEC.encode(buf, new ClientboundStatusResponsePacket(metadata));
//#endif
return buf;
}

public static ServerStatus createEmptyServerStatus() {
//#if MC >= 1.19.4
return new ServerStatus(
Components.EMPTY, Optional.empty(), Optional.empty(), Optional.empty(), false
//#if FORGELIKE
//#if FORGELIKE && MC < 1.20.5
//$$ , Optional.empty()
//#elseif NEOFORGE
//$$ , false
//#endif
);
//#else
Expand Down Expand Up @@ -696,6 +678,9 @@ public static void connect(Screen parentScreen, long cid, String host, int port)
//#endif
), false
//#endif
//#if MC >= 1.20.5
, null
//#endif
);
}

Expand Down Expand Up @@ -863,7 +848,7 @@ public static CompletableFuture<Optional<String>> checkForUpdates() {
});
}

//#if FORGELIKE
//#if FORGELIKE && MC < 1.20.5
//$$ @Mod.EventBusSubscriber(modid = MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
//$$ public static class ClientModEvents {
//$$ @SubscribeEvent
Expand Down
Expand Up @@ -61,9 +61,11 @@ protected void init() {

if (list == null) {
list = new FriendsList();
if (minecraft != null && minecraft.level != null) {
list.setRenderBackground(false);
}
//#if MC < 1.20.5
//$$ if (minecraft != null && minecraft.level != null) {
//$$ list.setRenderBackground(false);
//$$ }
//#endif
}
setListSize(list, 32, WorldHost.BEDROCK_SUPPORT ? 80 : 64);
addWidget(list);
Expand Down Expand Up @@ -199,8 +201,10 @@ protected void renderDecorations(@NotNull GuiGraphics graphics, int mouseX, int
//$$ final int y0 = this.y0;
//$$ final int y1 = this.y1;
//#endif
graphics.blit(BACKGROUND_LOCATION, x0, 0, 0f, 0f, width, y0, 32, 32);
graphics.blit(BACKGROUND_LOCATION, x0, y1, 0f, y1, width, height - y1, 32, 32);
//#if MC < 1.20.5
//$$ graphics.blit(BACKGROUND_LOCATION, x0, 0, 0f, 0f, width, y0, 32, 32);
//$$ graphics.blit(BACKGROUND_LOCATION, x0, y1, 0f, y1, width, height - y1, 32, 32);
//#endif
graphics.setColor(1f, 1f, 1f, 1f);
graphics.fillGradient(RenderType.guiOverlay(), x0, y0, x1, y0 + 4, 0xff000000, 0, 0);
graphics.fillGradient(RenderType.guiOverlay(), x0, y1 - 4, x1, y1, 0, 0xff000000, 0);
Expand Down
Expand Up @@ -115,8 +115,13 @@ private void serverIsClosed(boolean waitForServer, CallbackInfo ci) {
private void shareWorldOnLoad(UUID uuid, CallbackInfo ci) {
if (!WorldHost.shareWorldOnLoad) return;
WorldHost.shareWorldOnLoad = false;
//#if MC < 1.20.5
//$$ final boolean allowCommands = worldData.getAllowCommands();
//#else
final boolean allowCommands = worldData.isAllowCommands();
//#endif
final Component message;
if (publishServer(worldData.getGameType(), worldData.getAllowCommands(), HttpUtil.getAvailablePort())) {
if (publishServer(worldData.getGameType(), allowCommands, HttpUtil.getAvailablePort())) {
message = wh$getOpenedMessage();
} else {
message = Components.translatable("world-host.share_world.failed").withStyle(ChatFormatting.RED);
Expand Down
Expand Up @@ -2,7 +2,6 @@

import io.github.gaming32.worldhost.WorldHost;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket;
import net.minecraft.network.protocol.status.ServerStatus;

import java.io.DataOutputStream;
Expand Down Expand Up @@ -88,9 +87,7 @@ record QueryResponse(long connectionId, ServerStatus metadata) implements WorldH
public void encode(DataOutputStream dos) throws IOException {
dos.writeByte(7);
dos.writeLong(connectionId);
final FriendlyByteBuf buf = WorldHost.createByteBuf();
new ClientboundStatusResponsePacket(metadata != null ? metadata : WorldHost.createEmptyServerStatus())
.write(buf);
final FriendlyByteBuf buf = WorldHost.writeServerStatus(metadata);
dos.writeInt(buf.readableBytes());
buf.readBytes(dos, buf.readableBytes());
}
Expand Down Expand Up @@ -126,9 +123,7 @@ record NewQueryResponse(long connectionId, ServerStatus metadata) implements Wor
public void encode(DataOutputStream dos) throws IOException {
dos.writeByte(11);
dos.writeLong(connectionId);
final FriendlyByteBuf buf = WorldHost.createByteBuf();
new ClientboundStatusResponsePacket(metadata != null ? metadata : WorldHost.createEmptyServerStatus())
.write(buf);
final FriendlyByteBuf buf = WorldHost.writeServerStatus(metadata);
buf.readBytes(dos, buf.readableBytes());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/mods.toml
Expand Up @@ -43,9 +43,9 @@ modId = "minecraft"
##if MC == 1.20.1
#?? versionRange = "[1.20,1.20.2)"
##elseif MC == 1.20.4
versionRange = "[1.20.3,1.20.5)"
#?? versionRange = "[1.20.3,1.20.5)"
##else
#?? versionRange = "$mc_version"
versionRange = "$mc_version"
##endif
##if MC < 1.20.4
#?? mandatory = true
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/neoforge.mods.toml
Expand Up @@ -26,7 +26,7 @@ side = "BOTH"
[[dependencies.world_host]]
modId = "minecraft"
##if MC == 1.20.6
#?? versionRange = "[1.20.5,1.20.7)"
versionRange = "[1.20.5,1.20.7)"
##else
#?? versionRange = "$mc_version"
##endif
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Expand Up @@ -33,9 +33,9 @@
//#elseif MC == 1.20.1
//?? "minecraft": ">=1.20- <1.20.2",
//#elseif MC == 1.20.4
"minecraft": ">=1.20.3- <1.20.5",
//?? "minecraft": ">=1.20.3- <1.20.5",
//#elseif MC == 1.20.6
//?? "minecraft": ">=1.20.5- <1.20.7",
"minecraft": ">=1.20.5- <1.20.7",
//#else
//?? "minecraft": "$mc_version",
//#endif
Expand Down
2 changes: 1 addition & 1 deletion versions/mainProject
@@ -1 +1 @@
1.20.4-fabric
1.20.6-fabric

0 comments on commit f9e5168

Please sign in to comment.