Skip to content

Commit

Permalink
Use var where it saves imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 9, 2024
1 parent c99101b commit 46bbc49
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.UUID;

public interface FriendsListUpdate {
// TODO: fastutil
void friendsListUpdate(Map<UUID, Long> friends);

default void friendsListUpdate() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/github/gaming32/worldhost/ProxyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.gaming32.worldhost.protocol.proxy.ProxyPassthrough;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
Expand Down Expand Up @@ -38,7 +37,7 @@ public ProxyClient(
public void run() {
WorldHost.LOGGER.info("Starting proxy client from {}", remoteAddress);
try {
final InputStream is = socket.getInputStream();
final var is = socket.getInputStream();
final byte[] b = new byte[0xffff];
int n;
while ((n = is.read(b)) != -1) {
Expand Down
48 changes: 26 additions & 22 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.github.gaming32.worldhost.toast.WHToast;
import io.github.gaming32.worldhost.upnp.Gateway;
import io.github.gaming32.worldhost.upnp.GatewayFinder;
import io.github.gaming32.worldhost.upnp.UPnPErrors;
import io.github.gaming32.worldhost.versions.Components;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
Expand All @@ -27,8 +26,6 @@
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.multiplayer.resolver.ServerAddress;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.SkinManager;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.ClickEvent;
Expand All @@ -37,9 +34,6 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.players.GameProfileCache;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
Expand All @@ -49,7 +43,11 @@
import org.quiltmc.parsers.json.JsonWriter;
import org.semver4j.Semver;

import java.io.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -58,7 +56,16 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.security.SecureRandom;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.Consumer;
Expand All @@ -84,7 +91,6 @@
//#endif

//#if MC >= 1.20.2
import com.mojang.authlib.yggdrasil.ProfileResult;
import net.minecraft.client.resources.PlayerSkin;
//#endif

Expand All @@ -93,8 +99,6 @@
import net.fabricmc.loader.api.FabricLoader;
//#if MC >= 1.18.2
import dev.isxander.mainmenucredits.MainMenuCredits;
import dev.isxander.mainmenucredits.config.MMCConfig;
import dev.isxander.mainmenucredits.config.MMCConfigEntry;
import io.github.gaming32.worldhost.gui.OnlineStatusLocation;
//#endif
//#else
Expand Down Expand Up @@ -185,7 +189,7 @@ public class WorldHost
public static final WorldHostConfig CONFIG = new WorldHostConfig();

private static List<String> wordsForCid;
private static Map<String, Integer> wordsForCidInverse;
private static Map<String, Integer> wordsForCidInverse; // TODO: fastutil

public static final long MAX_CONNECTION_IDS = 1L << 42;

Expand Down Expand Up @@ -349,7 +353,7 @@ public static void tickHandler() {
LOGGER.info("Finished authenticating with WH server. Requesting friends list.");
ONLINE_FRIENDS.clear();
protoClient.listOnline(CONFIG.getFriends());
final IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
final var server = Minecraft.getInstance().getSingleplayerServer();
if (server != null && server.isPublished()) {
protoClient.publishedWorld(CONFIG.getFriends());
}
Expand All @@ -373,7 +377,7 @@ public static void commandRegistrationHandler(CommandDispatcher<CommandSourceSta
.executes(ctx -> {
try {
final int port = ctx.getSource().getServer().getPort();
final UPnPErrors.AddPortMappingErrors error = upnpGateway.openPort(port, 60, false);
final var error = upnpGateway.openPort(port, 60, false);
if (error == null) {
ctx.getSource().sendSuccess(
//#if MC >= 1.20.0
Expand Down Expand Up @@ -457,7 +461,7 @@ public static GameProfileCache getProfileCache() {
}

public static CompletableFuture<ResourceLocation> getInsecureSkinLocation(GameProfile gameProfile) {
final SkinManager skinManager = Minecraft.getInstance().getSkinManager();
final var skinManager = Minecraft.getInstance().getSkinManager();
//#if MC >= 1.20.2
return skinManager.getOrLoad(gameProfile).thenApply(PlayerSkin::texture);
//#elseif MC >= 1.19.2
Expand Down Expand Up @@ -497,7 +501,7 @@ public static GameProfile fetchProfile(MinecraftSessionService sessionService, U
//#if MC < 1.20.2
//$$ return sessionService.fillProfileProperties(fallback != null ? fallback : new GameProfile(uuid, null), false);
//#else
final ProfileResult result = sessionService.fetchProfile(uuid, false);
final var result = sessionService.fetchProfile(uuid, false);
if (result == null) {
return fallback != null ? fallback : new GameProfile(uuid, "");
}
Expand Down Expand Up @@ -710,7 +714,7 @@ private static int ipCommand(CommandContext<CommandSourceStack> ctx) {
}

public static void proxyConnect(long connectionId, InetAddress remoteAddr, Supplier<ProxyPassthrough> proxy) {
final IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
final var server = Minecraft.getInstance().getSingleplayerServer();
if (server == null || !server.isPublished()) {
if (protoClient != null) {
protoClient.proxyDisconnect(connectionId);
Expand Down Expand Up @@ -770,8 +774,8 @@ public static String getModVersion(String modId) {
//#if FABRIC && MC >= 1.18.2
public static int getMMCLines(boolean isPause) {
if (FabricLoader.getInstance().isModLoaded("isxander-main-menu-credits")) {
final MMCConfig baseConfig = MainMenuCredits.getInstance().getConfig();
final MMCConfigEntry config = isPause ? baseConfig.PAUSE_MENU : baseConfig.MAIN_MENU;
final var baseConfig = MainMenuCredits.getInstance().getConfig();
final var config = isPause ? baseConfig.PAUSE_MENU : baseConfig.MAIN_MENU;
return (CONFIG.getOnlineStatusLocation() == OnlineStatusLocation.RIGHT ? config.getBottomRight() : config.getBottomLeft()).size();
}
return 0;
Expand All @@ -792,12 +796,12 @@ public static <T> T httpGet(
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
try (CloseableHttpResponse response = client.execute(new HttpGet(uri))) {
final StatusLine status = response.getStatusLine();
try (var response = client.execute(new HttpGet(uri))) {
final var status = response.getStatusLine();
if (status.getStatusCode() != 200) {
throw new IOException("Failed to GET " + uri + ": " + status.getStatusCode() + " " + status.getReasonPhrase());
}
final HttpEntity entity = response.getEntity();
final var entity = response.getEntity();
if (entity == null) {
throw new IOException("GET " + uri + " returned no body.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.players.GameProfileCache;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
Expand Down Expand Up @@ -177,7 +176,7 @@ public void render(

if (friendProfile != null) {
assert minecraft != null;
final ResourceLocation skinTexture = WorldHost.getSkinLocationNow(friendProfile);
final var skinTexture = WorldHost.getSkinLocationNow(friendProfile);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.enableBlend();
//#if MC >= 1.19.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.ConfirmScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -124,7 +121,7 @@ private void addFriendAndUpdate(GameProfile profile) {
public static void addFriend(GameProfile profile) {
WorldHost.CONFIG.getFriends().add(profile.getId());
WorldHost.saveConfig();
final IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
final var server = Minecraft.getInstance().getSingleplayerServer();
if (server != null && server.isPublished() && WorldHost.protoClient != null) {
WorldHost.protoClient.publishedWorld(Collections.singleton(profile.getId()));
}
Expand Down Expand Up @@ -157,7 +154,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (mouseX >= textX && mouseX <= textX + textWidth) {
final int textY = height - 66 - font.lineHeight / 2;
if (mouseY >= textY && mouseY <= textY + font.lineHeight) {
final Style component = font.getSplitter().componentStyleAtWidth(BEDROCK_FRIENDS_TEXT, (int)Math.round(mouseX) - textX);
final var component = font.getSplitter().componentStyleAtWidth(BEDROCK_FRIENDS_TEXT, (int)Math.round(mouseX) - textX);
if (component != null) {
handleComponentClicked(component);
return true;
Expand Down Expand Up @@ -226,7 +223,7 @@ public void render(
//#endif
int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta
) {
final ResourceLocation skinTexture = WorldHost.getSkinLocationNow(profile);
final var skinTexture = WorldHost.getSkinLocationNow(profile);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.enableBlend();
blit(context, skinTexture, x, y, 32, 32, 8, 8, 8, 8, 64, 64);
Expand All @@ -247,7 +244,7 @@ public void maybeRemove() {
WorldHost.CONFIG.getFriends().remove(profile.getId());
WorldHost.saveConfig();
FriendsScreen.this.list.updateEntries();
final IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
final var server = Minecraft.getInstance().getSingleplayerServer();
if (server != null && server.isPublished() && WorldHost.protoClient != null) {
WorldHost.protoClient.closedWorld(Collections.singleton(profile.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.status.ServerStatus;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

//#if MC >= 1.20.0
import net.minecraft.client.gui.GuiGraphics;
Expand Down Expand Up @@ -419,7 +423,7 @@ public void render(

private void updateServerInfo() {
serverInfo.name = getName();
final ServerStatus metadata = WorldHost.ONLINE_FRIEND_PINGS.get(profile.getId());
final var metadata = WorldHost.ONLINE_FRIEND_PINGS.get(profile.getId());
if (metadata == null) {
serverInfo.status = Components.EMPTY;
serverInfo.motd = Components.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

//#if MC < 1.19.4
//$$ import net.minecraft.client.Minecraft;
//$$ import net.minecraft.client.gui.components.Button;
//$$ import org.jetbrains.annotations.NotNull;
//$$
//$$ import java.util.List;
//$$ import java.util.function.Consumer;
//#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
Expand Down Expand Up @@ -35,7 +34,7 @@ public FriendsButton(int x, int y, int width, int height, OnPress onPress) {
@Override
public void friendsListUpdate(Map<UUID, Long> friends) {
final int online = friends.size();
final Component baseText = WorldHostComponents.FRIENDS.copy().append(" " + online + " ");
final var baseText = WorldHostComponents.FRIENDS.copy().append(" " + online + " ");
setMessage(baseText);
final Font textRenderer = Minecraft.getInstance().font;
bgX = width / 2 - textRenderer.width(baseText) / 2 + textRenderer.width(WorldHostComponents.FRIENDS.copy().append(" "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(SelectWorldScreen.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import org.apache.commons.io.input.CountingInputStream;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.gaming32.worldhost.protocol;

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

import java.io.DataOutputStream;
Expand Down Expand Up @@ -87,7 +86,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.writeServerStatus(metadata);
final var buf = WorldHost.writeServerStatus(metadata);
dos.writeInt(buf.readableBytes());
buf.readBytes(dos, buf.readableBytes());
}
Expand Down Expand Up @@ -123,7 +122,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.writeServerStatus(metadata);
final var buf = WorldHost.writeServerStatus(metadata);
buf.readBytes(dos, buf.readableBytes());
}
}
Expand Down

0 comments on commit 46bbc49

Please sign in to comment.