remove some java warning, improve code

This commit is contained in:
Bixilon 2020-12-18 19:17:01 +01:00
parent d5707abc30
commit 14d25bcbb7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
14 changed files with 43 additions and 65 deletions

View File

@ -132,7 +132,7 @@ public class AssetsManager {
private static void downloadAsset(AssetsSource source, String hash) throws Exception {
switch (source) {
case MOJANG -> downloadAsset(String.format("https://resources.download.minecraft.net/%s/%s", hash.substring(0, 2), hash), hash);
case MOJANG -> downloadAsset(String.format(ProtocolDefinition.MOJANG_URL_RESOURCES, hash.substring(0, 2), hash), hash);
case MINOSOFT_GIT -> downloadAsset(String.format(Minosoft.getConfig().getString(ConfigurationPaths.StringPaths.RESOURCES_URL), hash.substring(0, 2), hash), hash, false);
}
}
@ -193,7 +193,7 @@ public class AssetsManager {
Log.verbose("client.jar assets probably already generated, skipping");
return;
}
JsonObject manifest = HTTP.getJson("https://launchermeta.mojang.com/mc/game/version_manifest.json").getAsJsonObject();
JsonObject manifest = HTTP.getJson(ProtocolDefinition.MOJANG_URL_VERSION_MANIFEST).getAsJsonObject();
String assetsVersionJsonUrl = null;
for (JsonElement versionElement : manifest.getAsJsonArray("versions")) {
JsonObject version = versionElement.getAsJsonObject();
@ -205,7 +205,7 @@ public class AssetsManager {
if (assetsVersionJsonUrl == null) {
throw new RuntimeException(String.format("Invalid version manifest or invalid ASSETS_CLIENT_JAR_VERSION (%s)", ASSETS_CLIENT_JAR_VERSION));
}
String versionJsonHash = assetsVersionJsonUrl.replace("https://launchermeta.mojang.com/v1/packages/", "").replace(String.format("/%s.json", ASSETS_CLIENT_JAR_VERSION), "");
String versionJsonHash = assetsVersionJsonUrl.replace(ProtocolDefinition.MOJANG_URL_PACKAGES, "").replace(String.format("/%s.json", ASSETS_CLIENT_JAR_VERSION), "");
downloadAsset(assetsVersionJsonUrl, versionJsonHash);
// download jar
JsonObject clientJarJson = readJsonAssetByHash(versionJsonHash).getAsJsonObject().getAsJsonObject("downloads").getAsJsonObject("client");

View File

@ -167,7 +167,7 @@ public class Slot {
return this.item;
}
@IntRange(from = 0, to = 64)
@IntRange(from = 0, to = ProtocolDefinition.ITEM_STACK_MAX_SIZE)
public int getItemCount() {
return this.itemCount;
}

View File

@ -13,15 +13,16 @@
package de.bixilon.minosoft.modding.event.events;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketPluginMessageReceiving;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
public class PluginMessageReceiveEvent extends CancelableEvent {
private final String channel;
private final ModIdentifier channel;
private final InByteBuffer data;
public PluginMessageReceiveEvent(Connection connection, String channel, InByteBuffer data) {
public PluginMessageReceiveEvent(Connection connection, ModIdentifier channel, InByteBuffer data) {
super(connection);
this.channel = channel;
this.data = data;
@ -33,7 +34,7 @@ public class PluginMessageReceiveEvent extends CancelableEvent {
this.data = pkg.getDataAsBuffer();
}
public String getChannel() {
public ModIdentifier getChannel() {
return this.channel;
}

View File

@ -166,7 +166,7 @@ public class ModLoader {
ZipFile zipFile = new ZipFile(file);
ModInfo modInfo = new ModInfo(Util.readJsonFromZip("mod.json", zipFile));
if (isModLoaded(modInfo)) {
Log.warn(String.format("Mod %s:%d (uuid=%s) is loaded multiple times! Skipping", modInfo.getName(), modInfo.getVersionId(), modInfo.getUUID()));
Log.warn(String.format("Mod %s:%d (uuid=%s) is loaded multiple times! Skipping", modInfo.getName(), modInfo.getModIdentifier().getVersionId(), modInfo.getModIdentifier().getUUID()));
return null;
}
JarClassLoader jcl = new JarClassLoader();

View File

@ -318,10 +318,8 @@ public class Connection {
// after sending it, switch to next state
setConnectionState(next);
}
case STATUS -> {
// send status request
this.network.sendPacket(new PacketStatusRequest());
}
case STATUS -> // send status request
this.network.sendPacket(new PacketStatusRequest());
case LOGIN -> this.network.sendPacket(new PacketLoginStart(this.player));
case DISCONNECTED -> {
if (this.reason == ConnectionReasons.GET_VERSION) {

View File

@ -26,8 +26,6 @@ import de.bixilon.minosoft.protocol.protocol.*;
import de.bixilon.minosoft.util.ServerAddress;
import de.bixilon.minosoft.util.Util;
import java.io.IOException;
public abstract class Network {
protected final Connection connection;
protected int compressionThreshold = -1;
@ -52,7 +50,7 @@ public abstract class Network {
}
protected ClientboundPacket receiveClientboundPacket(byte[] bytes) throws IOException, PacketParseException {
protected ClientboundPacket receiveClientboundPacket(byte[] bytes) throws PacketParseException {
if (this.compressionThreshold >= 0) {
// compression is enabled
InByteBuffer rawData = new InByteBuffer(bytes, this.connection);

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.config.ConfigurationPaths;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.modding.channels.DefaultPluginChannels;
import de.bixilon.minosoft.modding.event.events.PluginMessageReceiveEvent;
@ -27,14 +28,14 @@ import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W29A;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W31A;
public class PacketPluginMessageReceiving extends ClientboundPacket {
String channel;
ModIdentifier channel;
byte[] data;
Connection connection;
@Override
public boolean read(InByteBuffer buffer) {
this.connection = buffer.getConnection();
this.channel = buffer.readString();
this.channel = buffer.readIdentifier();
// "read" length prefix
if (buffer.getVersionId() < V_14W29A) {
buffer.readShort();
@ -84,7 +85,7 @@ public class PacketPluginMessageReceiving extends ClientboundPacket {
Log.protocol(String.format("[IN] Plugin message received in channel \"%s\" with %s bytes of data", this.channel, this.data.length));
}
public String getChannel() {
public ModIdentifier getChannel() {
return this.channel;
}

View File

@ -19,13 +19,14 @@ import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.nbt.tag.CompoundTag;
import de.bixilon.minosoft.util.nbt.tag.StringTag;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W04A;
public class PacketUpdateSignReceiving extends ClientboundPacket {
final ChatComponent[] lines = new ChatComponent[4];
final ChatComponent[] lines = new ChatComponent[ProtocolDefinition.SIGN_LINES];
BlockPosition position;
@Override
@ -35,7 +36,7 @@ public class PacketUpdateSignReceiving extends ClientboundPacket {
} else {
this.position = buffer.readPosition();
}
for (byte i = 0; i < 4; i++) {
for (byte i = 0; i < ProtocolDefinition.SIGN_LINES; i++) {
this.lines[i] = buffer.readChatComponent();
}
return true;
@ -46,7 +47,7 @@ public class PacketUpdateSignReceiving extends ClientboundPacket {
CompoundTag nbt = new CompoundTag();
nbt.writeBlockPosition(getPosition());
nbt.writeTag("id", new StringTag("minecraft:sign"));
for (int i = 0; i < 4; i++) {
for (int i = 0; i < ProtocolDefinition.SIGN_LINES; i++) {
nbt.writeTag(String.format("Text%d", (i + 1)), new StringTag(getLines()[i].getLegacyText()));
}
// ToDo: handle sign updates

View File

@ -24,8 +24,8 @@ import de.bixilon.minosoft.protocol.protocol.Packets;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W06B;
public class PacketPlayerPositionAndRotationSending implements ServerboundPacket {
Location location;
EntityRotation rotation;
final Location location;
final EntityRotation rotation;
final boolean onGround;
public PacketPlayerPositionAndRotationSending(Location location, EntityRotation rotation, boolean onGround) {

View File

@ -20,6 +20,7 @@ import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.Packets;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.*;
@ -41,11 +42,11 @@ public class PacketUpdateSignSending implements ServerboundPacket {
buffer.writePosition(this.position);
}
if (buffer.getVersionId() < V_14W25A || buffer.getVersionId() >= V_15W35A) {
for (int i = 0; i < 4; i++) {
for (int i = 0; i < ProtocolDefinition.SIGN_LINES; i++) {
buffer.writeString(this.lines[i].getMessage());
}
} else {
for (int i = 0; i < 4; i++) {
for (int i = 0; i < ProtocolDefinition.SIGN_LINES; i++) {
buffer.writeChatComponent(this.lines[i]);
}
}

View File

@ -54,6 +54,18 @@ public final class ProtocolDefinition {
public static final int SECTIONS_PER_CHUNK = 16;
public static final int BLOCKS_PER_SECTION = SECTION_WIDTH_X * SECTION_HEIGHT_Y * SECTION_WIDTH_X;
public static final int SIGN_LINES = 4;
public static final int ITEM_STACK_MAX_SIZE = 64;
public static final String MOJANG_URL_VERSION_MANIFEST = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
public static final String MOJANG_URL_RESOURCES = "https://resources.download.minecraft.net/%s/%s";
public static final String MOJANG_URL_PACKAGES = "https://launchermeta.mojang.com/v1/packages/";
public static final String MOJANG_URL_BLOCKED_SERVERS = "https://sessionserver.mojang.com/blockedservers";
public static final String MOJANG_URL_LOGIN = "https://authserver.mojang.com/authenticate";
public static final String MOJANG_URL_JOIN = "https://sessionserver.mojang.com/session/minecraft/join";
public static final String MOJANG_URL_REFRESH = "https://authserver.mojang.com/refresh";
public static final char[] OBFUSCATED_CHARS = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".toCharArray();

View File

@ -19,6 +19,7 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.config.ConfigurationPaths;
import de.bixilon.minosoft.config.StaticConfiguration;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.HTTP;
import java.net.http.HttpResponse;
@ -41,7 +42,7 @@ public final class MojangAuthentication {
payload.addProperty("clientToken", clientToken);
payload.addProperty("requestUser", true);
HttpResponse<String> response = HTTP.postJson(MojangURLs.LOGIN.getUrl(), payload);
HttpResponse<String> response = HTTP.postJson(ProtocolDefinition.MOJANG_URL_LOGIN, payload);
if (response == null) {
Log.mojang(String.format("Failed to login with username %s", username));
return new MojangAccountAuthenticationAttempt("Unknown error, check your Internet connection");
@ -65,7 +66,7 @@ public final class MojangAuthentication {
payload.addProperty("selectedProfile", account.getUUID().toString().replace("-", ""));
payload.addProperty("serverId", serverId);
HttpResponse<String> response = HTTP.postJson(MojangURLs.JOIN.toString(), payload);
HttpResponse<String> response = HTTP.postJson(ProtocolDefinition.MOJANG_URL_JOIN, payload);
if (response == null) {
Log.mojang(String.format("Failed to join server: %s", serverId));
@ -94,7 +95,7 @@ public final class MojangAuthentication {
HttpResponse<String> response;
try {
response = HTTP.postJson(MojangURLs.REFRESH.getUrl(), payload);
response = HTTP.postJson(ProtocolDefinition.MOJANG_URL_REFRESH, payload);
} catch (Exception e) {
Log.mojang(String.format("Could not connect to mojang server: %s", e.getCause().toString()));
return null;

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.util.mojang.api;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.HTTP;
import de.bixilon.minosoft.util.Util;
@ -25,7 +26,7 @@ import java.util.List;
public final class MojangBlockedServers {
public static ArrayList<String> getBlockedServers() {
HttpResponse<String> response = HTTP.get(MojangURLs.BLOCKED_SERVERS.getUrl());
HttpResponse<String> response = HTTP.get(ProtocolDefinition.MOJANG_URL_BLOCKED_SERVERS);
if (response == null) {
Log.mojang("Failed to fetch blocked servers");
return null;

View File

@ -1,36 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.mojang.api;
public enum MojangURLs {
BLOCKED_SERVERS("https://sessionserver.mojang.com/blockedservers"),
LOGIN("https://authserver.mojang.com/authenticate"),
JOIN("https://sessionserver.mojang.com/session/minecraft/join"),
REFRESH("https://authserver.mojang.com/refresh");
final String url;
MojangURLs(String url) {
this.url = url;
}
public String getUrl() {
return this.url;
}
@Override
public String toString() {
return this.url;
}
}