Rework events

This commit is contained in:
Mathieu Strypsteen 2024-09-14 22:35:55 +02:00
parent 21ab16c11e
commit 1e922c93a8
4 changed files with 46 additions and 36 deletions

View file

@ -1,13 +1,34 @@
package gent.zeus.mc13dtl.events; package gent.zeus.mc13dtl.events;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Team;
public interface Event { import net.md_5.bungee.api.ChatColor;
public String getMessage();
public int getScore(); public abstract class Event {
protected Team team;
public int getDifficultyThreshold(); public Event(Team team) {
this.team = team;
for (Player player : getPlayers()) {
player.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + getMessage());
}
}
public void run(Player player); protected List<Player> getPlayers() {
List<Player> players = new ArrayList<>();
for (String entry : team.getEntries()) {
Player player = Bukkit.getPlayer(entry);
if (player != null) {
players.add(player);
}
}
return players;
}
protected abstract String getMessage();
} }

View file

@ -1,20 +1,22 @@
package gent.zeus.mc13dtl.events; package gent.zeus.mc13dtl.events;
import org.bukkit.entity.Player; import org.bukkit.scoreboard.Team;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Arrays; import java.util.Arrays;
import gent.zeus.mc13dtl.events.catalog.TestEvent; import gent.zeus.mc13dtl.events.catalog.TestEvent;
import net.md_5.bungee.api.ChatColor;
public class RandomEventExecutor { public class RandomEventExecutor {
static List<Event> events = Arrays.asList(new TestEvent()); static List<Class<? extends Event>> events = Arrays.asList(TestEvent.class);
public static void executeRandomEvent(Player player) { public static void executeRandomEvent(Team team) {
Random random = new Random(); Random random = new Random();
int i = random.nextInt(events.size()); int i = random.nextInt(events.size());
Event event = events.get(i); Class<? extends Event> event = events.get(i);
player.sendMessage(ChatColor.ITALIC + event.getMessage()); try {
event.run(player); event.getConstructor(Team.class).newInstance(team);
} catch (Exception e) {
}
} }
} }

View file

@ -2,9 +2,9 @@ package gent.zeus.mc13dtl.events;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.MC13DTL; import gent.zeus.mc13dtl.MC13DTL;
@ -16,12 +16,8 @@ public class RandomEventRunner implements Runnable {
long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG); long lastRunDay = world.getPersistentDataContainer().get(eventKey, PersistentDataType.LONG);
long day = Bukkit.getWorld("world").getFullTime() / 24000; long day = Bukkit.getWorld("world").getFullTime() / 24000;
if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) { if (day > lastRunDay && Bukkit.getWorld("world").getTime() >= 14000) {
for (OfflinePlayer i : Bukkit.getOfflinePlayers()) { for (Team i : MC13DTL.scoreboard.getTeams()) {
if (i.isOnline()) { RandomEventExecutor.executeRandomEvent(i);
RandomEventExecutor.executeRandomEvent(i.getPlayer());
} else {
// TODO: Punishment
}
} }
world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day); world.getPersistentDataContainer().set(eventKey, PersistentDataType.LONG, day);
} }

View file

@ -1,26 +1,17 @@
package gent.zeus.mc13dtl.events.catalog; package gent.zeus.mc13dtl.events.catalog;
import org.bukkit.entity.Player; import org.bukkit.scoreboard.Team;
import gent.zeus.mc13dtl.events.Event; import gent.zeus.mc13dtl.events.Event;
public class TestEvent implements Event { public class TestEvent extends Event {
public TestEvent(Team team) {
super(team);
System.err.println("test");
}
@Override @Override
public String getMessage() { protected String getMessage() {
return "test"; return "test";
} }
@Override
public int getScore() {
return 1;
}
@Override
public int getDifficultyThreshold() {
return 0;
}
@Override
public void run(Player player) {
}
} }