diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml index 8c89be5..9b27a19 100644 --- a/.idea/jarRepositories.xml +++ b/.idea/jarRepositories.xml @@ -1,6 +1,11 @@ + + diff --git a/src/main/java/hu/jgj52/wolfFFA/Main.java b/src/main/java/hu/jgj52/wolfFFA/Main.java index c81e4c8..3fae19e 100644 --- a/src/main/java/hu/jgj52/wolfFFA/Main.java +++ b/src/main/java/hu/jgj52/wolfFFA/Main.java @@ -3,24 +3,37 @@ package hu.jgj52.wolfFFA; import hu.jgj52.wolfFFA.Commands.EditKitCommand; import hu.jgj52.wolfFFA.Commands.TpToFfaCommand; import hu.jgj52.wolfFFA.Listeners.KitListener; +import hu.jgj52.wolfFFA.Utils.MapReset; import org.bukkit.plugin.java.JavaPlugin; public final class Main extends JavaPlugin { + private MapReset mapReset; + @Override public void onEnable() { // Plugin startup logic getConfig().options().copyDefaults(true); + saveConfig(); getCommand("tptoffa").setExecutor(new TpToFfaCommand(this)); getCommand("editkit").setExecutor(new EditKitCommand(this)); getServer().getPluginManager().registerEvents(new KitListener(this), this); + this.mapReset = new MapReset(this); } @Override public void onDisable() { // Plugin shutdown logic } + + public static Main getInstance() { + return getPlugin(Main.class); + } + + public MapReset getMapResetManager() { + return this.mapReset; + } } diff --git a/src/main/java/hu/jgj52/wolfFFA/Utils/MapReset.java b/src/main/java/hu/jgj52/wolfFFA/Utils/MapReset.java new file mode 100644 index 0000000..96faaec --- /dev/null +++ b/src/main/java/hu/jgj52/wolfFFA/Utils/MapReset.java @@ -0,0 +1,194 @@ +package hu.jgj52.wolfFFA.Utils; + +import com.fastasyncworldedit.core.FaweAPI; +import com.fastasyncworldedit.core.extent.clipboard.io.ClipboardFormat; +import com.fastasyncworldedit.core.extent.clipboard.io.ClipboardFormats; +import com.fastasyncworldedit.core.extent.clipboard.io.ClipboardReader; +import com.fastasyncworldedit.core.function.operation.Operation; +import com.fastasyncworldedit.core.function.operation.Operations; +import com.fastasyncworldedit.core.worldedit.EditSession; +import com.fastasyncworldedit.core.worldedit.session.ClipboardHolder; +import com.fastasyncworldedit.core.worldedit.math.BlockVector3; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class MapReset { + private final JavaPlugin plugin; + private BukkitTask resetTask; + + public MapReset(JavaPlugin plugin) { + this.plugin = plugin; + mapResetRunnable(); + } + + public boolean placeSchematic(World world, int x, int y, int z, String schematicName) { + File schematicFile = new File(plugin.getDataFolder() + File.separator + "schematics", schematicName + ".schem"); + + if (!schematicFile.exists()) { + plugin.getLogger().warning("Schematic file not found: " + schematicFile.getAbsolutePath()); + return false; + } + + ClipboardFormat format = ClipboardFormats.findByFile(schematicFile); + if (format == null) { + plugin.getLogger().warning("Unknown schematic format for file: " + schematicFile.getAbsolutePath()); + return false; + } + + try (ClipboardReader reader = format.getReader(new FileInputStream(schematicFile))) { + ClipboardHolder clipboard = new ClipboardHolder(reader.read()); + + EditSession editSession = FaweAPI.getWorld(world.getName()).getEditSession(); + editSession.setFastMode(true); // Optimize for FAWE + + Operation operation = clipboard + .createPaste(editSession) + .to(BlockVector3.at(x, y, z)) + .ignoreAirBlocks(false) + .build(); + + Operations.complete(operation); + editSession.flushSession(); + return true; + } catch (IOException e) { + plugin.getLogger().severe("Error reading schematic file: " + e.getMessage()); + e.printStackTrace(); + return false; + } catch (Exception e) { + plugin.getLogger().severe("Error placing schematic: " + e.getMessage()); + e.printStackTrace(); + return false; + } + } + + public boolean placeSchematic(Location location, String schematicName) { + return placeSchematic( + location.getWorld(), + location.getBlockX(), + location.getBlockY(), + location.getBlockZ(), + schematicName + ); + } + + public void placeSchematicsSequentially() { + class SchematicPlacement { + final World world; + final int x, y, z; + final String name; + + SchematicPlacement(World world, int x, int y, int z, String name) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + this.name = name; + } + } + + List schematics = new ArrayList<>(); + schematics.add(new SchematicPlacement(Bukkit.getWorld("world"), -76, 91, -1076, "uhc")); + schematics.add(new SchematicPlacement(Bukkit.getWorld("world"), -1078, 87, 1076, "boxcart")); + + new BukkitRunnable() { + int index = 0; + + @Override + public void run() { + if (index < schematics.size()) { + SchematicPlacement placement = schematics.get(index); + for (Player player : Bukkit.getOnlinePlayers()) { + player.sendMessage("§ePlacing schematic " + placement.name + "..."); + } + + boolean success = placeSchematic( + placement.world, + placement.x, + placement.y, + placement.z, + placement.name + ); + + if (success) { + for (Player player : Bukkit.getOnlinePlayers()) { + player.sendMessage("§aSchematic " + placement.name + " placed successfully!"); + } + } else { + for (Player player : Bukkit.getOnlinePlayers()) { + player.sendMessage("§cFailed to place schematic " + placement.name); + } + } + + index++; + } else { + removeEntities(); + for (Player player : Bukkit.getOnlinePlayers()) { + player.sendMessage("§cA mapok resetelve lettek!"); + } + this.cancel(); + } + } + }.runTaskTimer(plugin, 0L, 20L); + } + + private void removeEntities() { + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (entity.getType() == EntityType.TNT_MINECART || + entity.getType() == EntityType.END_CRYSTAL) { + entity.remove(); + } + } + } + } + + public void mapResetRunnable() { + if (resetTask != null && !resetTask.isCancelled()) { + resetTask.cancel(); + } + + resetTask = new BukkitRunnable() { + @Override + public void run() { + new BukkitRunnable() { + int countdown = 5; + + @Override + public void run() { + if (countdown > 0) { + for (Player player : Bukkit.getOnlinePlayers()) { + player.sendMessage("§cA mapok resetelődnek " + countdown + " másodperc múlva!"); + } + countdown--; + } else { + // Start sequential schematic placement + placeSchematicsSequentially(); + this.cancel(); + } + } + }.runTaskTimer(plugin, 0L, 20L); + } + }.runTaskTimer(plugin, 0L, 12000L); + } + + // Method to stop the scheduler (call this in onDisable) + public void stopScheduler() { + if (resetTask != null && !resetTask.isCancelled()) { + resetTask.cancel(); + resetTask = null; + } + } +}