From 7111ba01bc76882f474d5bd0cf5ed909171d6f8a Mon Sep 17 00:00:00 2001 From: Janis Date: Tue, 1 Nov 2022 03:12:42 +0100 Subject: [PATCH] first commit --- .gitignore | 113 ++++++++ pom.xml | 75 ++++++ .../collectrandomitem/CollectRandomItem.java | 242 ++++++++++++++++++ .../de/tealfire/collectrandomitem/Events.java | 13 + .../collectrandomitem/MaterialManager.java | 220 ++++++++++++++++ src/main/resources/plugin.yml | 37 +++ 6 files changed, 700 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/de/tealfire/collectrandomitem/CollectRandomItem.java create mode 100644 src/main/java/de/tealfire/collectrandomitem/Events.java create mode 100644 src/main/java/de/tealfire/collectrandomitem/MaterialManager.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5a7ed02 --- /dev/null +++ b/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + de.tealfire + CollectRandomItem + 1.0-SNAPSHOT + jar + + CollectRandomItem + + Collect random items in mc + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + io.papermc.paper + paper-api + 1.19.2-R0.1-SNAPSHOT + provided + + + diff --git a/src/main/java/de/tealfire/collectrandomitem/CollectRandomItem.java b/src/main/java/de/tealfire/collectrandomitem/CollectRandomItem.java new file mode 100644 index 0000000..8353ed8 --- /dev/null +++ b/src/main/java/de/tealfire/collectrandomitem/CollectRandomItem.java @@ -0,0 +1,242 @@ +package de.tealfire.collectrandomitem; + +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; +import org.jetbrains.annotations.NotNull; + +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public final class CollectRandomItem extends JavaPlugin implements Listener { + public MaterialManager materialManager; + public long timerTicks = 0L; + public boolean isRunning = false; + public boolean isPaused = false; + + public void start() { + + if (!isRunning) { + materialManager = new MaterialManager(); + isRunning = true; + loadTimerFromFile(); + getLogger().info("Started!"); + } + } + + public void restart() { + if (isRunning) { + stop(); + start(); + resume(); + } + } + + public void stop() { + if (isRunning) { + materialManager = null; + File skippedItemsFile = new File("plugins/skippedItems.txt"); + File finishedItemsFile = new File("plugins/finishedItems.txt"); + File timerFile = new File("plugins/timer.txt"); + + skippedItemsFile.delete(); + finishedItemsFile.delete(); + timerFile.delete(); + + isRunning = false; + getLogger().info("Stopped!"); + } + + } + + public void pause() { + if (isRunning) { + isPaused = true; + getLogger().info("Paused!"); + } + } + + public void resume() { + if (isPaused) { + isPaused = false; + getLogger().info("Resumed!"); + } + } + + public String getTimerString() { + long seconds = (long) (timerTicks) / 20L; + SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm:ss"); + sdfDate.setTimeZone(TimeZone.getTimeZone("UTC")); + Date timerDate = new Date(seconds * 1000); + return sdfDate.format(timerDate); + } + + public void saveTimerToFile() { + System.out.println("Saving timer to file..."); + try { + PrintWriter writer = new PrintWriter("plugins/timer.txt", "UTF-8"); + writer.println(Long.toString(timerTicks)); + writer.close(); + } catch (IOException e) { + System.out.println(e.toString()); + } + } + + public void saveTimerStringToFile() { + try { + PrintWriter writer = new PrintWriter("plugins/timerString.txt", "UTF-8"); + writer.println(getTimerString()); + writer.close(); + } catch (IOException e) { + System.out.println(e.toString()); + } + } + + public void loadTimerFromFile() { + System.out.println("Loading file plugins/timer.txt..."); + try { + BufferedReader reader = new BufferedReader(new FileReader("plugins/timer.txt")); + timerTicks = Long.parseLong(reader.readLine()); + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + timerTicks = 0L; + } + } + + @Override + public void onEnable() { + BukkitScheduler scheduler = Bukkit.getScheduler(); + getServer().getPluginManager().registerEvents(this, this); + + File skippedItemsFile = new File("plugins/skippedItems.txt"); + File finishedItemsFile = new File("plugins/finishedItems.txt"); + if ((skippedItemsFile.exists() && !skippedItemsFile.isDirectory()) || (finishedItemsFile.exists() && !finishedItemsFile.isDirectory())) { + start(); + pause(); + } + + long updateInterval = 3L; + + scheduler.runTaskTimer(this, () -> { + if (isRunning) { + if (!isPaused) { + timerTicks += updateInterval; + saveTimerStringToFile(); + for (Player player : + Bukkit.getOnlinePlayers()) { + if (player != null) { + if (materialManager.scanInventoryForItem(player.getInventory(), materialManager.wantedMaterial)) { + materialManager.finishWantedMaterial(); + } + } + } + if (Bukkit.getOnlinePlayers().size() == 0) { + pause(); + } + } + + + if (materialManager != null) { + String msg = ""; + if (isPaused) { + msg = "" + ChatColor.BOLD + ChatColor.GRAY + "[PAUSED] " + "[" + Integer.toString(materialManager.countClosedMaterials()) + "/" + + Integer.toString(materialManager.countMaxMaterials()) + "] " + ChatColor.GOLD + materialManager.wantedMaterial.toString().toLowerCase(); + } else { + msg = "" + ChatColor.BOLD + ChatColor.GRAY + "[" + Integer.toString(materialManager.countClosedMaterials()) + "/" + + Integer.toString(materialManager.countMaxMaterials()) + "] " + ChatColor.GOLD + materialManager.wantedMaterial.toString().toLowerCase(); + } + + for (Audience a : Bukkit.getServer().audiences() + ) { + a.sendActionBar(Component.text(msg)); + } + } + } + }, updateInterval /*<-- the initial delay */, updateInterval /*<-- the interval */); + + Bukkit.getLogger().info("CollectRandomItems started!"); + } + + @Override + public void onDisable() { + if (materialManager != null) { + materialManager.saveToFile(); + } + } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + + if (command.getName().equalsIgnoreCase("skip")) { + if (isRunning) { + if (args.length > 0) { + for (int i = 0; i <= Integer.parseInt(args[0]); i++) { + materialManager.skipWantedMaterial(); + } + } else { + materialManager.skipWantedMaterial(); + } + } + + } else if (command.getName().equalsIgnoreCase("unskip")) { + if (isRunning) { + if (args.length > 0) { + for (int i = 0; i <= Integer.parseInt(args[0]); i++) { + materialManager.unskipLast(); + } + } else { + materialManager.unskipLast(); + } + } + } else if (command.getName().equalsIgnoreCase("itemsstop")) { + stop(); + } else if (command.getName().equalsIgnoreCase("pause")) { + pause(); + } else if (command.getName().equalsIgnoreCase("resume")) { + resume(); + } else if (command.getName().equalsIgnoreCase("itemsrestart")) { + restart(); + } else if (command.getName().equalsIgnoreCase("itemsstart")) { + start(); + } else if (command.getName().equalsIgnoreCase("reroll")) { + if (isRunning) { + materialManager.updateWantedMaterial(); + } + } else if (command.getName().equalsIgnoreCase("finishedItems")) { + materialManager.showFinished(); + } else if (command.getName().equalsIgnoreCase("skippedItems")) { + materialManager.showSkipped(); + } else if (command.getName().equalsIgnoreCase("closedItems")) { + materialManager.showClosed(); + } else if (command.getName().equalsIgnoreCase("remainingItems")) { + materialManager.showRemaining(); + } else if (command.getName().equalsIgnoreCase("itemsSave")) { + saveTimerToFile(); + materialManager.saveToFile(); + } else if (command.getName().equalsIgnoreCase("timer")) { + sender.sendMessage("Timer -> " + ChatColor.BOLD + ChatColor.YELLOW + "[" + getTimerString() + "]"); + } else if (command.getName().equalsIgnoreCase("timerReset")) { + timerTicks = 0L; + } + return true; + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + if (isRunning) { + event.getPlayer().sendMessage("Current Item -> " + materialManager.wantedMaterial.toString()); + } + } +} diff --git a/src/main/java/de/tealfire/collectrandomitem/Events.java b/src/main/java/de/tealfire/collectrandomitem/Events.java new file mode 100644 index 0000000..908ad14 --- /dev/null +++ b/src/main/java/de/tealfire/collectrandomitem/Events.java @@ -0,0 +1,13 @@ +package de.tealfire.collectrandomitem; + +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.entity.EntityPickupItemEvent; + +public class Events { + @EventHandler + public void onEntityPickupItemEvent(EntityPickupItemEvent event){ + Bukkit.getLogger().info("Pick up"); + Bukkit.getLogger().info( event.getItem().toString()); + } +} diff --git a/src/main/java/de/tealfire/collectrandomitem/MaterialManager.java b/src/main/java/de/tealfire/collectrandomitem/MaterialManager.java new file mode 100644 index 0000000..b4a312d --- /dev/null +++ b/src/main/java/de/tealfire/collectrandomitem/MaterialManager.java @@ -0,0 +1,220 @@ +package de.tealfire.collectrandomitem; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Random; + +public class MaterialManager { + + boolean won = false; + public Material wantedMaterial; + ArrayList materials = new ArrayList(); + ArrayList finishedMaterials = new ArrayList(); + ArrayList skippedMaterials = new ArrayList(); + + public MaterialManager() { + System.out.println("Loading materials..."); + for (Material material : Material.values() + ) { + if (material != null) { + if (material.isItem() && !material.name().toLowerCase().contains("spawn_egg")) { + // System.out.println(material.name()); + materials.add(material); + } + } + } + System.out.println("Loading save files..."); + loadFromFile(); + System.out.println("Updating wanted material..."); + updateWantedMaterial(); + } + + public Material getRandomMaterial() { + return materials.get(new Random().nextInt(materials.size())); + } + + public int countMaxMaterials() { + return materials.size(); + } + + public int countClosedMaterials() { + return finishedMaterials.size() + skippedMaterials.size(); + } + + public int countRemainingMaterials() { + return countMaxMaterials() - countClosedMaterials(); + } + + public void broadcastMessage(String msg) { + System.out.println(msg); + for (Player p : + Bukkit.getOnlinePlayers() + ) { + p.sendMessage(msg); + } + } + + public boolean scanInventoryForItem(PlayerInventory inv, Material material) { + for (ItemStack i : inv.getContents()) { + if (i != null) { + if (i.getType() == wantedMaterial) { + return true; + } + } + } + return false; + } + + public void won() { + won = true; + broadcastMessage("You finished all!"); + } + + public void finishWantedMaterial() { + if (!won) { + + broadcastMessage(ChatColor.GREEN + wantedMaterial.toString().toLowerCase() + " finished"); + finishedMaterials.add(wantedMaterial); + updateWantedMaterial(); + } + + } + + public void skipWantedMaterial() { + if (!won) { + broadcastMessage(ChatColor.GRAY + wantedMaterial.toString().toLowerCase() + " skipped"); + skippedMaterials.add(wantedMaterial); + updateWantedMaterial(); + } + + } + + public void unskipLast() { + if (skippedMaterials.size() > 0) { + Material material = skippedMaterials.get(skippedMaterials.size() - 1); + skippedMaterials.remove(skippedMaterials.size() - 1); + setWantedMaterial(material); + } + } + + public void showSkipped() { + for (Material material : skippedMaterials + ) { + broadcastMessage("" + ChatColor.BOLD + ChatColor.GRAY + "Skipped: " + material.toString().toLowerCase()); + } + } + + public void showFinished() { + for (Material material : finishedMaterials + ) { + broadcastMessage("" + ChatColor.BOLD + ChatColor.GREEN + "Finished: " + material.toString().toLowerCase()); + } + } + + public void showClosed() { + showSkipped(); + showFinished(); + } + + public void showRemaining() { + for (Material material : materials + ) { + if ((finishedMaterials.contains(material) || skippedMaterials.contains(material))) { + broadcastMessage("" + ChatColor.BOLD + ChatColor.YELLOW + "Remaining: " + material.toString().toLowerCase()); + } + } + } + + public void updateWantedMaterial() { + if (countRemainingMaterials() <= 0) { + won(); + } else { + Material newMaterial = getRandomMaterial(); + + while (finishedMaterials.contains(newMaterial) || skippedMaterials.contains(newMaterial)) { + newMaterial = getRandomMaterial(); + } + setWantedMaterial(newMaterial); + + } + } + + public void setWantedMaterial(Material newMaterial) { + wantedMaterial = newMaterial; + broadcastMessage("[" + Integer.toString(countClosedMaterials()) + "/" + Integer.toString(countMaxMaterials()) + "]" + + " -> " + ChatColor.GOLD + ChatColor.BOLD + newMaterial.toString()); + } + + + public void loadFromFile() { + System.out.println("Loading from files..."); + System.out.println("Loading file plugins/skippedItems.txt..."); + try { + BufferedReader reader = new BufferedReader(new FileReader("plugins/skippedItems.txt")); + String line = reader.readLine(); + while (line != null) { + System.out.println(line); + skippedMaterials.add(Material.getMaterial(line)); + // read next line + line = reader.readLine(); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("Loading file plugins/finishedItems.txt..."); + try { + BufferedReader reader = new BufferedReader(new FileReader("plugins/finishedItems.txt")); + String line = reader.readLine(); + while (line != null) { + System.out.println(line); + finishedMaterials.add(Material.getMaterial(line)); + // read next line + line = reader.readLine(); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void saveToFile() { + System.out.println("Saving to files..."); + + System.out.println("Saving skippedItems"); + try { + PrintWriter writer = new PrintWriter("plugins/skippedItems.txt", "UTF-8"); + for (Material material : skippedMaterials + ) { + writer.println(material.name()); + + } + writer.close(); + } catch (IOException e) { + System.out.println(e.toString()); + } + + System.out.println("Saving finishedItems"); + try { + PrintWriter writer = new PrintWriter("plugins/finishedItems.txt", "UTF-8"); + for (Material material : finishedMaterials + ) { + writer.println(material.name()); + + } + writer.close(); + } catch (IOException e) { + System.out.println(e.toString()); + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..c3f592a --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,37 @@ +name: CollectRandomItem +version: '${project.version}' +main: de.tealfire.collectrandomitem.CollectRandomItem +api-version: 1.19 +authors: [ DerTyp876 ] +description: Collect random items in mc +commands: + skip: + description: Skip item + unskip: + description: Unskip last item + itemsStart: + description: Start + itemsStop: + description: Stop + itemsRestart: + description: Restart + pause: + description: Pause + resume: + description: resume + reroll: + description: reroll item + skippedItems: + description: List skipped items + finishedItems: + description: Show finished items + closedItems: + description: Show remaining items + remainingItems: + description: Show remaining items + itemsSave: + description: Save + timer: + description: Shows timer + timerReset: + description: Resets the timer \ No newline at end of file