Setup enderchest API
This commit is contained in:
parent
92ed095e92
commit
e892476ca0
@ -0,0 +1,5 @@
|
||||
package mineplex.core.common.api;
|
||||
|
||||
public class ApiException extends Exception
|
||||
{
|
||||
}
|
@ -1,6 +1,11 @@
|
||||
package mineplex.core.common.api;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@ -13,10 +18,18 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import mineplex.core.common.api.enderchest.HashesNotEqualException;
|
||||
|
||||
/**
|
||||
* @author Shaun Bennett
|
||||
@ -28,6 +41,11 @@ public class ApiWebCall
|
||||
private PoolingHttpClientConnectionManager _cm;
|
||||
private CloseableHttpClient _httpClient;
|
||||
|
||||
public ApiWebCall(String url)
|
||||
{
|
||||
this(url, new Gson());
|
||||
}
|
||||
|
||||
public ApiWebCall(String url, Gson gson)
|
||||
{
|
||||
_url = url;
|
||||
@ -89,6 +107,50 @@ public class ApiWebCall
|
||||
return returnData;
|
||||
}
|
||||
|
||||
public File getFile(String resource, String savePath) throws HashesNotEqualException, IOException
|
||||
{
|
||||
HttpGet httpGet = new HttpGet(_url + resource);
|
||||
File file = new File(savePath);
|
||||
|
||||
FileOutputStream fos = null;
|
||||
DigestInputStream dis = null;
|
||||
try (CloseableHttpResponse response = _httpClient.execute(httpGet))
|
||||
{
|
||||
MessageDigest md = DigestUtils.getMd5Digest();
|
||||
HttpEntity entity = response.getEntity();
|
||||
dis = new DigestInputStream(entity.getContent(), md);
|
||||
fos = new FileOutputStream(file);
|
||||
IOUtils.copy(dis, fos);
|
||||
|
||||
String calculatedHash = Hex.encodeHexString(md.digest());
|
||||
Header hashHeader = response.getFirstHeader("Content-MD5");
|
||||
|
||||
if (hashHeader != null && !calculatedHash.equals(hashHeader.getValue()))
|
||||
{
|
||||
file.delete();
|
||||
throw new HashesNotEqualException(hashHeader.getValue(), calculatedHash);
|
||||
}
|
||||
} finally {
|
||||
try
|
||||
{
|
||||
if (fos != null) fos.close();
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (dis != null) dis.close();
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
private <T> T parseResponse(CloseableHttpResponse response, Type type) throws IOException
|
||||
{
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
@ -0,0 +1,44 @@
|
||||
package mineplex.core.common.api.enderchest;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import mineplex.core.common.api.ApiWebCall;
|
||||
import mineplex.core.common.util.ZipUtil;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
|
||||
/**
|
||||
* Load worlds from the `enderchest` microservice
|
||||
*/
|
||||
public class EnderchestWorldLoader
|
||||
{
|
||||
private static final String TIMINGS_PREFIX = "Enderchest LoadMap::";
|
||||
private ApiWebCall _webCall;
|
||||
|
||||
public EnderchestWorldLoader()
|
||||
{
|
||||
_webCall = new ApiWebCall("http://localhost:3000/");
|
||||
}
|
||||
|
||||
public void loadMap(String mapType, String folder) throws HashesNotEqualException, IOException
|
||||
{
|
||||
TimingManager.start(TIMINGS_PREFIX + "DownloadMap");
|
||||
String fileName = mapType + "_map.zip";
|
||||
File f = _webCall.getFile(mapType, fileName);
|
||||
TimingManager.stop(TIMINGS_PREFIX + "DownloadMap");
|
||||
|
||||
TimingManager.start(TIMINGS_PREFIX + "CreateFolders");
|
||||
new File(folder).mkdir();
|
||||
new File(folder + java.io.File.separator + "region").mkdir();
|
||||
new File(folder + java.io.File.separator + "data").mkdir();
|
||||
TimingManager.stop(TIMINGS_PREFIX + "CreateFolders");
|
||||
|
||||
TimingManager.start(TIMINGS_PREFIX + "UnzipToDirectory");
|
||||
ZipUtil.UnzipToDirectory(f.getAbsolutePath(), folder);
|
||||
TimingManager.stop(TIMINGS_PREFIX + "UnzipToDirectory");
|
||||
|
||||
TimingManager.start(TIMINGS_PREFIX + "DeleteZip");
|
||||
f.delete();
|
||||
TimingManager.stop(TIMINGS_PREFIX + "DeleteZip");
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package mineplex.core.common.api.enderchest;
|
||||
|
||||
import mineplex.core.common.api.ApiException;
|
||||
|
||||
public class HashesNotEqualException extends ApiException
|
||||
{
|
||||
private String _hashFromServer;
|
||||
private String _calculatedHash;
|
||||
|
||||
public HashesNotEqualException(String hashFromServer, String calculatedHash)
|
||||
{
|
||||
_hashFromServer = hashFromServer;
|
||||
_calculatedHash = calculatedHash;
|
||||
}
|
||||
|
||||
public String getHashFromServer()
|
||||
{
|
||||
return _hashFromServer;
|
||||
}
|
||||
|
||||
public String getGeneratedHash()
|
||||
{
|
||||
return _calculatedHash;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package mineplex.core.timing;
|
||||
package mineplex.core.common.timing;
|
||||
|
||||
public class TimeData
|
||||
{
|
@ -1,17 +1,11 @@
|
||||
package mineplex.core.timing;
|
||||
package mineplex.core.common.timing;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
|
||||
public class TimingManager implements Listener
|
||||
public class TimingManager
|
||||
{
|
||||
private static TimingManager _instance;
|
||||
|
||||
private JavaPlugin _plugin;
|
||||
private static NautHashMap<String, Long> _timingList = new NautHashMap<String, Long>();
|
||||
private static NautHashMap<String, TimeData> _totalList = new NautHashMap<String, TimeData>();
|
||||
|
||||
@ -20,30 +14,6 @@ public class TimingManager implements Listener
|
||||
|
||||
public static boolean Debug = true;
|
||||
|
||||
protected TimingManager(JavaPlugin plugin)
|
||||
{
|
||||
_instance = this;
|
||||
|
||||
_plugin = plugin;
|
||||
|
||||
_plugin.getServer().getPluginManager().registerEvents(this, _plugin);
|
||||
}
|
||||
|
||||
public static TimingManager Initialize(JavaPlugin plugin)
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new TimingManager(plugin);
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
|
||||
public static TimingManager instance()
|
||||
{
|
||||
return _instance;
|
||||
}
|
||||
|
||||
public static void startTotal(String title)
|
||||
{
|
||||
if (!Debug)
|
@ -30,7 +30,7 @@ import mineplex.core.common.util.Callback;
|
||||
import mineplex.core.common.util.UUIDFetcher;
|
||||
import mineplex.core.common.util.UtilPlayer;
|
||||
import mineplex.core.common.util.UtilTasks;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
|
||||
|
@ -4,7 +4,7 @@ import java.util.Iterator;
|
||||
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
import mineplex.core.recharge.Recharge;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
|
||||
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -5,7 +5,7 @@ import java.util.Iterator;
|
||||
import mineplex.core.MiniPlugin;
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
import mineplex.core.common.util.UtilServer;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
|
||||
|
@ -98,7 +98,7 @@ import mineplex.core.status.ServerStatusManager;
|
||||
import mineplex.core.task.TaskManager;
|
||||
import mineplex.core.teleport.Teleport;
|
||||
import mineplex.core.thank.ThankManager;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import mineplex.core.rankGiveaway.titangiveaway.TitanGiveawayManager;
|
||||
import mineplex.core.valentines.ValentinesGiftManager;
|
||||
import mineplex.core.youtube.YoutubeManager;
|
||||
|
@ -273,7 +273,7 @@ public abstract class UHC extends Game
|
||||
new OreVeinEditorModule().removeNonAirVeins().register(this);
|
||||
new AntiExpOrbModule().register(this);
|
||||
new RejoinModule().register(this);
|
||||
|
||||
|
||||
registerDebugCommand(new DebugCommand("startpvp", Rank.ADMIN)
|
||||
{
|
||||
@Override
|
||||
|
@ -3,7 +3,7 @@ package nautilus.game.arcade.game.modules;
|
||||
import mineplex.core.common.util.UtilBlock;
|
||||
import mineplex.core.common.util.UtilMath;
|
||||
import mineplex.core.common.util.UtilWorld;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.EventHandler;
|
||||
|
@ -4,7 +4,7 @@ import mineplex.core.common.util.UtilMath;
|
||||
import mineplex.core.common.util.UtilServer;
|
||||
import mineplex.core.common.util.UtilTime;
|
||||
import mineplex.core.itemstack.ItemStackFactory;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
import mineplex.minecraft.game.core.combat.CombatManager.AttackReason;
|
||||
|
@ -5,7 +5,7 @@ import com.google.common.collect.Maps;
|
||||
import mineplex.core.common.util.MapUtil;
|
||||
import mineplex.core.common.util.UtilAlg;
|
||||
import mineplex.core.common.util.UtilEnt;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import nautilus.game.arcade.ArcadeManager;
|
||||
import nautilus.game.arcade.game.Game;
|
||||
import nautilus.game.arcade.game.GameTeam;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package nautilus.game.arcade.world;
|
||||
|
||||
import com.mineplex.spigot.ChunkPreLoadEvent;
|
||||
|
||||
import mineplex.core.common.util.FileUtil;
|
||||
import mineplex.core.common.util.MapUtil;
|
||||
import mineplex.core.common.util.UtilMath;
|
||||
@ -8,14 +9,14 @@ import mineplex.core.common.util.UtilServer;
|
||||
import mineplex.core.common.util.WorldUtil;
|
||||
import mineplex.core.common.util.ZipUtil;
|
||||
import mineplex.core.common.util.worldgen.WorldGenCleanRoom;
|
||||
import mineplex.core.timing.TimingManager;
|
||||
import mineplex.core.common.api.enderchest.EnderchestWorldLoader;
|
||||
import mineplex.core.common.timing.TimingManager;
|
||||
import nautilus.game.arcade.GameType;
|
||||
import nautilus.game.arcade.game.Game;
|
||||
import nautilus.game.arcade.game.games.uhc.UHC;
|
||||
import org.bukkit.Difficulty;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.World.Environment;
|
||||
import org.bukkit.WorldCreator;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@ -77,7 +78,17 @@ public class WorldData
|
||||
public void run()
|
||||
{
|
||||
//Unzip
|
||||
worldData.UnzipWorld();
|
||||
if (Host instanceof UHC) {
|
||||
boolean uhcLoaded = loadUHCMap(); // attempt to load from enderchest
|
||||
if (!uhcLoaded)
|
||||
{
|
||||
// failsafe on normal UHC map
|
||||
worldData.UnzipWorld();
|
||||
}
|
||||
} else
|
||||
{
|
||||
worldData.UnzipWorld();
|
||||
}
|
||||
|
||||
//Load World Data Sync
|
||||
UtilServer.getServer().getScheduler().runTask(Host.Manager.getPlugin(), new Runnable()
|
||||
@ -85,9 +96,12 @@ public class WorldData
|
||||
public void run()
|
||||
{
|
||||
TimingManager.start("WorldData loading world.");
|
||||
|
||||
WorldCreator creator = new WorldCreator(GetFolder());
|
||||
creator.generator(new WorldGenCleanRoom());
|
||||
World = WorldUtil.LoadWorld(creator);
|
||||
|
||||
|
||||
TimingManager.stop("WorldData loading world.");
|
||||
|
||||
World.setDifficulty(Difficulty.HARD);
|
||||
@ -112,6 +126,29 @@ public class WorldData
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean loadUHCMap()
|
||||
{
|
||||
EnderchestWorldLoader worldLoader = new EnderchestWorldLoader();
|
||||
boolean success = false;
|
||||
|
||||
for (int attempt = 1; !success && attempt <= 3; attempt++)
|
||||
{
|
||||
System.out.println("Grabbing UHC map from Enderchest, attempt " + attempt);
|
||||
|
||||
try
|
||||
{
|
||||
worldLoader.loadMap("uhc", GetFolder());
|
||||
success = true;
|
||||
} catch (Exception e)
|
||||
{
|
||||
attempt++;
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
protected GameType GetGame()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user