first commit

This commit is contained in:
Janis
2022-11-01 03:12:42 +01:00
commit 7111ba01bc
6 changed files with 700 additions and 0 deletions

113
.gitignore vendored Normal file
View File

@@ -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/

75
pom.xml Normal file
View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.tealfire</groupId>
<artifactId>CollectRandomItem</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CollectRandomItem</name>
<description>Collect random items in mc</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.19.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -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());
}
}
}

View 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());
}
}

View 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());
}
}
}

View 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