mirror of
https://github.com/DerTyp7/mc-collect-random-items.git
synced 2025-10-30 21:17:08 +01:00
first commit
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/main/java/de/tealfire/collectrandomitem/Events.java
Normal file
13
src/main/java/de/tealfire/collectrandomitem/Events.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
220
src/main/java/de/tealfire/collectrandomitem/MaterialManager.java
Normal file
220
src/main/java/de/tealfire/collectrandomitem/MaterialManager.java
Normal file
@@ -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<Material> materials = new ArrayList<Material>();
|
||||
ArrayList<Material> finishedMaterials = new ArrayList<Material>();
|
||||
ArrayList<Material> skippedMaterials = new ArrayList<Material>();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/main/resources/plugin.yml
Normal file
37
src/main/resources/plugin.yml
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user