Skip to content

Commit

Permalink
First working version... now tests tests and tests + preparing for ne…
Browse files Browse the repository at this point in the history
…w features :D
  • Loading branch information
Galajus committed Nov 27, 2021
1 parent ae615fd commit 9ac4152
Show file tree
Hide file tree
Showing 24 changed files with 1,385 additions and 62 deletions.
46 changes: 46 additions & 0 deletions src/main/java/pl/galajus/twitchunt/Clock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pl.galajus.twitchunt;

import org.bukkit.Bukkit;

import java.util.Calendar;

public class Clock {

private final Twitchunt twitchunt;

//Maybe usable in future
//private static int dayOfMonth;
//private static int hourOfDay;
// private static int minuteOfHour;
private static int secondOfMinute;

public Clock(Twitchunt twitchunt) {
this.twitchunt = twitchunt;

Calendar rightNow = Calendar.getInstance();
secondOfMinute = rightNow.get(Calendar.SECOND);

//Maybe usable in future
//minuteOfHour = rightNow.get(Calendar.MINUTE);
//hourOfDay = rightNow.get(Calendar.HOUR_OF_DAY);
//dayOfMonth = rightNow.get(Calendar.DAY_OF_MONTH);

Bukkit.getScheduler().runTaskLater(twitchunt, this::timer, 50);
}

private void timer() {
Bukkit.getScheduler().runTaskAsynchronously(twitchunt, () -> {
Calendar rightNow = Calendar.getInstance();

if (secondOfMinute != rightNow.get(Calendar.SECOND)) {
secondOfMinute = rightNow.get(Calendar.SECOND);

twitchunt.getPollCreator().tick();
}


Bukkit.getScheduler().runTaskLater(twitchunt, this::timer, 9);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void initialize() {
"reload",
"pollsinstastart");

commandTwitchuntChoiceArgsList = List.of("true, false");
commandTwitchuntChoiceArgsList = List.of("true", "false");
}

public List<String> getCommandTwitchuntArgsList() {
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/pl/galajus/twitchunt/Commands/TwitchuntCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

if (args[0].equalsIgnoreCase("reload")) {
twitchunt.getManager().reloadConfigOptions();
twitchunt.getTranslations().initialize();
dependencyResolver.sendTranslatedMessage(sender,"configReloaded");
return true;
}
Expand All @@ -75,28 +76,25 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
dependencyResolver.sendTranslatedMessage(sender,"stopCooldown");
return true;
}
//TODO:
// if (startEffectPolls()) {
// dependencyResolver.sendTranslatedMessage(sender,"pollsStarted");
// } else {
// dependencyResolver.sendTranslatedMessage(sender,"pollsAlreadyStarted");
// }

if (twitchunt.getPollCreator().enablePolls()) {
dependencyResolver.sendTranslatedMessage(sender,"pollsStarted");
} else {
dependencyResolver.sendTranslatedMessage(sender,"pollsAlreadyStarted");
}
return true;
}
if (args[0].equalsIgnoreCase("stop")) {

//TODO:
// if (stopEffectPolls()) {
// dependencyResolver.sendTranslatedMessage(sender,"pollsDisabled");
// Bukkit.getScheduler().cancelTask(TimeChoices.getTaskIDPoll());
// Bukkit.getScheduler().cancelTask(TimeChoices.getTaskIDInterval());
// cooldown = true;
// Bukkit.getScheduler().runTaskLater(twitchunt,
// () -> cooldown = false,
// (configReader.getPollInterval() + 5L) * 20);
// } else {
// dependencyResolver.sendTranslatedMessage(sender,"pollsNotEnabled");
// }
if (twitchunt.getPollCreator().disablePolls()) {
dependencyResolver.sendTranslatedMessage(sender,"pollsDisabled");
cooldown = true;
Bukkit.getScheduler().runTaskLater(twitchunt,
() -> cooldown = false,
(configReader.getPollInterval() + 5L) * 20);
} else {
dependencyResolver.sendTranslatedMessage(sender,"pollsNotEnabled");
}
return true;
}

Expand Down
53 changes: 39 additions & 14 deletions src/main/java/pl/galajus/twitchunt/ConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import pl.galajus.twitchunt.Dependency.DependencyResolver;
import pl.galajus.twitchunt.ObjectsManager.PluginPollChoice;

import java.io.File;
Expand All @@ -16,15 +15,13 @@
public class ConfigReader {

private final Twitchunt twitchunt;
private final DependencyResolver dependencyResolver;

private FileConfiguration mainConfig;
private FileConfiguration effectsConfig;
private FileConfiguration customEffectsConfig;

public ConfigReader(Twitchunt twitchunt, DependencyResolver dependencyResolver) {
public ConfigReader(Twitchunt twitchunt) {
this.twitchunt = twitchunt;
this.dependencyResolver = dependencyResolver;

this.readConfigs();
}
Expand All @@ -47,7 +44,8 @@ public void readConfigs() {
try {
mainConfig.load(fDefault);
} catch (Exception ex) {
dependencyResolver.errorLogToConsole("configLoadError");
Bukkit.getScheduler().runTaskLater(twitchunt, () -> twitchunt.getDependencyResolver().errorLogToConsole("configLoadError"),1);

}
twitchunt.saveDefaultConfig();

Expand Down Expand Up @@ -168,9 +166,16 @@ public boolean addHunted(String hunted) {
List<String> huntedPlayers = getHuntedPlayersAsStrings();
if (!this.getHuntedPlayersAsStrings().contains(hunted)) {
huntedPlayers.add(hunted);
mainConfig.set("huntedPlayers", getHuntedPlayersAsStrings().add(hunted));
mainConfig.set("huntedPlayers", huntedPlayers);
mainConfig.options().header("Config instructions in file: Config-help.txt");
twitchunt.saveConfig();
twitchunt.getEffectController().getEffectCaster().updateHunters();

Bukkit.getScheduler().runTaskLater(twitchunt, () -> {
if (twitchunt.getDependencyResolver().isPaper()) {
twitchunt.getEffectController().getPaperEffectCaster().updateHunters();
}
},1);
return true;
}
return false;
Expand All @@ -182,6 +187,13 @@ public void removeHunted(String hunted) {
mainConfig.set("huntedPlayers", huntedPlayers);
mainConfig.options().header("Config instructions in file: Config-help.txt");
twitchunt.saveConfig();
twitchunt.getEffectController().getEffectCaster().updateHunters();

Bukkit.getScheduler().runTaskLater(twitchunt, () -> {
if (twitchunt.getDependencyResolver().isPaper()) {
twitchunt.getEffectController().getPaperEffectCaster().updateHunters();
}
},1);
}

public void setPollDuration(long duration) {
Expand Down Expand Up @@ -216,16 +228,24 @@ public void setPollInstaStart(boolean instaStart) {

private void validateConfig() {
if (this.getPollDuration() < 20) {
dependencyResolver.warningLogToConsole("configPollDurationInvalid");
Bukkit.getScheduler().runTaskLater(twitchunt, () -> twitchunt.getDependencyResolver().warningLogToConsole("configPollDurationInvalid"),1);
this.setPollDuration(30);
}

if (this.getPollInterval() < (this.getPollDuration() + 4)) {
dependencyResolver.warningLogToConsole("configPollIntervalInvalid");
Bukkit.getScheduler().runTaskLater(twitchunt, () -> twitchunt.getDependencyResolver().warningLogToConsole("configPollIntervalInvalid"),1);
this.setPollInterval(this.getPollDuration() + 10L);
}

this.loadPluginEffects();

Bukkit.getScheduler().runTaskLater(twitchunt, () -> {
twitchunt.getEffectController().getEffectCaster().updateHunters();
if (twitchunt.getDependencyResolver().isPaper()) {
twitchunt.getEffectController().getPaperEffectCaster().updateHunters();
}
}, 1);

}

private void loadPluginEffects() {
Expand All @@ -241,9 +261,12 @@ private void loadPluginEffects() {
lore = "[empty lore]";
}
if (lore.length() > 25) {
dependencyResolver.errorLogToConsole("tooLongLore1");
dependencyResolver.errorLogToConsole("tooLongLore2");
Bukkit.getPluginManager().disablePlugin(twitchunt);
Bukkit.getScheduler().runTaskLater(twitchunt, () -> {
twitchunt.getDependencyResolver().errorLogToConsole("tooLongLore1");
twitchunt.getDependencyResolver().errorLogToConsole("tooLongLore2");
Bukkit.getPluginManager().disablePlugin(twitchunt);
},1);

return;
}
new PluginPollChoice(id, lore);
Expand All @@ -270,9 +293,11 @@ private void loadCustomEffects() {
lore = "[empty lore]";
}
if (lore.length() > 25) {
dependencyResolver.errorLogToConsole("tooLongLore1");
dependencyResolver.errorLogToConsole("tooLongLore2");
Bukkit.getPluginManager().disablePlugin(twitchunt);
Bukkit.getScheduler().runTaskLater(twitchunt, () -> {
twitchunt.getDependencyResolver().errorLogToConsole("tooLongLore1");
twitchunt.getDependencyResolver().errorLogToConsole("tooLongLore2");
Bukkit.getPluginManager().disablePlugin(twitchunt);
},1);
return;
}
new PluginPollChoice(id, lore, command1, command2, delay, commandDelay1, commandDelay2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import pl.galajus.twitchunt.Minecraft.Events.*;
import pl.galajus.twitchunt.Minecraft.EventsController;
import pl.galajus.twitchunt.Translations;
import pl.galajus.twitchunt.Twitchunt;

Expand All @@ -21,7 +23,6 @@ public DependencyResolver(Twitchunt twitchunt, Translations translations) {
this.translations = translations;
this.isPaper = paperCheck();
this.registerPaperDependencies();
this.registerEvents();
}

public void sendMessage(Player p, String message) {
Expand Down Expand Up @@ -132,8 +133,23 @@ public void errorLogToConsole(String key) {
Bukkit.getLogger().log(Level.SEVERE, twitchunt.getPrefix() + translations.getTranslation(key));
}

private void registerEvents() {
public void registerEvents() {

EventsController eventsController = twitchunt.getEventsController();
if (isPaper) {
Bukkit.getPluginManager().registerEvents(new ItemPickup(twitchunt, eventsController), twitchunt);
} else {
Bukkit.getPluginManager().registerEvents(new ItemPickupSpigot(twitchunt, eventsController), twitchunt);
}
Bukkit.getPluginManager().registerEvents(new BlockBreak(twitchunt, eventsController), twitchunt);
Bukkit.getPluginManager().registerEvents(new EntityDamageEntity(twitchunt, eventsController), twitchunt);
Bukkit.getPluginManager().registerEvents(new EntityDeath(twitchunt, eventsController), twitchunt);
Bukkit.getPluginManager().registerEvents(new InventoryClick(twitchunt, eventsController), twitchunt);
Bukkit.getPluginManager().registerEvents(new InventoryOpen(twitchunt, eventsController), twitchunt);
}

public boolean isPaper() {
return isPaper;
}

private void registerPaperDependencies() {
Expand All @@ -147,6 +163,11 @@ private void registerPaperDependencies() {
}

private boolean paperCheck() {

if (twitchunt.getConfigReader().getForceSpigotUsage()) {
return false;
}

try {
String paperTest = Class.forName("com.destroystokyo.paper.VersionHistoryManager$VersionData").toString();
if (!paperTest.isBlank()) {
Expand Down
Loading

0 comments on commit 9ac4152

Please sign in to comment.