SteamWar/SpigotCore
Archiviert
13
0

Merge pull request 'Add versionDependantCall, See #144 BauSystem' (#80) from VersionDependantCalls into master

Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Lixfel 2020-12-25 23:10:53 +01:00
Commit 1b4507c7e2
11 geänderte Dateien mit 178 neuen und 158 gelöschten Zeilen

Datei anzeigen

@ -64,17 +64,17 @@ class Schematic_14 {
private static final ClipboardFormat SCHEMATIC = BuiltInClipboardFormat.MCEDIT_SCHEMATIC; private static final ClipboardFormat SCHEMATIC = BuiltInClipboardFormat.MCEDIT_SCHEMATIC;
private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC; private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC;
static byte[] getPlayerClipboard(Player player, boolean schemFormat) throws IOException, NoClipboardException { static byte[] getPlayerClipboard(Player player, boolean schemFormat) {
ClipboardHolder clipboardHolder; ClipboardHolder clipboardHolder;
try { try {
clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard(); clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard();
} catch (EmptyClipboardException e) { } catch (EmptyClipboardException e) {
throw new NoClipboardException(); throw new RuntimeException(e.getMessage(), new NoClipboardException());
} }
Clipboard clipboard = clipboardHolder.getClipboard(); Clipboard clipboard = clipboardHolder.getClipboard();
if(clipboard == null) if(clipboard == null)
throw new NoClipboardException(); throw new RuntimeException("Clipboard was null", new NoClipboardException());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try{ try{
@ -86,16 +86,23 @@ class Schematic_14 {
SCHEMATIC.getWriter(outputStream).write(clipboard); SCHEMATIC.getWriter(outputStream).write(clipboard);
} }
}catch(NullPointerException e){ }catch(NullPointerException e){
throw new IOException(e); throw new RuntimeException(e.getMessage(), new IOException(e));
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} }
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) throws IOException, NoClipboardException { static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) {
Clipboard clipboard = getClipboard(is, schemFormat); Clipboard clipboard = null;
try {
clipboard = getClipboard(is, schemFormat);
} catch (IOException | NoClipboardException e) {
throw new RuntimeException(e.getMessage(), e);
}
if (clipboard == null) if (clipboard == null)
throw new NoClipboardException(); throw new RuntimeException("clipboard was null", new NoClipboardException());
Actor actor = getWorldEditPlugin().wrapCommandSender(player); Actor actor = getWorldEditPlugin().wrapCommandSender(player);
getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard)); getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard));

Datei anzeigen

@ -52,26 +52,35 @@ import java.util.zip.GZIPInputStream;
class Schematic_8 { class Schematic_8 {
private Schematic_8(){} private Schematic_8(){}
static byte[] getPlayerClipboard(Player player) throws IOException, NoClipboardException { static byte[] getPlayerClipboard(Player player) {
ClipboardHolder clipboardHolder; ClipboardHolder clipboardHolder;
try { try {
clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard(); clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard();
} catch (EmptyClipboardException e) { } catch (EmptyClipboardException e) {
throw new NoClipboardException(); throw new RuntimeException(e.getMessage(), e);
} }
Clipboard clipboard = clipboardHolder.getClipboard(); Clipboard clipboard = clipboardHolder.getClipboard();
if(clipboard == null) if(clipboard == null)
throw new NoClipboardException(); throw new RuntimeException("clipboard was null", new NoClipboardException());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); try {
ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData());
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) throws IOException { static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) {
WorldData world = new BukkitWorld(player.getWorld()).getWorldData(); WorldData world = new BukkitWorld(player.getWorld()).getWorldData();
Clipboard clipboard = getClipboard(is, schemFormat); Clipboard clipboard;
try {
clipboard = getClipboard(is, schemFormat);
} catch (IOException e) {
throw new RuntimeException(e);
}
Actor actor = getWorldEditPlugin().wrapCommandSender(player); Actor actor = getWorldEditPlugin().wrapCommandSender(player);
getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard, world)); getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard, world));

Datei anzeigen

@ -23,7 +23,7 @@ import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import de.steamwar.comms.handlers.BungeeHandler; import de.steamwar.comms.handlers.BungeeHandler;
import de.steamwar.comms.handlers.InventoryHandler; import de.steamwar.comms.handlers.InventoryHandler;
import de.steamwar.core.Core; import de.steamwar.core.VersionedRunnable;
import de.steamwar.sql.*; import de.steamwar.sql.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -46,11 +46,8 @@ public class BungeeReceiver implements PluginMessageListener {
UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID();
if(Bukkit.getPlayer(uuid).isOnline()) { if(Bukkit.getPlayer(uuid).isOnline()) {
Player player = Bukkit.getPlayer(uuid); Player player = Bukkit.getPlayer(uuid);
if (Core.getVersion() == 8) { VersionedRunnable.call(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8),
BungeeReceiver_8.playPling(player); new VersionedRunnable(() -> BungeeReceiver_9.playpling(player), 9));
} else {
BungeeReceiver_9.playpling(player);
}
} }
}); });

Datei anzeigen

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

Datei anzeigen

@ -0,0 +1,50 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* /
*/
package de.steamwar.core;
import java.util.concurrent.Callable;
public class VersionedCallable<T> {
private Callable<T> callable;
private int minVersion;
public VersionedCallable(Callable<T> callable, int minVersion) {
this.callable = callable;
this.minVersion = minVersion;
}
public static <T> T call(VersionedCallable<T>... versionedCallables) {
for (int i = versionedCallables.length - 1; i >= 0; i--) {
VersionedCallable<T> versionedCallable = versionedCallables[i];
if (Core.getVersion() >= versionedCallable.minVersion) {
try {
return versionedCallable.callable.call();
} catch (Exception e) {
throw new RuntimeException("Could not run version dependent code", e);
}
}
}
throw new SecurityException();
}
}

Datei anzeigen

@ -0,0 +1,44 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* /
*/
package de.steamwar.core;
public class VersionedRunnable {
private Runnable runnable;
private int minVersion;
public VersionedRunnable(Runnable runnable, int minVersion) {
this.runnable = runnable;
this.minVersion = minVersion;
}
public static void call(VersionedRunnable... versionedRunnables) {
for (int i = versionedRunnables.length - 1; i >= 0; i--) {
VersionedRunnable versionedRunnable = versionedRunnables[i];
if (Core.getVersion() >= versionedRunnable.minVersion) {
versionedRunnable.runnable.run();
return;
}
}
}
}

Datei anzeigen

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

Datei anzeigen

@ -21,7 +21,7 @@ package de.steamwar.inventory;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@ -44,53 +44,24 @@ public class SWItem {
public static SWItem getPlayerSkull(String playerName){ public static SWItem getPlayerSkull(String playerName){
SWItem p = new SWItem(); SWItem p = new SWItem();
ItemStack head; ItemStack head = VersionedCallable.call(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8),
switch(Core.getVersion()){ new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName), 14));
case 8:
case 9:
case 10:
case 12:
head = SWItem_8.setSkullOwner(playerName);
break;
case 14:
case 15:
default:
head = SWItem_14.setSkullOwner(playerName);
}
p.setItemStack(head); p.setItemStack(head);
return p; return p;
} }
public static Material getMaterial(String material){ public static Material getMaterial(String material){
try{ try{
switch(Core.getVersion()){ return VersionedCallable.call(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8),
case 8: new VersionedCallable<>(() -> SWItem_14.getMaterial(material), 14));
case 9:
case 10:
case 12:
return SWItem_8.getMaterial(material);
case 14:
case 15:
default:
return SWItem_14.getMaterial(material);
}
}catch(IllegalArgumentException e){ }catch(IllegalArgumentException e){
return Material.STONE; return Material.STONE;
} }
} }
public static Material getDye(int colorCode){ public static Material getDye(int colorCode){
switch(Core.getVersion()){ return VersionedCallable.call(new VersionedCallable<>(SWItem_8::getDye, 8),
case 8: new VersionedCallable<>(() -> SWItem_14.getDye(colorCode), 14));
case 9:
case 10:
case 12:
return SWItem_8.getDye();
case 14:
case 15:
default:
return SWItem_14.getDye(colorCode);
}
} }
public SWItem() { public SWItem() {

Datei anzeigen

@ -19,7 +19,7 @@
package de.steamwar.message; package de.steamwar.message;
import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.HoverEvent;
@ -73,17 +73,8 @@ public class Message {
} }
private Locale getLocale(Player player){ private Locale getLocale(Player player){
switch(Core.getVersion()){ return VersionedCallable.call(new VersionedCallable<>(() -> Message_8.getLocale(player), 8),
case 8: new VersionedCallable<>(() -> Message_12.getLocale(player), 12));
case 9:
case 10:
return Message_8.getLocale(player);
case 12:
case 14:
case 15:
default:
return Message_12.getLocale(player);
}
} }
/* Send a message to one player */ /* Send a message to one player */

Datei anzeigen

@ -25,6 +25,7 @@ import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedChatComponent;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import de.steamwar.core.VersionedRunnable;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -94,18 +95,8 @@ public class SWScoreboard {
PacketContainer packet = manager.createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE); PacketContainer packet = manager.createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE);
packet.getStrings().write(0, SIDEBAR + toggle); packet.getStrings().write(0, SIDEBAR + toggle);
packet.getIntegers().write(0, 0); //0 to create packet.getIntegers().write(0, 0); //0 to create
switch(Core.getVersion()){ VersionedRunnable.call(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8),
case 8: new VersionedRunnable(() -> packet.getChatComponents().write(0, WrappedChatComponent.fromText(name)), 14));
case 9:
case 10:
case 12:
packet.getStrings().write(1, name);
break;
case 14:
case 15:
default:
packet.getChatComponents().write(0, WrappedChatComponent.fromText(name));
}
packet.getEnumModifier(RenderType.class, 2).write(0, RenderType.INTEGER); packet.getEnumModifier(RenderType.class, 2).write(0, RenderType.INTEGER);
return packet; return packet;
} }

Datei anzeigen

@ -20,7 +20,8 @@
package de.steamwar.sql; package de.steamwar.sql;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable;
import de.steamwar.core.VersionedRunnable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.IOException; import java.io.IOException;
@ -189,24 +190,14 @@ public class Schematic {
if(schemData == null) if(schemData == null)
throw new IOException("SchemData is null"); throw new IOException("SchemData is null");
InputStream is = schemData.getBinaryStream(); InputStream is = schemData.getBinaryStream();
switch(Core.getVersion()){ return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8),
case 8: new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14));
case 9:
case 10:
case 12:
return Schematic_8.getClipboard(is, schemFormat);
case 14:
case 15:
default:
return Schematic_14.getClipboard(is, schemFormat);
}
} catch (SQLException e) { } catch (SQLException e) {
throw new IOException(e); throw new IOException(e);
} }
} }
public void loadToPlayer(Player player) throws IOException, NoClipboardException { public void loadToPlayer(Player player) throws IOException, NoClipboardException {
ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID);
try { try {
rs.next(); rs.next();
@ -214,18 +205,8 @@ public class Schematic {
if(blob == null) if(blob == null)
throw new NoClipboardException(); throw new NoClipboardException();
InputStream is = blob.getBinaryStream(); InputStream is = blob.getBinaryStream();
switch(Core.getVersion()){ VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8),
case 8: new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14));
case 9:
case 10:
case 12:
Schematic_8.setPlayerClipboard(player, is, schemFormat);
break;
case 14:
case 15:
default:
Schematic_14.setPlayerClipboard(player, is, schemFormat);
}
} catch (SQLException e) { } catch (SQLException e) {
throw new IOException(e); throw new IOException(e);
} }
@ -240,26 +221,27 @@ public class Schematic {
} }
private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException {
try{ Blob blob = SQL.blob();
Blob blob = SQL.blob(); VersionedRunnable.call(new VersionedRunnable(() -> {
switch(Core.getVersion()){ try {
case 8: blob.setBytes(1, Schematic_8.getPlayerClipboard(player));
case 9: } catch (SQLException e) {
case 10: throw new RuntimeException(e.getMessage(), e);
case 12:
newFormat = false;
blob.setBytes(1, Schematic_8.getPlayerClipboard(player));
break;
case 14:
case 15:
default:
blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat));
} }
SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID); updateDatabase(blob, player, false);
schemFormat = newFormat; }, 8), new VersionedRunnable(() -> {
}catch(SQLException e){ try {
throw new IOException(e); blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat));
} } catch (SQLException exception) {
throw new RuntimeException(exception.getMessage(), exception);
}
updateDatabase(blob, player, newFormat);
}, 14));
}
private void updateDatabase(Blob blob, Player player, boolean newFormat) {
SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID);
schemFormat = newFormat;
} }
public void remove(){ public void remove(){