mirror of
https://github.com/darksaid98/AlathranWars.git
synced 2026-12-01 14:38:55 +00:00
Merge pull request #18 from ShermansWorld/feature/war-points
Feature/war-points
This commit is contained in:
5 files changed
+38
-181
No files matched your search
@@ -235,24 +235,7 @@ public class Siege {
|
||||
Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The attackers have won the siege of " + this.town.getName() + "!");
|
||||
try {
|
||||
nation = resident.getTown().getNation();
|
||||
} catch (NotRegisteredException e) {
|
||||
for (final String playerName : this.attackerPlayers) {
|
||||
try {
|
||||
Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "The town of "
|
||||
+ this.town.getName()
|
||||
+ " could not be occupied because the player who started the siege is not part of a nation");
|
||||
} catch (NullPointerException ex) {
|
||||
}
|
||||
}
|
||||
for (final String playerName : this.defenderPlayers) {
|
||||
try {
|
||||
Bukkit.getPlayer(playerName).sendMessage(String.valueOf(Helper.Chatlabel()) + "The town of "
|
||||
+ this.town.getName()
|
||||
+ " could not be occupied because the player who started the siege is not part of a nation");
|
||||
} catch (NullPointerException ex2) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
if (nation != null) {
|
||||
Bukkit.broadcastMessage(String.valueOf(Helper.Chatlabel()) + "The town of " + this.town.getName()
|
||||
+ " has been placed under occupation by " + nation.getName() + "!");
|
||||
@@ -280,6 +263,12 @@ public class Siege {
|
||||
+ ", valuing $" + String.valueOf(amt));
|
||||
Main.econ.depositPlayer(siegeLeader, amt);
|
||||
|
||||
if (side1AreAttackers) {
|
||||
war.addSide1Points(50);
|
||||
} else {
|
||||
war.addSide2Points(50);
|
||||
}
|
||||
|
||||
stop();
|
||||
clearBeacon();
|
||||
}
|
||||
@@ -291,6 +280,13 @@ public class Siege {
|
||||
+ " has recovered the attackers' war chest, valued at $2,500");
|
||||
Main.warLogger
|
||||
.log(war.getName() + ": The defenders have won the siege of " + this.town.getName() + "!");
|
||||
|
||||
if (side1AreAttackers) {
|
||||
war.addSide2Points(50);
|
||||
} else {
|
||||
war.addSide1Points(50);
|
||||
}
|
||||
|
||||
stop();
|
||||
clearBeacon();
|
||||
}
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
package me.ShermansWorld.AlathraWar;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
public class UUIDFetcher {
|
||||
|
||||
/**
|
||||
* Date when name changes were introduced
|
||||
* @see UUIDFetcher#getUUIDAt(String, long)
|
||||
*/
|
||||
public static final long FEBRUARY_2015 = 1422748800000L;
|
||||
|
||||
|
||||
private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
|
||||
|
||||
private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%d";
|
||||
private static final String NAME_URL = "https://api.mojang.com/user/profiles/%s/names";
|
||||
|
||||
private static Map<String, UUID> uuidCache = new HashMap<String, UUID>();
|
||||
private static Map<UUID, String> nameCache = new HashMap<UUID, String>();
|
||||
|
||||
private static ExecutorService pool = Executors.newCachedThreadPool();
|
||||
|
||||
private String name;
|
||||
private UUID id;
|
||||
|
||||
/**
|
||||
* Fetches the uuid asynchronously and passes it to the consumer
|
||||
*
|
||||
* @param name The name
|
||||
* @param action Do what you want to do with the uuid her
|
||||
*/
|
||||
public static void getUUID(String name, Consumer<UUID> action) {
|
||||
pool.execute(() -> action.accept(getUUID(name)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the uuid synchronously and returns it
|
||||
*
|
||||
* @param name The name
|
||||
* @return The uuid
|
||||
*/
|
||||
public static UUID getUUID(String name) {
|
||||
return getUUIDAt(name, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the uuid synchronously for a specified name and time and passes the result to the consumer
|
||||
*
|
||||
* @param name The name
|
||||
* @param timestamp Time when the player had this name in milliseconds
|
||||
* @param action Do what you want to do with the uuid her
|
||||
*/
|
||||
public static void getUUIDAt(String name, long timestamp, Consumer<UUID> action) {
|
||||
pool.execute(() -> action.accept(getUUIDAt(name, timestamp)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the uuid synchronously for a specified name and time
|
||||
*
|
||||
* @param name The name
|
||||
* @param timestamp Time when the player had this name in milliseconds
|
||||
* @see UUIDFetcher#FEBRUARY_2015
|
||||
*/
|
||||
public static UUID getUUIDAt(String name, long timestamp) {
|
||||
name = name.toLowerCase();
|
||||
if (uuidCache.containsKey(name)) {
|
||||
return uuidCache.get(name);
|
||||
}
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(String.format(UUID_URL, name, timestamp/1000)).openConnection();
|
||||
connection.setReadTimeout(5000);
|
||||
UUIDFetcher data = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher.class);
|
||||
|
||||
uuidCache.put(name, data.id);
|
||||
nameCache.put(data.id, data.name);
|
||||
|
||||
return data.id;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the name asynchronously and passes it to the consumer
|
||||
*
|
||||
* @param uuid The uuid
|
||||
* @param action Do what you want to do with the name her
|
||||
*/
|
||||
public static void getName(UUID uuid, Consumer<String> action) {
|
||||
pool.execute(() -> action.accept(getName(uuid)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the name synchronously and returns it
|
||||
*
|
||||
* @param uuid The uuid
|
||||
* @return The name
|
||||
*/
|
||||
public static String getName(UUID uuid) {
|
||||
if (nameCache.containsKey(uuid)) {
|
||||
return nameCache.get(uuid);
|
||||
}
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(String.format(NAME_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
|
||||
connection.setReadTimeout(5000);
|
||||
UUIDFetcher[] nameHistory = gson.fromJson(new BufferedReader(new InputStreamReader(connection.getInputStream())), UUIDFetcher[].class);
|
||||
UUIDFetcher currentNameData = nameHistory[nameHistory.length - 1];
|
||||
|
||||
uuidCache.put(currentNameData.name.toLowerCase(), uuid);
|
||||
nameCache.put(uuid, currentNameData.name);
|
||||
|
||||
return currentNameData.name;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class UUIDTypeAdapter extends TypeAdapter<UUID> {
|
||||
|
||||
public static String fromUUID(UUID value) {
|
||||
return value.toString().replace("-", "");
|
||||
}
|
||||
|
||||
public static UUID fromString(String input) {
|
||||
return UUID.fromString(input.replaceFirst(
|
||||
"(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, UUID value) throws IOException {
|
||||
// TODO Auto-generated method stub
|
||||
out.value(fromUUID(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID read(JsonReader in) throws IOException {
|
||||
// TODO Auto-generated method stub
|
||||
return fromString(in.nextString());
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@ public class War {
|
||||
private ArrayList<String> side1Towns = new ArrayList<String>();
|
||||
private ArrayList<String> side2Towns = new ArrayList<String>();
|
||||
private ArrayList<String> surrenderedTowns = new ArrayList<String>();
|
||||
private int side1Points = 0;
|
||||
private int side2Points = 0;
|
||||
|
||||
// References
|
||||
private ArrayList<Siege> sieges = new ArrayList<Siege>();
|
||||
@@ -268,4 +270,20 @@ public class War {
|
||||
public int getLastRaidTimeSide2() {
|
||||
return this.lastRaidTimeSide2;
|
||||
}
|
||||
|
||||
public int getSide1Points() {
|
||||
return this.side1Points;
|
||||
}
|
||||
|
||||
public int getSide2Points() {
|
||||
return this.side2Points;
|
||||
}
|
||||
|
||||
public void addSide1Points(int points) {
|
||||
side1Points += points;
|
||||
}
|
||||
|
||||
public void addSide2Points(int points){
|
||||
side2Points += points;
|
||||
}
|
||||
}
|
||||
@@ -276,7 +276,7 @@ public class WarCommands implements CommandExecutor {
|
||||
p.sendMessage("There are no current wars.");
|
||||
} else {
|
||||
for (War war : wars) {
|
||||
p.sendMessage(war.getName() + " - " + war.getSide1() + " vs. " + war.getSide2());
|
||||
p.sendMessage(war.getName() + " - " + war.getSide1() + "(" + war.getSide1Points() + ") vs. " + war.getSide2() + "(" + war.getSide2Points() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +107,9 @@ public class WarData
|
||||
war.setSide2Towns((ArrayList<String>) fileData.get("side2Towns"));
|
||||
war.setSurrenderedTowns((ArrayList<String>) fileData.get("surrenderedTowns"));
|
||||
|
||||
war.addSide1Points((int) fileData.get("side1Points"));
|
||||
war.addSide2Points((int) fileData.get("side2Points"));
|
||||
|
||||
if(fileData.get("lastRaidTimeSide1") != null && fileData.get("lastRaidTimeSide2") != null) {
|
||||
war.setLastRaidTimeSide1((int) fileData.get("lastRaidTimeSide1"));
|
||||
war.setLastRaidTimeSide2((int) fileData.get("lastRaidTimeSide2"));
|
||||
@@ -144,6 +147,8 @@ public class WarData
|
||||
sHashMap.put("side1Towns", (List<String>) war.getSide1Towns());
|
||||
sHashMap.put("side2Towns", (List<String>) war.getSide2Towns());
|
||||
sHashMap.put("surrenderedTowns", (List<String>) war.getSurrenderedTowns());
|
||||
sHashMap.put("side1Points", war.getSide1Points());
|
||||
sHashMap.put("side2Points", war.getSide2Points());
|
||||
|
||||
sHashMap.put("sieges", SiegeData.getSiegeMap(war));
|
||||
sHashMap.put("raids", RaidData.getRaidMap(war));
|
||||
|
||||
Reference in new issue
Block a user