Skip to content

Commit 295a63f

Browse files
committed
fix update check and add update type
1 parent 1fc9ad5 commit 295a63f

File tree

7 files changed

+124
-56
lines changed

7 files changed

+124
-56
lines changed

core/src/main/java/org/vivecraft/ViveMain.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.vivecraft.network.NetworkHandler;
2121
import org.vivecraft.util.JsonUtils;
2222
import org.vivecraft.util.MCVersion;
23+
import org.vivecraft.util.UpdateChecker;
2324

2425
import java.util.HashMap;
2526
import java.util.List;
@@ -76,6 +77,10 @@ public void onEnable() {
7677
// set up config
7778
CONFIG = new Config(this);
7879

80+
if (CONFIG.checkForUpdates.get()) {
81+
UpdateChecker.scheduleUpdateCheck(LOGGER::info);
82+
}
83+
7984
if (!PermissionManager.checkForVault() && ViveMain.CONFIG.permissionsGroupsEnabled.get()) {
8085
ViveMain.LOGGER.warning("To use the permission groups feature, 'Vault' needs to be installed");
8186
}

core/src/main/java/org/vivecraft/config/Config.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import org.vivecraft.ViveMain;
55
import org.vivecraft.config.enums.ClimbeyBlockmode;
66
import org.vivecraft.config.enums.HeadshotIndicator;
7+
import org.vivecraft.config.enums.UpdateType;
78
import org.vivecraft.network.NetworkHandler;
89
import org.vivecraft.network.PacketUtils;
910
import org.vivecraft.network.packet.s2c.AttackWhileBlockingPayloadS2C;
1011
import org.vivecraft.network.packet.s2c.CrawlPayloadS2C;
1112
import org.vivecraft.network.packet.s2c.DualWieldingPayloadS2C;
1213
import org.vivecraft.network.packet.s2c.TeleportPayloadS2C;
1314
import org.vivecraft.util.MCVersion;
15+
import org.vivecraft.util.UpdateChecker;
1416

1517
import java.io.*;
1618
import java.util.*;
@@ -30,6 +32,7 @@ public class Config {
3032

3133
// general
3234
public final ConfigBuilder.BooleanValue checkForUpdates;
35+
public final ConfigBuilder.EnumValue<UpdateType> updateType;
3336
public final ConfigBuilder.BooleanValue vrOnly;
3437
public final ConfigBuilder.BooleanValue viveOnly;
3538
public final ConfigBuilder.BooleanValue allowOp;
@@ -151,19 +154,32 @@ public Config(Plugin plugin, boolean inMemory) {
151154
.push("general");
152155
this.checkForUpdates = this.builder
153156
.push("checkForUpdate")
154-
.define(true);
157+
.define(true)
158+
.setOnUpdate((oV, nV, notifier) -> {
159+
if (nV) {
160+
UpdateChecker.scheduleUpdateCheck(notifier);
161+
}
162+
});
163+
this.updateType = this.builder
164+
.push("checkForUpdateType")
165+
.defineEnum(UpdateType.RELEASE, UpdateType.class)
166+
.setOnUpdate((oV, nV, notifier) -> {
167+
if (this.checkForUpdates.get() && oV != nV) {
168+
UpdateChecker.scheduleUpdateCheck(notifier);
169+
}
170+
});
155171
this.vrOnly = this.builder
156172
.push("vr_only")
157173
.define(false)
158-
.setOnUpdate((oV, nV) -> NetworkHandler.updateViveVROnly());
174+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> NetworkHandler.updateViveVROnly());
159175
this.viveOnly = this.builder
160176
.push("vive_only")
161177
.define(false)
162-
.setOnUpdate((oV, nV) -> NetworkHandler.updateViveVROnly());
178+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> NetworkHandler.updateViveVROnly());
163179
this.allowOp = this.builder
164180
.push("allow_op")
165181
.define(true)
166-
.setOnUpdate((oV, nV) -> NetworkHandler.updateViveVROnly());
182+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> NetworkHandler.updateViveVROnly());
167183
this.messageKickDelay = this.builder
168184
.push("messageAndKickDelay")
169185
.defineInRange(200, 100, 1000);
@@ -173,15 +189,15 @@ public Config(Plugin plugin, boolean inMemory) {
173189
this.viveCrafting = this.builder
174190
.push("viveCrafting")
175191
.define(true)
176-
.setOnUpdate((oV, nV) -> ViveMain.INSTANCE.updateRecipes());
192+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> ViveMain.INSTANCE.updateRecipes());
177193
this.requestData = this.builder
178194
.push("requestData")
179195
.define(true)
180196
.setNeedsReload(true);
181197
this.sendData = this.builder
182198
.push("sendData")
183199
.define(true)
184-
.setOnUpdate((oV, nV) -> ViveMain.INSTANCE.toggleDataTask(nV));
200+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> ViveMain.INSTANCE.toggleDataTask(nV));
185201
this.sendDataToOwner = this.builder
186202
.push("sendDataToOwner")
187203
.define(false);
@@ -372,7 +388,7 @@ public Config(Plugin plugin, boolean inMemory) {
372388
.push("enabled")
373389
.define(false)
374390
.setPacketFunction((v, p) -> PacketUtils.getClimbeyServerPayload(p))
375-
.setOnUpdate((oV, nV) -> ViveMain.INSTANCE.updateRecipes());
391+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> ViveMain.INSTANCE.updateRecipes());
376392
this.climbeyBlockmode = this.builder
377393
.push("blockmode")
378394
.defineEnum(ClimbeyBlockmode.DISABLED, ClimbeyBlockmode.class)
@@ -391,7 +407,7 @@ public Config(Plugin plugin, boolean inMemory) {
391407
this.crawlingEnabled = this.builder
392408
.push("enabled")
393409
.define(true)
394-
.setOnUpdate((oV, nV) -> {
410+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> {
395411
if (!nV) {
396412
// disable crawling for everyone
397413
ViveMain.VIVE_PLAYERS.values().forEach(vp -> vp.crawling = false);
@@ -478,7 +494,7 @@ public Config(Plugin plugin, boolean inMemory) {
478494
this.debugParticlesEnabled = this.builder
479495
.push("enabled")
480496
.define(false)
481-
.setOnUpdate((oV, nV) -> ViveMain.INSTANCE.toggleParticleTask(nV));
497+
.setOnUpdate((ConfigBuilder.SimpleUpdateNotifier<Boolean>) nV -> ViveMain.INSTANCE.toggleParticleTask(nV));
482498
this.debugParticlesOpOnly = this.builder
483499
.push("opOnly")
484500
.define(true);

core/src/main/java/org/vivecraft/config/ConfigBuilder.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.vivecraft.util.Utils;
1111

1212
import java.util.*;
13-
import java.util.function.BiConsumer;
1413
import java.util.function.BiFunction;
1514
import java.util.function.Consumer;
1615

@@ -307,7 +306,7 @@ public static class ConfigValue<T> {
307306
/**
308307
* Consumer that takes the old and new value to send updates
309308
*/
310-
private BiConsumer<T, T> updateConsumer = null;
309+
private UpdateNotifier<T> updateConsumer = null;
311310

312311
public ConfigValue(ConfigBuilder config, String path, T defaultValue) {
313312
this.config = config;
@@ -367,14 +366,14 @@ public String getPath() {
367366
}
368367

369368
@SuppressWarnings("unchecked")
370-
public <V extends ConfigValue<T>> V setOnUpdate(BiConsumer<T, T> onUpdate) {
369+
public <V extends ConfigValue<T>> V setOnUpdate(UpdateNotifier<T> onUpdate) {
371370
this.updateConsumer = onUpdate;
372371
return (V) this;
373372
}
374373

375374
public void onUpdate(T oldValue, T newValue, @Nullable Consumer<String> notifier) {
376375
if (this.updateConsumer != null) {
377-
this.updateConsumer.accept(oldValue, newValue);
376+
this.updateConsumer.onUpdate(oldValue, newValue, notifier);
378377
}
379378
NetworkHandler.sendUpdatePacketToAll(this, notifier);
380379
}
@@ -572,4 +571,27 @@ protected Double getFromConfig() {
572571
return this.config.getConfig().getDouble(this.path);
573572
}
574573
}
574+
575+
@FunctionalInterface
576+
public interface UpdateNotifier<T> {
577+
void onUpdate(T oldValue, T newValue, @Nullable Consumer<String> notifier);
578+
}
579+
580+
@FunctionalInterface
581+
public interface SingleUpdateNotifier<T> extends UpdateNotifier<T> {
582+
void onUpdate(T newValue, @Nullable Consumer<String> notifier);
583+
584+
default void onUpdate(T oldValue, T newValue, @Nullable Consumer<String> notifier) {
585+
this.onUpdate(newValue, notifier);
586+
}
587+
}
588+
589+
@FunctionalInterface
590+
public interface SimpleUpdateNotifier<T> extends SingleUpdateNotifier<T> {
591+
void onUpdate(T newValue);
592+
593+
default void onUpdate(T newValue, @Nullable Consumer<String> notifier) {
594+
this.onUpdate(newValue);
595+
}
596+
}
575597
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.vivecraft.config.enums;
2+
3+
public enum UpdateType {
4+
RELEASE,
5+
BETA,
6+
ALPHA
7+
}

core/src/main/java/org/vivecraft/events/PlayerEvents.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.vivecraft.network.NetworkUtils;
1919
import org.vivecraft.util.MetadataHelper;
2020
import org.vivecraft.util.UpdateChecker;
21-
import org.vivecraft.util.Utils;
2221

2322
import java.util.Random;
2423

@@ -82,14 +81,7 @@ public void onPlayerConnect(PlayerJoinEvent event) {
8281
}, ViveMain.CONFIG.messageKickDelay.get());
8382

8483
if (ViveMain.CONFIG.checkForUpdates.get() && player.isOp()) {
85-
// check for update on not the main thread
86-
Platform.getInstance().getScheduler().runAsync(() -> {
87-
if (UpdateChecker.checkForUpdates()) {
88-
Platform.getInstance().getScheduler().runGlobal(
89-
() -> player.sendMessage(ViveMain.translate("vivecraft.plugin.update",
90-
Utils.green(UpdateChecker.NEWEST_VERSION))));
91-
}
92-
});
84+
UpdateChecker.scheduleUpdateCheck(player::sendMessage);
9385
}
9486

9587
new AimFixHandler(player, ViveMain.NMS.getConnection(player));

0 commit comments

Comments
 (0)