Big update

This commit is contained in:
2022-01-03 21:04:39 -06:00
parent 921a62cd58
commit 88c70e84cc
59 changed files with 407 additions and 296 deletions

View File

@ -2,14 +2,19 @@ package dev.plex.services;
public abstract class AbstractService implements IService
{
private boolean asynchronous;
private boolean repeating;
public AbstractService(boolean async)
public AbstractService(boolean repeating, boolean async)
{
this.repeating = repeating;
this.asynchronous = async;
}
public boolean isRepeating() {
return repeating;
}
public boolean isAsynchronous() {
return asynchronous;
}

View File

@ -2,7 +2,6 @@ package dev.plex.services;
public interface IService
{
void run();
int repeatInSeconds();

View File

@ -3,42 +3,36 @@ package dev.plex.services;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.services.impl.BanService;
import dev.plex.services.impl.GameruleService;
import org.bukkit.Bukkit;
import java.util.List;
public class ServiceManager
{
public class ServiceManager {
private final List<AbstractService> services = Lists.newArrayList();
public ServiceManager()
{
public ServiceManager() {
registerService(new BanService());
registerService(new GameruleService());
}
public void startServices()
{
for (AbstractService service : services)
{
if (service.isAsynchronous())
{
public void startServices() {
for (AbstractService service : services) {
if (!service.isRepeating()) {
Bukkit.getScheduler().runTask(Plex.get(), service::run);
} else if (service.isRepeating() && service.isAsynchronous()) {
Bukkit.getScheduler().runTaskTimerAsynchronously(Plex.get(), service::run, 0, 20 * service.repeatInSeconds());
} else {
} else if (service.isRepeating() && !service.isAsynchronous()) {
Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20 * service.repeatInSeconds());
}
}
}
private void registerService(AbstractService service)
{
private void registerService(AbstractService service) {
services.add(service);
}
public int serviceCount()
{
public int serviceCount() {
return services.size();
}
}

View File

@ -1,8 +1,8 @@
package dev.plex.services.impl;
import dev.plex.Plex;
import dev.plex.services.AbstractService;
import dev.plex.banning.Ban;
import dev.plex.services.AbstractService;
import org.bukkit.Bukkit;
import java.util.Date;
@ -10,7 +10,7 @@ import java.util.Date;
public class BanService extends AbstractService
{
public BanService() {
super(true);
super(true, true);
}
@Override

View File

@ -0,0 +1,92 @@
package dev.plex.services.impl;
import dev.plex.Plex;
import dev.plex.services.AbstractService;
import dev.plex.util.PlexLog;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public class GameruleService extends AbstractService {
private final Map<GameRule, Boolean> rules = new EnumMap<>(GameRule.class);
public GameruleService() {
super(false, false);
}
public void setGameRule(GameRule gameRule, boolean value) {
setGameRule(gameRule, value, true);
}
public void setGameRule(GameRule gameRule, boolean value, boolean doCommit) {
rules.put(gameRule, value);
if (doCommit) {
commitGameRules();
}
}
@SuppressWarnings("deprecation")
public void commitGameRules() {
List<World> worlds = Bukkit.getWorlds();
for (Map.Entry<GameRule, Boolean> gameRuleEntry : rules.entrySet()) {
String gameRuleName = gameRuleEntry.getKey().getGameRuleName();
String gameRuleValue = gameRuleEntry.getValue().toString();
for (World world : worlds) {
world.setGameRuleValue(gameRuleName, gameRuleValue);
if (gameRuleEntry.getKey() == GameRule.DO_DAYLIGHT_CYCLE && !gameRuleEntry.getValue()) {
long time = world.getTime();
time -= time % 24000;
world.setTime(time + 24000 + 6000);
}
}
}
}
@Override
public void run() {
for (GameRule gameRule : GameRule.values()) {
rules.put(gameRule, gameRule.getDefaultValue());
PlexLog.log(gameRule.toString());
}
}
@Override
public int repeatInSeconds() {
return 0;
}
public enum GameRule {
DO_FIRE_TICK("doFireTick", true),
MOB_GRIEFING("mobGriefing", true),
KEEP_INVENTORY("keepInventory", true),
DO_MOB_SPAWNING("doMobSpawning", true),
DO_MOB_LOOT("doMobLoot", true),
DO_TILE_DROPS("doTileDrops", true),
COMMAND_BLOCK_OUTPUT("commandBlockOutput", true),
NATURAL_REGENERATION("naturalRegeneration", true),
DO_DAYLIGHT_CYCLE("doDaylightCycle", true),
ANNOUNCE_ADVANCEMENTS("announceAdvancements", true),
SHOW_DEATH_MESSAGES("showDeathMessages", true),
SEND_COMMAND_FEEDBACK("sendCommandFeedback", true);
private final String gameRuleName;
private final boolean defaultValue;
GameRule(String gameRuleName, boolean defaultValue) {
this.gameRuleName = gameRuleName;
this.defaultValue = defaultValue;
}
public String getGameRuleName() {
return gameRuleName;
}
public boolean getDefaultValue() {
return defaultValue;
}
}
}