Skip to content

Commit

Permalink
Update for Movecraft-Repair rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerS1066 committed Dec 18, 2022
1 parent f78a8ee commit 30ee7e8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package net.countercraft.movecraft.warfare;

import net.countercraft.movecraft.Movecraft;
import net.countercraft.movecraft.repair.MovecraftRepair;
import net.countercraft.movecraft.repair.util.WarfareUtils;
import net.countercraft.movecraft.warfare.commands.AssaultRepairCommand;
import net.countercraft.movecraft.warfare.listener.BlockListener;
import net.countercraft.movecraft.warfare.assault.AssaultManager;
import net.countercraft.movecraft.warfare.commands.AssaultCommand;
import net.countercraft.movecraft.warfare.commands.AssaultInfoCommand;
import net.countercraft.movecraft.warfare.commands.SiegeCommand;
import net.countercraft.movecraft.warfare.config.Config;
import net.countercraft.movecraft.warfare.listener.TypesReloadedListener;
import net.countercraft.movecraft.warfare.localisation.I18nSupport;
import net.countercraft.movecraft.warfare.siege.Siege;
import net.countercraft.movecraft.warfare.siege.SiegeManager;
import net.countercraft.movecraft.warfare.sign.RegionDamagedSign;
import net.countercraft.movecraft.warfare.utils.WarfareRepair;
import net.countercraft.movecraft.worldguard.MovecraftWorldGuard;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -26,7 +24,7 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
Expand All @@ -35,6 +33,7 @@ public final class MovecraftWarfare extends JavaPlugin {
private static MovecraftWarfare instance;
private AssaultManager assaultManager;
private SiegeManager siegeManager;
private WarfareUtils warfareUtils;

public static synchronized MovecraftWarfare getInstance() {
return instance;
Expand All @@ -47,28 +46,27 @@ public void onEnable() {
saveDefaultConfig();

// TODO other languages
String[] languages = {"en"};
String[] languages = { "en" };
for (String s : languages) {
if (!new File(getDataFolder() + "/localisation/mcwlang_"+ s +".properties").exists()) {
this.saveResource("localisation/mcwlang_"+ s +".properties", false);
if (!new File(getDataFolder() + "/localisation/mcwlang_" + s + ".properties").exists()) {
saveResource("localisation/mcwlang_" + s + ".properties", false);
}
}
Config.Locale = getConfig().getString("Locale", "en");
I18nSupport.init();


Config.AssaultEnable = getConfig().getBoolean("AssaultEnable", false);
Config.SiegeEnable = getConfig().getBoolean("SiegeEnable", false);

if(MovecraftWorldGuard.getInstance() == null || MovecraftRepair.getInstance().getEconomy() == null) {
if (MovecraftWorldGuard.getInstance() == null || MovecraftRepair.getInstance().getEconomy() == null) {
Config.AssaultEnable = false;
Config.SiegeEnable = false;
}
if(MovecraftRepair.getInstance() == null) {
if (MovecraftRepair.getInstance() == null) {
Config.AssaultEnable = false;
}

if(Config.AssaultEnable) {
if (Config.AssaultEnable) {
assaultManager = new AssaultManager(this);
assaultManager.runTaskTimerAsynchronously(this, 0, 20);

Expand All @@ -87,13 +85,12 @@ public void onEnable() {
Config.AssaultChunkSavePeriod = getConfig().getInt("AssaultChunkSavePeriod", 1);
Config.AssaultChunkRepairPerTick = getConfig().getInt("AssaultChunkRepairPerTick", 1);
Config.AssaultChunkRepairPeriod = getConfig().getInt("AssaultChunkSavePeriod", 1);
Config.AssaultDestroyableBlocks = new HashSet<>();
for(String s : getConfig().getStringList("AssaultDestroyableBlocks")) {
Config.AssaultDestroyableBlocks = EnumSet.noneOf(Material.class);
for (String s : getConfig().getStringList("AssaultDestroyableBlocks")) {
Material m = Material.getMaterial(s.toUpperCase());
if(m == null) {
if (m == null) {
getLogger().info("Failed to load AssaultDestroyableBlock: '" + s + "'");
}
else {
} else {
Config.AssaultDestroyableBlocks.add(m);
}
}
Expand All @@ -105,11 +102,11 @@ public void onEnable() {
new WarfareRepair(this);
}

if(Config.SiegeEnable) {
if (Config.SiegeEnable) {
Config.SiegeTaskSeconds = getConfig().getInt("SiegeTaskSeconds", 600);
siegeManager = new SiegeManager(this);
getLogger().info("Enabling siege");
//load the sieges.yml file
// load the sieges.yml file
File siegesFile = new File(MovecraftWarfare.getInstance().getDataFolder().getAbsolutePath() + "/sieges.yml");
InputStream input;
try {
Expand All @@ -118,11 +115,11 @@ public void onEnable() {
input = null;
}
if (input != null) {
Map data = new Yaml().loadAs(input, Map.class);
Map<?, ?> data = new Yaml().loadAs(input, Map.class);
Map<String, Map<String, ?>> siegesMap = (Map<String, Map<String, ?>>) data.get("sieges");
List<Siege> sieges = siegeManager.getSieges();
for (Map.Entry<String, Map<String, ?>> entry : siegesMap.entrySet()) {
Map<String,Object> siegeMap = (Map<String, Object>) entry.getValue();
Map<String, Object> siegeMap = (Map<String, Object>) entry.getValue();
sieges.add(new Siege(
entry.getKey(),
(String) siegeMap.get("RegionToControl"),
Expand All @@ -143,20 +140,16 @@ public void onEnable() {
getLogger().log(Level.INFO, "Siege configuration loaded.");
}
siegeManager.runTaskTimerAsynchronously(this, 0, 20);

getServer().getPluginManager().registerEvents(new TypesReloadedListener(), this);
}

this.getCommand("assaultinfo").setExecutor(new AssaultInfoCommand());
this.getCommand("assault").setExecutor(new AssaultCommand());
this.getCommand("assaultrepair").setExecutor(new AssaultRepairCommand());
warfareUtils = new WarfareUtils();
warfareUtils.runTaskTimer(this, 0, 1);

this.getCommand("siege").setExecutor(new SiegeCommand());
}
getCommand("assaultinfo").setExecutor(new AssaultInfoCommand());
getCommand("assault").setExecutor(new AssaultCommand());
getCommand("assaultrepair").setExecutor(new AssaultRepairCommand());

@Override
public void onDisable() {
// Plugin shutdown logic
getCommand("siege").setExecutor(new SiegeCommand());
}

public AssaultManager getAssaultManager() {
Expand All @@ -167,7 +160,7 @@ public SiegeManager getSiegeManager() {
return siegeManager;
}

public void reloadTypes() {
// Currently nothing is needed here, since craft types are referred to as strings.
public WarfareUtils getWarfareUtils() {
return warfareUtils;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import org.bukkit.Material;

import java.util.HashSet;
import java.util.Set;

public class Config {

// Localisation
public static String Locale = "en";

Expand All @@ -21,7 +20,7 @@ public class Config {
public static double AssaultMaxBalance;
public static double AssaultOwnerWeightPercent;
public static double AssaultMemberWeightPercent;
public static HashSet<Material> AssaultDestroyableBlocks;
public static Set<Material> AssaultDestroyableBlocks;
public static int AssaultDamagesPerBlock;
public static int AssaultChunkSavePerTick;
public static int AssaultChunkRepairPerTick;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.countercraft.movecraft.warfare.utils;

import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.repair.MovecraftRepair;
import net.countercraft.movecraft.warfare.MovecraftWarfare;
import net.countercraft.movecraft.warfare.config.Config;
import net.countercraft.movecraft.warfare.events.AssaultBroadcastEvent;
import net.countercraft.movecraft.warfare.localisation.I18nSupport;
Expand All @@ -18,16 +18,13 @@

public class ChunkRepairTask extends BukkitRunnable {
private final String regionName;
private final World w;
private final Queue<Chunk> chunks;
private final File saveDirectory;
private final Predicate<MovecraftLocation> regionTester;
private final Player player;


public ChunkRepairTask(String regionName, World w, Queue<Chunk> chunks, File saveDirectory, Predicate<MovecraftLocation> regionTester, @Nullable Player player) {
public ChunkRepairTask(String regionName, Queue<Chunk> chunks, File saveDirectory, Predicate<MovecraftLocation> regionTester, @Nullable Player player) {
this.regionName = regionName;
this.w = w;
this.chunks = chunks;
this.saveDirectory = saveDirectory;
this.regionTester = regionTester;
Expand All @@ -36,35 +33,38 @@ public ChunkRepairTask(String regionName, World w, Queue<Chunk> chunks, File sav

@Override
public void run() {
for(int i = 0; i < Config.AssaultChunkRepairPerTick; i++) {
for (int i = 0; i < Config.AssaultChunkRepairPerTick; i++) {
Chunk c = chunks.poll();
if(c == null) {
if(chunks.size() == 0) {
if(player == null) {
String broadcast = String.format(I18nSupport.getInternationalisedString("Assault - Repair Finished"), regionName);
if (c == null) {
if (chunks.size() == 0) {
if (player == null) {
String broadcast = String.format(
I18nSupport.getInternationalisedString("Assault - Repair Finished"), regionName);
Bukkit.broadcastMessage(broadcast);

// Note: there is no assault to pass here...
AssaultBroadcastEvent event = new AssaultBroadcastEvent(null, broadcast, AssaultBroadcastEvent.Type.REPAIR_FINISHED);
AssaultBroadcastEvent event = new AssaultBroadcastEvent(null, broadcast,
AssaultBroadcastEvent.Type.REPAIR_FINISHED);
Bukkit.getServer().getPluginManager().callEvent(event);
}
else
player.sendMessage(String.format(I18nSupport.getInternationalisedString("Assault - Repair Finished"), regionName));
} else
player.sendMessage(String.format(
I18nSupport.getInternationalisedString("Assault - Repair Finished"), regionName));
this.cancel();
}
else {
String broadcast = String.format(I18nSupport.getInternationalisedString("Assault - Repair Failed"), regionName);
} else {
String broadcast = String.format(I18nSupport.getInternationalisedString("Assault - Repair Failed"),
regionName);
Bukkit.getServer().broadcastMessage(broadcast);

// Note: there is no assault to pass here...
AssaultBroadcastEvent event = new AssaultBroadcastEvent(null, broadcast, AssaultBroadcastEvent.Type.REPAIR_FAIL);
AssaultBroadcastEvent event = new AssaultBroadcastEvent(null, broadcast,
AssaultBroadcastEvent.Type.REPAIR_FAIL);
Bukkit.getServer().getPluginManager().callEvent(event);
this.cancel();
}
return;
}

if (!MovecraftRepair.getInstance().getWEUtils().repairChunk(c, saveDirectory, regionTester)) {
if (!MovecraftWarfare.getInstance().getWarfareUtils().repairChunk(c, saveDirectory, regionTester)) {
this.cancel();
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.countercraft.movecraft.warfare.utils;

import net.countercraft.movecraft.repair.MovecraftRepair;
import net.countercraft.movecraft.warfare.MovecraftWarfare;
import net.countercraft.movecraft.warfare.assault.Assault;
import net.countercraft.movecraft.warfare.config.Config;
import org.bukkit.Chunk;
Expand All @@ -22,23 +22,22 @@ public ChunkSaveTask(Assault a, Queue<Chunk> chunks, File saveDirectory) {

@Override
public void run() {
if(a.getSavedCorrectly().get() != Assault.SavedState.UNSAVED)
if (a.getSavedCorrectly().get() != Assault.SavedState.UNSAVED)
return;

for(int i = 0; i < Config.AssaultChunkSavePerTick; i++) {
for (int i = 0; i < Config.AssaultChunkSavePerTick; i++) {
Chunk c = chunks.poll();
if(c == null) {
if(chunks.size() == 0) {
if (c == null) {
if (chunks.size() == 0) {
a.getSavedCorrectly().set(Assault.SavedState.SAVED);
this.cancel();
}
else {
} else {
a.getSavedCorrectly().set(Assault.SavedState.FAILED);
}
return;
}

if(!MovecraftRepair.getInstance().getWEUtils().saveChunk(c, saveDirectory, Config.AssaultDestroyableBlocks)) {
if (!MovecraftWarfare.getInstance().getWarfareUtils().saveChunk(c, saveDirectory, Config.AssaultDestroyableBlocks)) {
a.getSavedCorrectly().set(Assault.SavedState.FAILED);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean repairRegionRepairState(World world, String regionName, @Nullable
if(regionTester == null)
return false;

ChunkRepairTask repairTask = new ChunkRepairTask(regionName, world, chunks, saveDirectory, regionTester, player);
ChunkRepairTask repairTask = new ChunkRepairTask(regionName, chunks, saveDirectory, regionTester, player);
repairTask.runTaskTimer(MovecraftWarfare.getInstance(), 2, Config.AssaultChunkRepairPeriod);
return true;
}
Expand Down

0 comments on commit 30ee7e8

Please sign in to comment.