Skip to content

Commit

Permalink
feat(forge20): port changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danorris709 committed Nov 30, 2024
1 parent 115a0c1 commit 5075a20
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 315 deletions.
1 change: 1 addition & 0 deletions forge20/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
shadow group: 'com.cronutils', name: 'cron-utils', version: '9.2.0'

shadow group: 'com.envyful.api', name: 'commons', version: '7.2.3'
shadow (group: 'com.envyful.api', name: 'sqlite', version: '7.2.3')
shadow (group: 'com.envyful.api', name: 'forge20', version: '7.2.3') {
transitive = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@

import com.envyful.api.concurrency.UtilConcurrency;
import com.envyful.api.concurrency.UtilLogger;
import com.envyful.api.config.database.DatabaseDetailsConfig;
import com.envyful.api.config.database.DatabaseDetailsRegistry;
import com.envyful.api.config.type.SQLDatabaseDetails;
import com.envyful.api.config.yaml.YamlConfigFactory;
import com.envyful.api.database.Database;
import com.envyful.api.database.impl.SimpleHikariDatabase;
import com.envyful.api.forge.chat.ComponentTextFormatter;
import com.envyful.api.forge.command.ForgeCommandFactory;
import com.envyful.api.forge.command.parser.ForgeAnnotationCommandParser;
import com.envyful.api.forge.concurrency.ForgeTaskBuilder;
import com.envyful.api.forge.gui.factory.ForgeGuiFactory;
import com.envyful.api.forge.platform.ForgePlatformHandler;
import com.envyful.api.forge.player.ForgePlayerManager;
import com.envyful.api.gui.factory.GuiFactory;
import com.envyful.api.platform.PlatformProxy;
import com.envyful.api.player.SaveMode;
import com.envyful.api.player.save.impl.JsonSaveManager;
import com.envyful.api.player.Attribute;
import com.envyful.api.sqlite.config.SQLiteDatabaseDetailsConfig;
import com.envyful.reforged.bingo.forge.command.BingoCardCommand;
import com.envyful.reforged.bingo.forge.config.BingoConfig;
import com.envyful.reforged.bingo.forge.config.BingoLocaleConfig;
import com.envyful.reforged.bingo.forge.config.BingoQueries;
import com.envyful.reforged.bingo.forge.listener.BingoCardCompleteListener;
import com.envyful.reforged.bingo.forge.listener.PokemonCatchListener;
import com.envyful.reforged.bingo.forge.player.BingoAttribute;
import com.envyful.reforged.bingo.forge.player.SQLBingoAttributeAdapter;
import com.envyful.reforged.bingo.forge.player.SQLiteBingoAttributeAdapter;
import com.envyful.reforged.bingo.forge.task.CardResetTask;
import com.pixelmonmod.pixelmon.Pixelmon;
import com.pixelmonmod.pixelmon.api.pokemon.species.Species;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
Expand All @@ -33,14 +38,13 @@
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;

@Mod("reforgedbingo")
public class ReforgedBingo {

private static final Logger LOGGER = LogManager.getLogger("reforgedbingo");

private static ReforgedBingo instance;

private ForgePlayerManager playerManager = new ForgePlayerManager();
Expand All @@ -49,63 +53,54 @@ public class ReforgedBingo {
private BingoConfig config;
private BingoLocaleConfig locale;
private Database database;
private Logger logger = LogManager.getLogger("reforgedbingo");

public ReforgedBingo() {
instance = this;
UtilLogger.setLogger(this.logger);
MinecraftForge.EVENT_BUS.register(this);
}
SQLiteDatabaseDetailsConfig.register();
UtilLogger.setLogger(LOGGER);

@SubscribeEvent
public void onInit(ServerAboutToStartEvent event) {
GuiFactory.setPlatformFactory(new ForgeGuiFactory());
GuiFactory.setPlayerManager(this.playerManager);
PlatformProxy.setHandler(ForgePlatformHandler.getInstance());
PlatformProxy.setPlayerManager(this.playerManager);
PlatformProxy.setTextFormatter(ComponentTextFormatter.getInstance());

this.reloadConfig();
MinecraftForge.EVENT_BUS.register(this);
instance = this;
}

if (this.config.getSaveMode() == SaveMode.JSON) {
this.playerManager.setSaveManager(new JsonSaveManager<>(this.playerManager));
}
@SubscribeEvent
public void onInit(ServerAboutToStartEvent event) {
this.reloadConfig();

this.playerManager.registerAttribute(BingoAttribute.class, BingoAttribute::new);
var saveMode = DatabaseDetailsRegistry.getRegistry().getKey((Class<DatabaseDetailsConfig>) this.getConfig().getDatabaseDetails().getClass());

new BingoCardCompleteListener(this);
new PokemonCatchListener(this);
if (saveMode == null) {
getLogger().error("Failed to find save mode for Bingo config. Please check your config and try again");
return;
}

if (this.config.getSaveMode() == SaveMode.MYSQL) {
UtilConcurrency.runAsync(() -> {
this.database = new SimpleHikariDatabase(this.config.getDatabase());
this.playerManager.getSaveManager().setSaveMode(saveMode);
this.playerManager.registerAttribute(Attribute.builder(BingoAttribute.class, ServerPlayer.class)
.constructor(BingoAttribute::new)
.registerAdapter(SQLDatabaseDetails.ID, new SQLBingoAttributeAdapter())
.registerAdapter(SQLiteDatabaseDetailsConfig.ID, new SQLiteBingoAttributeAdapter())
);

try (Connection connection = this.database.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(BingoQueries.CREATE_TABLE)) {
preparedStatement.executeUpdate();
this.database = this.config.getDatabaseDetails().createDatabase();
this.playerManager.getSaveManager().getAdapter(BingoAttribute.class).initialize();

try (PreparedStatement alterStatement = connection.prepareStatement(BingoQueries.ALTER_TABLE)) {
alterStatement.executeUpdate();
} catch (SQLException ignored) {}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
new BingoCardCompleteListener();
Pixelmon.EVENT_BUS.register(new PokemonCatchListener());

new ForgeTaskBuilder()
.async(true)
.delay(10L)
.interval(10L)
.task(new CardResetTask(this))
.start();
UtilConcurrency.runRepeatingTask(new CardResetTask(), 25L, 25L);
}

public void reloadConfig() {
try {
this.config = YamlConfigFactory.getInstance(BingoConfig.class);
this.locale = YamlConfigFactory.getInstance(BingoLocaleConfig.class);
} catch (IOException e) {
e.printStackTrace();
getLogger().error("Failed to load config", e);
}
}

Expand All @@ -118,28 +113,28 @@ public static ReforgedBingo getInstance() {
return instance;
}

public ForgePlayerManager getPlayerManager() {
return this.playerManager;
public static ForgePlayerManager getPlayerManager() {
return instance.playerManager;
}

public BingoConfig getConfig() {
return this.config;
public static BingoConfig getConfig() {
return instance.config;
}

public BingoLocaleConfig getLocale() {
return this.locale;
public static BingoLocaleConfig getLocale() {
return instance.locale;
}

public Database getDatabase() {
return this.database;
public static Database getDatabase() {
return instance.database;
}

public boolean isBlacklisted(Species pokemon) {
if (pokemon.isLegendary() || pokemon.isUltraBeast()) {
return true;
}

for (Species blacklistedSpawn : this.getConfig().getBlacklistedSpawns()) {
for (Species blacklistedSpawn : config.getBlacklistedSpawns()) {
if (Objects.equals(blacklistedSpawn, pokemon)) {
return true;
}
Expand All @@ -149,6 +144,6 @@ public boolean isBlacklisted(Species pokemon) {
}

public static Logger getLogger() {
return instance.logger;
return LOGGER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import com.envyful.api.command.annotate.SubCommands;
import com.envyful.api.command.annotate.executor.CommandProcessor;
import com.envyful.api.command.annotate.executor.Sender;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.api.player.EnvyPlayer;
import com.envyful.api.forge.player.ForgeEnvyPlayer;
import com.envyful.reforged.bingo.forge.ReforgedBingo;
import com.envyful.reforged.bingo.forge.player.BingoAttribute;
import com.envyful.reforged.bingo.forge.ui.BingoCardUI;
import net.minecraft.server.level.ServerPlayer;

@Command(
value = {
Expand All @@ -27,18 +25,11 @@
public class BingoCardCommand {

@CommandProcessor
public void run(@Sender ServerPlayer player, String[] args) {
EnvyPlayer<ServerPlayer> sender = ReforgedBingo.getInstance().getPlayerManager().getPlayer(player);

if (sender == null) {
return;
}

public void run(@Sender ForgeEnvyPlayer sender, String[] args) {
BingoCardUI.open(sender);
BingoAttribute attribute = sender.getAttributeNow(BingoAttribute.class);

sender.message(UtilChatColour.colour(
ReforgedBingo.getInstance().getLocale().getRemainingTimeMessage()
.replace("%hours%", attribute.getTimeRemaining() + "")));
var attribute = sender.getAttributeNow(BingoAttribute.class);

sender.message(ReforgedBingo.getLocale().getRemainingTimeMessage().replace("%hours%", attribute.getTimeRemaining() + ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import com.envyful.api.command.annotate.executor.Completable;
import com.envyful.api.command.annotate.executor.Sender;
import com.envyful.api.command.annotate.permission.Permissible;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.api.forge.command.completion.player.PlayerTabCompleter;
import com.envyful.api.player.EnvyPlayer;
import com.envyful.reforged.bingo.forge.ReforgedBingo;
import com.envyful.api.forge.player.ForgeEnvyPlayer;
import com.envyful.api.platform.Messageable;
import com.envyful.reforged.bingo.forge.player.BingoAttribute;
import net.minecraft.commands.CommandSource;
import net.minecraft.server.level.ServerPlayer;

@Command(
value = "completeslot"
Expand All @@ -21,27 +18,21 @@
public class CompleteSlotCommand {

@CommandProcessor
public void onCommand(@Sender CommandSource sender,
@Completable(PlayerTabCompleter.class) @Argument ServerPlayer target,
public void onCommand(@Sender Messageable<?> sender,
@Completable(PlayerTabCompleter.class) @Argument ForgeEnvyPlayer target,
@Argument int slotX,
@Argument int slotY) {
EnvyPlayer<ServerPlayer> targetPlayer = ReforgedBingo.getInstance().getPlayerManager().getPlayer(target);

if (targetPlayer == null) {
return;
}

BingoAttribute attribute = targetPlayer.getAttributeNow(BingoAttribute.class);
var attribute = target.getAttributeNow(BingoAttribute.class);

if (attribute == null) {
return;
}

if (!attribute.completeSlot(slotY, slotX)) {
sender.sendSystemMessage(UtilChatColour.colour("&c&l(!) &cInvalid slot numbers (start form 0)"));
sender.message("&c&l(!) &cInvalid slot numbers (start form 0)");
return;
}

sender.sendSystemMessage(UtilChatColour.colour("&c&l(!) &cCompleted slot at " + slotY + ", " + slotX + " for " + target.getName()));
sender.message("&c&l(!) &cCompleted slot at " + slotY + ", " + slotX + " for " + target.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import com.envyful.api.command.annotate.executor.Completable;
import com.envyful.api.command.annotate.executor.Sender;
import com.envyful.api.command.annotate.permission.Permissible;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.api.forge.command.completion.player.PlayerTabCompleter;
import com.envyful.api.player.EnvyPlayer;
import com.envyful.api.forge.player.ForgeEnvyPlayer;
import com.envyful.api.platform.Messageable;
import com.envyful.reforged.bingo.forge.ReforgedBingo;
import com.envyful.reforged.bingo.forge.player.BingoAttribute;
import net.minecraft.commands.CommandSource;
import net.minecraft.server.level.ServerPlayer;

@Command(
value = "reroll"
Expand All @@ -21,24 +19,16 @@
public class ReRollCommand {

@CommandProcessor
public void onCommand(@Sender CommandSource sender,
@Completable(PlayerTabCompleter.class) @Argument ServerPlayer target) {
EnvyPlayer<ServerPlayer> targetPlayer = ReforgedBingo.getInstance().getPlayerManager().getPlayer(target);

if (targetPlayer == null) {
return;
}

BingoAttribute attribute = targetPlayer.getAttributeNow(BingoAttribute.class);
public void onCommand(@Sender Messageable<?> sender,
@Completable(PlayerTabCompleter.class) @Argument ForgeEnvyPlayer target) {
var attribute = target.getAttributeNow(BingoAttribute.class);

if (attribute == null) {
return;
}

attribute.generateNewCard();
sender.sendSystemMessage(UtilChatColour.colour(
ReforgedBingo.getInstance().getLocale().getRerollCardMessage()
.replace("%player%", target.getName().getString())
));
sender.message(ReforgedBingo.getLocale().getRerollCardMessage()
.replace("%player%", target.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.envyful.api.command.annotate.executor.CommandProcessor;
import com.envyful.api.command.annotate.executor.Sender;
import com.envyful.api.command.annotate.permission.Permissible;
import com.envyful.api.forge.chat.UtilChatColour;
import com.envyful.api.platform.Messageable;
import com.envyful.reforged.bingo.forge.ReforgedBingo;
import net.minecraft.commands.CommandSource;

@Command(
value = "reload"
Expand All @@ -15,10 +14,8 @@
public class ReloadCommand {

@CommandProcessor
public void onCommand(@Sender CommandSource sender, String[] args) {
public void onCommand(@Sender Messageable<?> sender, String[] args) {
ReforgedBingo.getInstance().reloadConfig();
sender.sendSystemMessage(UtilChatColour.colour(
ReforgedBingo.getInstance().getLocale().getReloadMessage()
));
sender.message(ReforgedBingo.getLocale().getReloadMessage());
}
}
Loading

0 comments on commit 5075a20

Please sign in to comment.