SteamWar/SpigotCore
Archiviert
13
0

Simplify VersionedCallable

Simplify VersionedRunnable
Dieser Commit ist enthalten in:
jojo 2020-12-24 14:24:48 +01:00
Ursprung 19394d03ee
Commit 65f747fdb3
10 geänderte Dateien mit 53 neuen und 119 gelöschten Zeilen

Datei anzeigen

@ -47,8 +47,8 @@ public class BungeeReceiver implements PluginMessageListener {
UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID();
if(Bukkit.getPlayer(uuid).isOnline()) {
Player player = Bukkit.getPlayer(uuid);
Core.versionDependantCall(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8),
new VersionedRunnable(() -> BungeeReceiver_9.playpling(player)));
VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8),
new VersionedRunnable(() -> BungeeReceiver_9.playpling(player), 9));
}
});

Datei anzeigen

@ -83,83 +83,4 @@ public class Core extends JavaPlugin{
Core.instance = instance;
}
public static void versionDependantCall(Runnable v12, Runnable v15) {
switch (getVersion()) {
case 12:
v12.run();
break;
case 15:
default:
v15.run();
break;
}
}
public static <T> T versionDependantCall(ExceptionlessCallable<T> v12, ExceptionlessCallable<T> v15) {
switch (getVersion()) {
case 12:
return v12.call();
case 15:
default:
return v15.call();
}
}
public static void versionDependantCall(Runnable v8, Runnable v9, Runnable v10, Runnable v12, Runnable v14, Runnable v15) {
switch (Core.getVersion()) {
case 8:
v8.run();
break;
case 9:
v9.run();
break;
case 10:
v10.run();
break;
case 12:
v12.run();
break;
case 14:
v14.run();
break;
case 15:
default:
v15.run();
break;
}
}
public static <T> T versionDependantCall(ExceptionlessCallable<T> v8, ExceptionlessCallable<T> v9, ExceptionlessCallable<T> v10, ExceptionlessCallable<T> v12, ExceptionlessCallable<T> v14, ExceptionlessCallable<T> v15) {
switch (Core.getVersion()) {
case 8:
return v8.call();
case 9:
return v9.call();
case 10:
return v10.call();
case 12:
return v12.call();
case 14:
return v14.call();
case 15:
default:
return v15.call();
}
}
public static void versionDependantCall(VersionedRunnable versionedRunnable1, VersionedRunnable versionedRunnable2) {
if (versionedRunnable1.isVersion()) {
versionedRunnable1.run();
return;
}
versionedRunnable2.run();
}
public static <T> T versionDependantCall(VersionedCallable<T> versionedCallable1, VersionedCallable<T> versionedCallable2) {
if (versionedCallable1.isVersion()) {
return versionedCallable1.call();
}
return versionedCallable2.call();
}
}

Datei anzeigen

@ -71,7 +71,12 @@ public class TPSWatcher {
}
private static double[] getSpigotTPS() {
return Core.versionDependantCall(SpigotTPS_8::getTps, SpigotTPS_9::getTps, SpigotTPS_10::getTps, SpigotTPS_12::getTps, SpigotTPS_14::getTps, SpigotTPS_15::getTps);
return VersionedCallable.versionDependantCall(new VersionedCallable<>(SpigotTPS_8::getTps, 8),
new VersionedCallable<>(SpigotTPS_9::getTps, 9),
new VersionedCallable<>(SpigotTPS_10::getTps, 10),
new VersionedCallable<>(SpigotTPS_12::getTps, 12),
new VersionedCallable<>(SpigotTPS_14::getTps, 14),
new VersionedCallable<>(SpigotTPS_15::getTps, 15));
}
private static double round(double d) {

Datei anzeigen

@ -21,25 +21,29 @@
package de.steamwar.core;
import java.util.HashSet;
import java.util.Set;
public class VersionedCallable<T> {
private ExceptionlessCallable<T> exceptionlessCallable;
private Set<Integer> versions = new HashSet<>();
private int minVersion;
public VersionedCallable(ExceptionlessCallable<T> exceptionlessCallable, int... versions) {
public VersionedCallable(ExceptionlessCallable<T> exceptionlessCallable, int minVersion) {
this.exceptionlessCallable = exceptionlessCallable;
for (int version : versions) this.versions.add(version);
}
boolean isVersion() {
return versions.contains(Core.getVersion());
this.minVersion = minVersion;
}
public T call() {
return exceptionlessCallable.call();
}
public static <T> T versionDependantCall(VersionedCallable<T>... versionedCallables) {
int version = Core.getVersion();
for (int i = versionedCallables.length - 1; i >= 0; i--) {
VersionedCallable<T> versionedCallable = versionedCallables[i];
if (version >= versionedCallable.minVersion || i == 0) {
return versionedCallable.call();
}
}
return null;
}
}

Datei anzeigen

@ -21,25 +21,29 @@
package de.steamwar.core;
import java.util.HashSet;
import java.util.Set;
public class VersionedRunnable {
private Runnable runnable;
private Set<Integer> versions = new HashSet<>();
private int minVersion;
public VersionedRunnable(Runnable runnable, int... versions) {
public VersionedRunnable(Runnable runnable, int minVersion) {
this.runnable = runnable;
for (int version : versions) this.versions.add(version);
}
boolean isVersion() {
return versions.contains(Core.getVersion());
this.minVersion = minVersion;
}
public void run() {
runnable.run();
}
public static void versionDependantCall(VersionedRunnable... versionedRunnables) {
int version = Core.getVersion();
for (int i = versionedRunnables.length - 1; i >= 0; i--) {
VersionedRunnable versionedRunnable = versionedRunnables[i];
if (version >= versionedRunnable.minVersion) {
versionedRunnable.run();
return;
}
}
}
}

Datei anzeigen

@ -28,6 +28,7 @@ import com.comphenix.protocol.injector.server.TemporaryPlayer;
import com.comphenix.protocol.reflect.StructureModifier;
import de.steamwar.chunk.*;
import de.steamwar.core.Core;
import de.steamwar.core.VersionedRunnable;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -71,11 +72,11 @@ public class ChunkListener {
}
public static void sendChunk(Player p, int chunkX, int chunkZ){
Core.versionDependantCall(() -> Chunk_8.sendChunk(p, chunkX, chunkZ),
() -> Chunk_9.sendChunk(p, chunkX, chunkZ),
() -> Chunk_10.sendChunk(p, chunkX, chunkZ),
() -> Chunk_12.sendChunk(p, chunkX, chunkZ),
() -> Chunk_14.sendChunk(p, chunkX, chunkZ),
() -> Chunk_15.sendChunk(p, chunkX, chunkZ));
VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> Chunk_8.sendChunk(p, chunkX, chunkZ), 8),
new VersionedRunnable(() -> Chunk_9.sendChunk(p, chunkX, chunkZ), 9),
new VersionedRunnable(() -> Chunk_10.sendChunk(p, chunkX, chunkZ), 10),
new VersionedRunnable(() -> Chunk_12.sendChunk(p, chunkX, chunkZ), 12),
new VersionedRunnable(() -> Chunk_14.sendChunk(p, chunkX, chunkZ), 14),
new VersionedRunnable(() -> Chunk_15.sendChunk(p, chunkX, chunkZ), 15));
}
}

Datei anzeigen

@ -45,24 +45,24 @@ public class SWItem {
public static SWItem getPlayerSkull(String playerName){
SWItem p = new SWItem();
ItemStack head = Core.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8, 9, 10, 12),
new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName)));
ItemStack head = VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8),
new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName), 14));
p.setItemStack(head);
return p;
}
public static Material getMaterial(String material){
try{
return Core.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8, 9, 10, 12),
new VersionedCallable<>(() -> SWItem_14.getMaterial(material)));
return VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8),
new VersionedCallable<>(() -> SWItem_14.getMaterial(material), 14));
}catch(IllegalArgumentException e){
return Material.STONE;
}
}
public static Material getDye(int colorCode){
return Core.versionDependantCall(new VersionedCallable<>(SWItem_8::getDye, 8, 9, 10, 12),
new VersionedCallable<>(() -> SWItem_14.getDye(colorCode)));
return VersionedCallable.versionDependantCall(new VersionedCallable<>(SWItem_8::getDye, 8),
new VersionedCallable<>(() -> SWItem_14.getDye(colorCode), 14));
}
public SWItem() {

Datei anzeigen

@ -74,8 +74,8 @@ public class Message {
}
private Locale getLocale(Player player){
return Core.versionDependantCall(new VersionedCallable<>(() -> Message_8.getLocale(player), 8, 9, 10),
new VersionedCallable<>(() -> Message_12.getLocale(player)));
return VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> Message_8.getLocale(player), 8),
new VersionedCallable<>(() -> Message_12.getLocale(player), 12));
}
/* Send a message to one player */

Datei anzeigen

@ -95,8 +95,8 @@ public class SWScoreboard {
PacketContainer packet = manager.createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE);
packet.getStrings().write(0, SIDEBAR + toggle);
packet.getIntegers().write(0, 0); //0 to create
Core.versionDependantCall(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8, 9, 10, 12),
new VersionedRunnable(() -> packet.getChatComponents().write(0, WrappedChatComponent.fromText(name))));
VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8),
new VersionedRunnable(() -> packet.getChatComponents().write(0, WrappedChatComponent.fromText(name)), 14));
packet.getEnumModifier(RenderType.class, 2).write(0, RenderType.INTEGER);
return packet;
}

Datei anzeigen

@ -21,7 +21,6 @@ package de.steamwar.sql;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.core.Core;
import de.steamwar.core.VersionedCallable;
import org.bukkit.entity.Player;
import java.io.IOException;