Skip to content

Commit be16f5e

Browse files
committed
feat: update UpdateSystem form Orebfuscator
chore: remove unused class
1 parent 0d3ef90 commit be16f5e

File tree

7 files changed

+426
-218
lines changed

7 files changed

+426
-218
lines changed

zip-common/src/main/java/net/imprex/zip/common/ItemStackWithSlot.java

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 21 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
/**
2+
* @author Imprex-Development
3+
* @see <a href="https://github.com/Imprex-Development/orebfuscator/blob/master/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java">MinecraftVersion.java</a>
4+
*/
15
package net.imprex.zip.common;
26

37
import java.util.ArrayList;
48
import java.util.List;
5-
import java.util.Objects;
69
import java.util.regex.Matcher;
710
import java.util.regex.Pattern;
811

912
import org.bukkit.Bukkit;
1013

11-
/**
12-
* @author Imprex-Development
13-
* @see https://github.com/Imprex-Development/orebfuscator/blob/master/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java
14-
*/
15-
public final class MinecraftVersion implements Comparable<MinecraftVersion> {
14+
public final class MinecraftVersion {
1615

1716
private static final class NmsMapping {
1817

@@ -27,11 +26,11 @@ private static final class NmsMapping {
2726
MAPPINGS.add(new NmsMapping("1.20.5", "v1_20_R4"));
2827
}
2928

30-
public static String get(MinecraftVersion version) {
29+
public static String get(Version version) {
3130
for (NmsMapping mapping : MAPPINGS) {
3231
if (version.isAtOrAbove(mapping.version)) {
3332
if (mapping.version.minor() != version.minor()) {
34-
System.out.println(String.format("Using nms mapping with mismatched minor versions: %s - %s",
33+
ZIPLogger.warn(String.format("Using nms mapping with mismatched minor versions: %s - %s",
3534
mapping.version, version));
3635
}
3736

@@ -42,19 +41,17 @@ public static String get(MinecraftVersion version) {
4241
throw new RuntimeException("Can't get nms package version for minecraft version: " + version);
4342
}
4443

45-
private final MinecraftVersion version;
44+
private final Version version;
4645
private final String nmsVersion;
4746

4847
public NmsMapping(String version, String nmsVersion) {
49-
this.version = new MinecraftVersion(version);
48+
this.version = Version.parse(version);
5049
this.nmsVersion = nmsVersion;
5150
}
5251
}
5352

54-
private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>\\d+)(?:\\.(?<minor>\\d+))(?:\\.(?<patch>\\d+))?");
5553
private static final Pattern PACKAGE_PATTERN = Pattern.compile("org\\.bukkit\\.craftbukkit\\.(v\\d+_\\d+_R\\d+)");
56-
57-
private static final MinecraftVersion CURRENT_VERSION = new MinecraftVersion(Bukkit.getBukkitVersion());
54+
private static final Version CURRENT_VERSION = Version.parse(Bukkit.getBukkitVersion());
5855

5956
private static String NMS_VERSION;
6057

@@ -73,118 +70,35 @@ public static String nmsVersion() {
7370
return NMS_VERSION;
7471
}
7572

73+
public static Version current() {
74+
return CURRENT_VERSION;
75+
}
76+
7677
public static int majorVersion() {
77-
return CURRENT_VERSION.major;
78+
return CURRENT_VERSION.major();
7879
}
7980

8081
public static int minorVersion() {
81-
return CURRENT_VERSION.minor;
82+
return CURRENT_VERSION.minor();
8283
}
8384

8485
public static int patchVersion() {
85-
return CURRENT_VERSION.patch;
86+
return CURRENT_VERSION.patch();
8687
}
8788

8889
public static boolean isAbove(String version) {
89-
return CURRENT_VERSION.isAbove(new MinecraftVersion(version));
90+
return CURRENT_VERSION.isAbove(Version.parse(version));
9091
}
9192

9293
public static boolean isAtOrAbove(String version) {
93-
return CURRENT_VERSION.isAtOrAbove(new MinecraftVersion(version));
94+
return CURRENT_VERSION.isAtOrAbove(Version.parse(version));
9495
}
9596

9697
public static boolean isAtOrBelow(String version) {
97-
return CURRENT_VERSION.isAtOrBelow(new MinecraftVersion(version));
98+
return CURRENT_VERSION.isAtOrBelow(Version.parse(version));
9899
}
99100

100101
public static boolean isBelow(String version) {
101-
return CURRENT_VERSION.isBelow(new MinecraftVersion(version));
102-
}
103-
104-
private final int major;
105-
private final int minor;
106-
private final int patch;
107-
108-
public MinecraftVersion(String version) {
109-
Matcher matcher = VERSION_PATTERN.matcher(version);
110-
111-
if (!matcher.find()) {
112-
throw new IllegalArgumentException("can't parse minecraft version: " + version);
113-
}
114-
115-
this.major = Integer.parseInt(matcher.group("major"));
116-
this.minor = Integer.parseInt(matcher.group("minor"));
117-
118-
String patch = matcher.group("patch");
119-
if (patch != null) {
120-
this.patch = Integer.parseInt(patch);
121-
} else {
122-
this.patch = 0;
123-
}
124-
}
125-
126-
public int major() {
127-
return this.major;
128-
}
129-
130-
public int minor() {
131-
return this.minor;
132-
}
133-
134-
public int patch() {
135-
return this.patch;
136-
}
137-
138-
public boolean isAbove(MinecraftVersion version) {
139-
return this.compareTo(version) > 0;
140-
}
141-
142-
public boolean isAtOrAbove(MinecraftVersion version) {
143-
return this.compareTo(version) >= 0;
144-
}
145-
146-
public boolean isAtOrBelow(MinecraftVersion version) {
147-
return this.compareTo(version) <= 0;
148-
}
149-
150-
public boolean isBelow(MinecraftVersion version) {
151-
return this.compareTo(version) < 0;
152-
}
153-
154-
@Override
155-
public int compareTo(MinecraftVersion other) {
156-
int major = Integer.compare(this.major, other.major);
157-
if (major != 0) {
158-
return major;
159-
}
160-
161-
int minor = Integer.compare(this.minor, other.minor);
162-
if (minor != 0) {
163-
return minor;
164-
}
165-
166-
return Integer.compare(this.patch, other.patch);
167-
}
168-
169-
@Override
170-
public int hashCode() {
171-
return Objects.hash(major, minor, patch);
172-
}
173-
174-
@Override
175-
public boolean equals(Object obj) {
176-
if (this == obj) {
177-
return true;
178-
}
179-
if (!(obj instanceof MinecraftVersion)) {
180-
return false;
181-
}
182-
MinecraftVersion other = (MinecraftVersion) obj;
183-
return major == other.major && minor == other.minor && patch == other.patch;
184-
}
185-
186-
@Override
187-
public String toString() {
188-
return String.format("%s.%s.%s", this.major, this.minor, this.patch);
102+
return CURRENT_VERSION.isBelow(Version.parse(version));
189103
}
190104
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @author Imprex-Development
3+
* @see <a href="https://github.com/Imprex-Development/orebfuscator/blob/master/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/Version.java">Version.java</a>
4+
*/
5+
package net.imprex.zip.common;
6+
7+
import java.io.IOException;
8+
import java.util.Objects;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
12+
import com.google.gson.TypeAdapter;
13+
import com.google.gson.stream.JsonReader;
14+
import com.google.gson.stream.JsonWriter;
15+
16+
public record Version(int major, int minor, int patch) implements Comparable<Version> {
17+
18+
private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>\\d+)(?:\\.(?<minor>\\d+))?(?:\\.(?<patch>\\d+))?");
19+
20+
public static Version parse(String version) {
21+
Matcher matcher = VERSION_PATTERN.matcher(version);
22+
23+
if (!matcher.find()) {
24+
throw new IllegalArgumentException("can't parse version: " + version);
25+
}
26+
27+
int major = Integer.parseInt(matcher.group("major"));
28+
29+
String minorGroup = matcher.group("minor");
30+
int minor = minorGroup != null
31+
? Integer.parseInt(minorGroup)
32+
: 0;
33+
34+
String patchGroup = matcher.group("patch");
35+
int patch = patchGroup != null
36+
? Integer.parseInt(patchGroup)
37+
: 0;
38+
39+
return new Version(major, minor, patch);
40+
}
41+
42+
public boolean isAbove(Version version) {
43+
return this.compareTo(version) > 0;
44+
}
45+
46+
public boolean isAtOrAbove(Version version) {
47+
return this.compareTo(version) >= 0;
48+
}
49+
50+
public boolean isAtOrBelow(Version version) {
51+
return this.compareTo(version) <= 0;
52+
}
53+
54+
public boolean isBelow(Version version) {
55+
return this.compareTo(version) < 0;
56+
}
57+
58+
@Override
59+
public int compareTo(Version other) {
60+
int major = Integer.compare(this.major, other.major);
61+
if (major != 0) {
62+
return major;
63+
}
64+
65+
int minor = Integer.compare(this.minor, other.minor);
66+
if (minor != 0) {
67+
return minor;
68+
}
69+
70+
return Integer.compare(this.patch, other.patch);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(major, minor, patch);
76+
}
77+
78+
@Override
79+
public boolean equals(Object obj) {
80+
if (this == obj) {
81+
return true;
82+
}
83+
if (!(obj instanceof Version)) {
84+
return false;
85+
}
86+
Version other = (Version) obj;
87+
return major == other.major && minor == other.minor && patch == other.patch;
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return String.format("%s.%s.%s", this.major, this.minor, this.patch);
93+
}
94+
95+
public static final class Json extends TypeAdapter<Version> {
96+
97+
@Override
98+
public void write(JsonWriter out, Version value) throws IOException {
99+
out.value(value.toString());
100+
}
101+
102+
@Override
103+
public Version read(JsonReader in) throws IOException {
104+
return Version.parse(in.nextString());
105+
}
106+
}
107+
}

zip-common/src/main/java/net/imprex/zip/common/ZIPLogger.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public static void setVerbose(boolean verbose) {
1616
ZIPLogger.verbose = verbose;
1717
}
1818

19+
public static void log(Level level, String message) {
20+
ZIPLogger.logger.log(level, LOG_PREFIX + message);
21+
}
22+
1923
public static void debug(String message) {
2024
if (ZIPLogger.verbose) {
2125
ZIPLogger.logger.log(Level.FINE, LOG_DEBUG_PREFIX + message);
@@ -30,6 +34,10 @@ public static void warn(String message) {
3034
ZIPLogger.logger.log(Level.WARNING, LOG_PREFIX + message);
3135
}
3236

37+
public static void warn(String message, Throwable throwable) {
38+
ZIPLogger.logger.log(Level.WARNING, LOG_PREFIX + message, throwable);
39+
}
40+
3341
public static void error(String message, Throwable throwable) {
3442
ZIPLogger.logger.log(Level.SEVERE, LOG_PREFIX + message, throwable);
3543
}

0 commit comments

Comments
 (0)