siege weapon finalizations & analytics (wip)

This commit is contained in:
Ben 2016-03-15 22:52:13 +00:00
parent d7de4381a3
commit 4638ba909d
15 changed files with 397 additions and 36 deletions

View File

@ -0,0 +1,27 @@
package mineplex.core.common.util;
import java.util.HashMap;
import java.util.Map;
public class MapBuilder<K, V>
{
private Map<K, V> _map;
public MapBuilder()
{
_map = new HashMap<>();
}
public MapBuilder<K, V> Put(K key, V value)
{
_map.put(key, value);
return this;
}
public Map<K, V> GetMap()
{
return _map;
}
}

View File

@ -254,4 +254,25 @@ public class UtilCollections
return toList(elements);
}
public static <X> String combine(X[] data, String delimiter)
{
StringBuilder total = new StringBuilder();
int loops = 0;
for (X x : data)
{
if (delimiter != null && loops != 0)
{
total.append(delimiter);
}
total.append(x.toString());
loops++;
}
return total.toString();
}
}

View File

@ -50,5 +50,4 @@ public class UtilFile
return bytes;
}
}

View File

@ -0,0 +1,58 @@
package mineplex.core.common.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
public class UtilWeb
{
public static String doPOST(String url, Map<String, Object> params)
{
try
{
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet())
{
if (postData.length() != 0)
{
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder back = new StringBuilder();
for (int $char; ($char = in.read()) >= 0;)
{
back.append((char) $char);
}
return back.toString();
}
catch (Exception exception)
{
exception.printStackTrace();
return null;
}
}
}

View File

@ -41,6 +41,7 @@ import mineplex.core.teleport.Teleport;
import mineplex.core.updater.FileUpdater;
import mineplex.core.updater.Updater;
import mineplex.core.visibility.VisibilityManager;
import mineplex.game.clans.analytics.Profiler;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.ban.ClansBanManager;
import mineplex.game.clans.items.GearManager;
@ -65,6 +66,9 @@ public class Clans extends JavaPlugin
@Override
public void onEnable()
{
// Initialize Profiler
new Profiler();
// Configs
getConfig().addDefault(WEB_CONFIG, "http://accounts.mineplex.com/");
getConfig().set(WEB_CONFIG, getConfig().getString(WEB_CONFIG));

View File

@ -0,0 +1,37 @@
package mineplex.game.clans.analytics;
public class Analytic
{
private long _start;
private long _elapsed;
private String _section;
private String[] _data;
public Analytic(String section, String[] data, long start, long elapsed)
{
_data = data;
_elapsed = elapsed;
_start = start;
_section = section;
}
public String[] getData()
{
return _data;
}
public long getStart()
{
return _start;
}
public long getElapsed()
{
return _elapsed;
}
public String getSection()
{
return _section;
}
}

View File

@ -0,0 +1,100 @@
package mineplex.game.clans.analytics;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import mineplex.core.common.util.MapBuilder;
import mineplex.core.common.util.UtilCollections;
import mineplex.core.common.util.UtilTime;
import mineplex.core.common.util.UtilWeb;
public class AnalyticQueuer extends Thread
{
private List<Analytic> Queue = new ArrayList<>();
private ByteArrayOutputStream _bytes;
private long _lastPush = System.currentTimeMillis();
public void Queue(Section section, String[] data)
{
Queue.add(new Analytic(section.getSection(), data, section.getStart(), section.getElapsed()));
}
private void write(Analytic analytic)
{
try (
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(stream);
)
{
out.writeLong(analytic.getStart());
out.writeLong(analytic.getElapsed());
out.writeInt(analytic.getSection().getBytes().length);
out.writeBytes(analytic.getSection());
String data = UtilCollections.combine(analytic.getData(), ",");
out.writeInt(data.length());
out.writeBytes(data);
_bytes.write(stream.toByteArray());
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
public void run()
{
while (true)
{
Iterator<Analytic> iterator = Queue.iterator();
while (iterator.hasNext())
{
Analytic analytic = iterator.next();
write(analytic);
iterator.remove();
}
if (UtilTime.elapsed(_lastPush, 10000))
{
String response = UtilWeb.doPOST("http://analytics.garblox.com/PushAnalyticData",
new MapBuilder<String, Object>()
.Put("authkey", Profiler.ANALYTICS_KEY)
.Put("service", "ClansBeta")
.Put("data", _bytes.toString())
.GetMap()
);
_bytes.reset();
if (!response.equals("OK"))
{
System.out.println("[Clans Beta] Error while pushing analytic data to garblox: " + response);
}
}
try
{
Thread.sleep(250);
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,69 @@
package mineplex.game.clans.analytics;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;
import mineplex.core.common.util.MapBuilder;
import mineplex.core.common.util.UtilFile;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilWeb;
public class Profiler
{
public static final String ANALYTICS_KEY;
public static Profiler Instance;
private Map<Thread, Stack<Section>> _sections = new HashMap<>();
private AnalyticQueuer _queuer;
static
{
ANALYTICS_KEY = UtilFile.read(new File(UtilServer.getServer().getWorldContainer() + "/GarbloxAnalyticsKey"));
}
public Profiler()
{
_queuer = new AnalyticQueuer();
_queuer.start();
String response = UtilWeb.doPOST("http://analytics.garblox.com/RegisterAnalytics",
new MapBuilder<String, Object>()
.Put("authkey", Profiler.ANALYTICS_KEY)
.Put("name", "ClansBeta")
.GetMap()
);
if (!response.equals("OK"))
{
System.out.println("[Clans Beta Analytics] Error while registering new analytic type: " + response);
}
Instance = this;
}
public void start(String section)
{
Thread thread = Thread.currentThread();
if (_sections.get(thread) == null)
{
_sections.put(thread, new Stack<>());
}
_sections.get(thread).push(new Section(section, System.currentTimeMillis(), thread));
}
public void end(String... data)
{
Thread thread = Thread.currentThread();
Section section = _sections.get(thread).pop();
_queuer.Queue(section, data);
}
}

View File

@ -0,0 +1,36 @@
package mineplex.game.clans.analytics;
public class Section
{
private String _section;
private long _start;
private Thread _thread;
public Section(String section, long start, Thread thread)
{
_section = section;
_start = start;
_thread = thread;
}
public String getSection()
{
return _section;
}
public long getStart()
{
return _start;
}
public long getElapsed()
{
return System.currentTimeMillis() - _start;
}
public Thread getThread()
{
return _thread;
}
}

View File

@ -1,15 +0,0 @@
package mineplex.game.clans.clans;
import java.io.File;
import java.io.IOException;
import mineplex.core.common.util.UtilOfflinePlayer;
public class Test
{
public static void main(String[] args) throws IOException
{
System.out.println("wtf");
UtilOfflinePlayer.loadOfflineInventory(new File("M:/Clans/a797d3ef-dd25-4e18-a002-6537b19b3603.dat"));
}
}

View File

@ -22,6 +22,7 @@ import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.analytics.Profiler;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.clans.siege.commands.CommandSiegeSupplies;
import mineplex.game.clans.clans.siege.outpost.OutpostManager;
@ -132,6 +133,7 @@ public class SiegeManager extends MiniPlugin
_outpostManager.saveOutposts();
saveSiegeWeapons();
Profiler.Instance.start("SiegeManager.CleanupEntities");
for (Entity entity : Spawn.getSpawnWorld().getEntitiesByClass(ArmorStand.class))
{
boolean part = false;
@ -160,6 +162,7 @@ public class SiegeManager extends MiniPlugin
}
}
}
Profiler.Instance.end();
}
}
@ -176,24 +179,27 @@ public class SiegeManager extends MiniPlugin
});
}
runAsync(() -> {
Profiler.Instance.start("SiegeManager.CleanupEntities");
while (!queue.isEmpty())
{
queue.pop().run();
}
});
runAsync(() ->
_repository.getWeaponsByServer(_clansManager.getServerId(), tokens ->
_repository.getWeaponsByServer(_clansManager.getServerId(), tokens -> {
tokens.forEach(token -> {
if (!LiveSiegeWeapons.containsKey(Integer.valueOf(token.UniqueId)))
{
System.out.println("LiveSiegeWeapons no longer contains old weapon " + token.UniqueId + ", deleting.");
_repository.deleteWeapon(token.UniqueId);
}
})
)
);
});
});
Profiler.Instance.end();
});
}
@EventHandler

View File

@ -27,6 +27,7 @@ import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.game.clans.analytics.Profiler;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.ClansBlacklist;
import mineplex.game.clans.clans.ClansManager;
@ -294,21 +295,23 @@ public class OutpostManager extends MiniPlugin
});
runAsync(() -> {
Profiler.Instance.start("OutpostManager.UpdateOutposts");
while (!queue.isEmpty())
queue.pop().run();
});
runAsync(() ->
_repository.getOutpostsByServer(_clansManager.getServerId(), tokens ->
_repository.getOutpostsByServer(_clansManager.getServerId(), tokens -> {
tokens.forEach(token -> {
if (!_idToOutpost.containsKey(Integer.valueOf(token.UniqueId)))
{
System.out.println("[OUTPOSTS] OUTPOST [" + token.UniqueId + "] NO LONGER EXISTS, DELETING");
_repository.deleteOutpost(token.UniqueId);
}
})
)
);
});
});
Profiler.Instance.end();
});
}
public OutpostRepository getRepository()

View File

@ -12,6 +12,7 @@ import com.google.common.collect.Lists;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.database.MinecraftRepository;
import mineplex.game.clans.analytics.Profiler;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.siege.outpost.OutpostManager;
import mineplex.game.clans.clans.siege.outpost.OutpostState;

View File

@ -18,6 +18,7 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
@ -152,6 +153,16 @@ public abstract class SiegeWeapon implements Listener
_infoHologram = new Hologram(ClansManager.getInstance().getHologramManager(), _location.clone().add(.5, 3, .5), _name + " Health", getDisplayHealth());
_infoHologram.start();
_infoHologram.setInteraction((player, type) -> {
if (player.equals(_rider))
{
if (type.equals(ClickType.RIGHT))
{
handleLeftClick(player);
}
}
});
UtilServer.RegisterEvents(this);
_clans = clansManager;
@ -478,7 +489,7 @@ public abstract class SiegeWeapon implements Listener
new Hologram(
_siegeManager.getClansManager().getHologramManager(),
_location.add(UtilMath.random(-1, 1),1.4, UtilMath.random(-1, 1)),
_location.clone().add(UtilMath.random(-1, 1),1.4, UtilMath.random(-1, 1)),
3500l,
C.cRed + "-" + health)
.start();

View File

@ -53,13 +53,9 @@ public class WeaponProjectile implements Listener
@EventHandler(priority = EventPriority.HIGHEST)
public void onTntExplode(EntityExplodeEvent event)
{
if (!_attributes._isPrimedTnt)
{
return;
}
if (event.getEntity().equals(_projectileEntity))
{
((TNTPrimed) event.getEntity()).setFuseTicks(60);
event.setCancelled(true);
}
}
@ -82,12 +78,20 @@ public class WeaponProjectile implements Listener
return;
}
if (_projectileEntity.isDead())
{
die();
return;
}
if (_dead || _projectileEntity == null)
{
die();
return;
}
((TNTPrimed) _projectileEntity).setFuseTicks(60);
if ((Math.abs(_projectileEntity.getVelocity().getX()) < 0.01
|| Math.abs(_projectileEntity.getVelocity().getZ()) < 0.01)
&& UtilBlock.getInRadius(_projectileEntity.getLocation(), 2)