Skip to content

Commit

Permalink
Merge pull request #9 from Gaming32/no-arch-api
Browse files Browse the repository at this point in the history
No Arch API
  • Loading branch information
Gaming32 authored Apr 19, 2024
2 parents 1ff338d + 64d2bb3 commit 27637ad
Show file tree
Hide file tree
Showing 52 changed files with 1,075 additions and 356 deletions.
2 changes: 0 additions & 2 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ dependencies {
// We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies
// Do NOT use other classes from fabric loader
modImplementation("net.fabricmc:fabric-loader:${rootProject["fabric_loader_version"]}")
// Remove the next line if you don't want to depend on the API
modApi("dev.architectury:architectury:${rootProject["architectury_version"]}")

implementation("com.electronwill.night-config:toml:3.6.0")

Expand Down
63 changes: 20 additions & 43 deletions common/src/main/java/io/github/gaming32/bingo/Bingo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

import com.demonwav.mcdev.annotations.Translatable;
import com.mojang.logging.LogUtils;
import dev.architectury.event.CompoundEventResult;
import dev.architectury.event.EventResult;
import dev.architectury.event.events.common.CommandRegistrationEvent;
import dev.architectury.event.events.common.ExplosionEvent;
import dev.architectury.event.events.common.InteractionEvent;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.PlayerEvent;
import dev.architectury.event.events.common.TickEvent;
import dev.architectury.platform.Platform;
import dev.architectury.registry.ReloadListenerRegistry;
import dev.architectury.registry.registries.RegistrarManager;
import dev.architectury.utils.Env;
import io.github.gaming32.bingo.client.BingoClient;
import io.github.gaming32.bingo.conditions.BingoConditions;
import io.github.gaming32.bingo.conditions.BingoParamSets;
Expand All @@ -33,6 +21,8 @@
import io.github.gaming32.bingo.network.messages.s2c.SyncTeamPacket;
import io.github.gaming32.bingo.network.messages.s2c.UpdateProgressPacket;
import io.github.gaming32.bingo.network.messages.s2c.UpdateStatePacket;
import io.github.gaming32.bingo.platform.BingoPlatform;
import io.github.gaming32.bingo.platform.event.Event;
import io.github.gaming32.bingo.triggers.BingoTriggers;
import io.github.gaming32.bingo.util.BingoUtil;
import net.minecraft.locale.Language;
Expand All @@ -47,7 +37,6 @@
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.level.storage.LevelResource;
import org.slf4j.Logger;
Expand All @@ -64,8 +53,6 @@ public class Bingo {
public static final String MOD_ID = "bingo";
public static final Logger LOGGER = LogUtils.getLogger();

public static final RegistrarManager REGISTRAR_MANAGER = RegistrarManager.get(MOD_ID);

public static final LevelResource PERSISTED_BINGO_GAME = new LevelResource("persisted_bingo_game.dat");

public static boolean showOtherTeam;
Expand All @@ -74,26 +61,25 @@ public class Bingo {
public static final Set<UUID> needAdvancementsClear = new HashSet<>();

public static void init() {
CommandRegistrationEvent.EVENT.register(BingoCommand::register);
Event.REGISTER_COMMANDS.register(BingoCommand::register);

PlayerEvent.PLAYER_JOIN.register(player -> {
Event.PLAYER_JOIN.register(player -> {
if (activeGame != null) {
activeGame.addPlayer(player);
}
});

PlayerEvent.PLAYER_QUIT.register(player -> needAdvancementsClear.remove(player.getUUID()));
Event.PLAYER_QUIT.register(player -> needAdvancementsClear.remove(player.getUUID()));

LifecycleEvent.SERVER_STOPPED.register(instance -> activeGame = null);
Event.SERVER_STOPPED.register(instance -> activeGame = null);

InteractionEvent.RIGHT_CLICK_ITEM.register((player, hand) -> {
Event.RIGHT_CLICK_ITEM.register((player, hand) -> {
if (player instanceof ServerPlayer serverPlayer) {
BingoTriggers.TRY_USE_ITEM.get().trigger(serverPlayer, hand);
}
return CompoundEventResult.pass();
});

ExplosionEvent.PRE.register((level, explosion) -> {
Event.EXPLOSION_START.register((level, explosion) -> {
if (level instanceof ServerLevel serverLevel) {
final ServerPlayer player;
if (explosion.getIndirectSourceEntity() instanceof ServerPlayer thePlayer) {
Expand All @@ -107,10 +93,9 @@ public static void init() {
BingoTriggers.EXPLOSION.get().trigger(player, serverLevel, explosion);
}
}
return EventResult.pass();
});

TickEvent.SERVER_POST.register(instance -> {
Event.SERVER_TICK_END.register(instance -> {
if (activeGame != null && activeGame.isRequireClient()) {
for (final ServerPlayer player : instance.getPlayerList().getPlayers()) {
if (player.tickCount == 60 && !Bingo.isInstalledOnClient(player)) {
Expand All @@ -120,7 +105,7 @@ public static void init() {
}
});

LifecycleEvent.SERVER_STARTED.register(instance -> {
Event.SERVER_STARTED.register(instance -> {
final Path path = instance.getWorldPath(PERSISTED_BINGO_GAME);
if (!Files.isRegularFile(path)) return;
LOGGER.info("Reading persisted Bingo game");
Expand All @@ -134,7 +119,7 @@ public static void init() {
}
});

LifecycleEvent.SERVER_STOPPING.register(instance -> {
Event.SERVER_STOPPING.register(instance -> {
if (activeGame == null || !activeGame.isPersistent()) return;
LOGGER.info("Storing persistent Bingo game");
final Path path = instance.getWorldPath(PERSISTED_BINGO_GAME);
Expand All @@ -154,22 +139,14 @@ public static void init() {
ProgressTrackerType.load();
BingoTriggers.load();

ReloadListenerRegistry.register(
PackType.SERVER_DATA,
new BingoTag.ReloadListener(),
BingoTag.ReloadListener.ID
);
ReloadListenerRegistry.register(
PackType.SERVER_DATA,
new BingoDifficulty.ReloadListener(),
BingoDifficulty.ReloadListener.ID
);
ReloadListenerRegistry.register(
PackType.SERVER_DATA,
new BingoGoal.ReloadListener(),
BingoGoal.ReloadListener.ID,
List.of(BingoTag.ReloadListener.ID, BingoDifficulty.ReloadListener.ID)
);
BingoPlatform.platform.registerDataReloadListeners(registrar -> {
registrar.register(BingoTag.ReloadListener.ID, new BingoTag.ReloadListener());
registrar.register(BingoDifficulty.ReloadListener.ID, new BingoDifficulty.ReloadListener());
registrar.register(
BingoGoal.ReloadListener.ID, new BingoGoal.ReloadListener(),
List.of(BingoTag.ReloadListener.ID, BingoDifficulty.ReloadListener.ID)
);
});

BingoNetworking.instance().onRegister(registrar -> {
registrar.register(PacketFlow.CLIENTBOUND, InitBoardPacket.ID, InitBoardPacket::new);
Expand All @@ -182,7 +159,7 @@ public static void init() {
registrar.register(PacketFlow.SERVERBOUND, KeyPressedPacket.ID, KeyPressedPacket::new);
});

if (Platform.getEnvironment() == Env.CLIENT) {
if (BingoPlatform.platform.isClient()) {
BingoClient.init();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package io.github.gaming32.bingo.client;

import com.mojang.blaze3d.platform.InputConstants;
import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientGuiEvent;
import dev.architectury.event.events.client.ClientPlayerEvent;
import dev.architectury.event.events.client.ClientScreenInputEvent;
import dev.architectury.event.events.client.ClientTickEvent;
import dev.architectury.platform.Platform;
import dev.architectury.registry.client.gui.ClientTooltipComponentRegistry;
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
import com.mojang.blaze3d.platform.Window;
import io.github.gaming32.bingo.Bingo;
import io.github.gaming32.bingo.client.config.BingoClientConfig;
import io.github.gaming32.bingo.client.config.BingoConfigScreen;
import io.github.gaming32.bingo.client.icons.DefaultIconRenderers;
import io.github.gaming32.bingo.client.icons.IconRenderer;
import io.github.gaming32.bingo.client.icons.IconRenderers;
Expand All @@ -21,6 +13,8 @@
import io.github.gaming32.bingo.game.BingoGameMode;
import io.github.gaming32.bingo.game.GoalProgress;
import io.github.gaming32.bingo.network.ClientGoal;
import io.github.gaming32.bingo.platform.BingoPlatform;
import io.github.gaming32.bingo.platform.event.ClientEvents;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -57,19 +51,15 @@ public class BingoClient {
public static ClientGame clientGame;

public static final BingoClientConfig CONFIG = new BingoClientConfig(
Platform.getConfigFolder().resolve("bingo-client.toml")
BingoPlatform.platform.getConfigDir().resolve("bingo-client.toml")
);
private static RecipeViewerPlugin recipeViewerPlugin;

public static void init() {
CONFIG.load();
CONFIG.save();

if (!Platform.isFabric()) {
Platform.getMod(Bingo.MOD_ID).registerConfigurationScreen(BingoConfigScreen::new);
}

ClientGuiEvent.RENDER_HUD.register((graphics, tickDelta) -> {
ClientEvents.RENDER_HUD.register((graphics, tickDelta) -> {
if (clientGame == null) return;
final Minecraft minecraft = Minecraft.getInstance();
if (minecraft.getDebugOverlay().showDebugScreen() || minecraft.screen instanceof BoardScreen) return;
Expand Down Expand Up @@ -133,44 +123,46 @@ class TeamValue {
}
});

ClientScreenInputEvent.KEY_RELEASED_PRE.register((client, screen, keyCode, scanCode, modifiers) -> {
ClientEvents.KEY_RELEASED_PRE.register((screen, keyCode, scanCode, modifiers) -> {
if (clientGame == null || !(screen instanceof ChatScreen)) {
return EventResult.pass();
return false;
}
final Window window = Minecraft.getInstance().getWindow();
final float scale = CONFIG.getBoardScale();
final float x = CONFIG.getBoardCorner().getX(client.getWindow().getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(client.getWindow().getGuiScaledHeight(), scale);
return detectPress(keyCode, scanCode, x, y, scale) ? EventResult.interruptTrue() : EventResult.pass();
final float x = CONFIG.getBoardCorner().getX(window.getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(window.getGuiScaledHeight(), scale);
return detectPress(keyCode, scanCode, x, y, scale);
});

ClientScreenInputEvent.MOUSE_RELEASED_PRE.register((client, screen, mouseX, mouseY, button) -> {
ClientEvents.MOUSE_RELEASED_PRE.register((screen, mouseX, mouseY, button) -> {
if (clientGame == null || !(screen instanceof ChatScreen)) {
return EventResult.pass();
return false;
}
final Window window = Minecraft.getInstance().getWindow();
final float scale = CONFIG.getBoardScale();
final float x = CONFIG.getBoardCorner().getX(client.getWindow().getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(client.getWindow().getGuiScaledHeight(), scale);
return detectClick(button, x, y, scale) ? EventResult.interruptTrue() : EventResult.pass();
final float x = CONFIG.getBoardCorner().getX(window.getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(window.getGuiScaledHeight(), scale);
return detectClick(button, x, y, scale);
});

ClientPlayerEvent.CLIENT_PLAYER_QUIT.register(player -> {
ClientEvents.PLAYER_QUIT.register(player -> {
clientTeam = BingoBoard.Teams.NONE;
clientGame = null;
});

DefaultIconRenderers.setup();

final KeyMapping boardKey = new KeyMapping("bingo.key.board", InputConstants.KEY_B, "bingo.key.category");
KeyMappingRegistry.register(boardKey);
ClientTickEvent.CLIENT_PRE.register(instance -> {
BingoPlatform.platform.registerKeyMappings(registrar -> registrar.accept(boardKey));
ClientEvents.CLIENT_TICK_START.register(minecraft -> {
while (boardKey.consumeClick()) {
if (clientGame != null) {
instance.setScreen(new BoardScreen());
minecraft.setScreen(new BoardScreen());
}
}
});

ClientTooltipComponentRegistry.register(IconTooltip.class, ClientIconTooltip::new);
BingoPlatform.platform.registerClientTooltips(registrar -> registrar.register(IconTooltip.class, ClientIconTooltip::new));
}

public static RecipeViewerPlugin getRecipeViewerPlugin() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.gaming32.bingo.client.icons;

import dev.architectury.registry.registries.RegistrySupplier;
import io.github.gaming32.bingo.data.icons.GoalIcon;
import io.github.gaming32.bingo.data.icons.GoalIconType;
import io.github.gaming32.bingo.platform.registry.RegistryValue;
import net.minecraft.resources.ResourceLocation;

import java.util.HashMap;
Expand All @@ -16,27 +16,27 @@ private IconRenderers() {
}

public static <I extends GoalIcon> void register(GoalIconType<I> iconType, IconRenderer<I> renderer) {
final ResourceLocation id = GoalIconType.REGISTRAR.getId(iconType);
final ResourceLocation id = GoalIconType.REGISTRAR.registry().getKey(iconType);
if (id == null) {
throw new IllegalArgumentException("Tried to register renderer for unregistered icon type " + iconType);
}
RENDERERS.put(GoalIconType.REGISTRAR.getId(iconType), renderer);
RENDERERS.put(id, renderer);
}

public static <I extends GoalIcon> void register(RegistrySupplier<GoalIconType<I>> iconType, IconRenderer<I> renderer) {
RENDERERS.put(iconType.getId(), renderer);
public static <I extends GoalIcon> void register(RegistryValue<GoalIconType<I>> iconType, IconRenderer<I> renderer) {
RENDERERS.put(iconType.id(), renderer);
}

public static <I extends GoalIcon> IconRenderer<I> getRenderer(GoalIconType<I> iconType) {
final ResourceLocation id = GoalIconType.REGISTRAR.getId(iconType);
final ResourceLocation id = GoalIconType.REGISTRAR.registry().getKey(iconType);
if (id == null) {
throw new NoSuchElementException("Unknown id for icon type " + iconType);
}
return getRenderer(id);
}

public static <I extends GoalIcon> IconRenderer<I> getRenderer(RegistrySupplier<GoalIconType<I>> iconType) {
return getRenderer(iconType.getId());
public static <I extends GoalIcon> IconRenderer<I> getRenderer(RegistryValue<GoalIconType<I>> iconType) {
return getRenderer(iconType.id());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package io.github.gaming32.bingo.client.recipeviewer;

import com.mojang.blaze3d.platform.InputConstants;
import dev.architectury.platform.Platform;
import io.github.gaming32.bingo.client.recipeviewer.jei.JEIPlugin;
import io.github.gaming32.bingo.platform.BingoPlatform;
import net.minecraft.world.item.ItemStack;

public abstract class RecipeViewerPlugin {
public static RecipeViewerPlugin detect() {
if (Platform.isModLoaded("emi")) {
if (BingoPlatform.platform.isModLoaded("emi")) {
return new EMIPlugin();
}
if (Platform.isModLoaded("jei")) {
if (BingoPlatform.platform.isModLoaded("jei")) {
return new JEIPlugin();
}
if (Platform.isModLoaded("roughlyenoughitems")) {
if (BingoPlatform.platform.isModLoaded("roughlyenoughitems")) {
return new REIPlugin();
}
return new NoPlugin();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.gaming32.bingo.client.recipeviewer.jei;

import com.mojang.blaze3d.platform.InputConstants;
import dev.architectury.event.EventResult;
import dev.architectury.event.events.client.ClientRawInputEvent;
import io.github.gaming32.bingo.client.recipeviewer.RecipeViewerPlugin;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.recipe.IFocus;
Expand All @@ -17,50 +15,6 @@
import java.util.function.Function;

public class JEIPlugin extends RecipeViewerPlugin {
private int viewRecipe = 0;
private int viewUsages = 0;

public JEIPlugin() {
ClientRawInputEvent.KEY_PRESSED.register((client, keyCode, scanCode, action, modifiers) -> {
final InputConstants.Key key = InputConstants.getKey(keyCode, scanCode);
if (action == InputConstants.PRESS) {
if (matchesKey(key, IJeiKeyMappings::getShowRecipe)) {
viewRecipe++;
}
if (matchesKey(key, IJeiKeyMappings::getShowUses)) {
viewUsages++;
}
} else if (action == InputConstants.RELEASE) {
if (matchesKey(key, IJeiKeyMappings::getShowRecipe)) {
viewRecipe--;
}
if (matchesKey(key, IJeiKeyMappings::getShowUses)) {
viewUsages--;
}
}
return EventResult.pass();
});
ClientRawInputEvent.MOUSE_CLICKED_PRE.register((client, button, action, mods) -> {
final InputConstants.Key key = InputConstants.Type.MOUSE.getOrCreate(button);
if (action == InputConstants.PRESS) {
if (matchesKey(key, IJeiKeyMappings::getShowRecipe)) {
viewRecipe++;
}
if (matchesKey(key, IJeiKeyMappings::getShowUses)) {
viewUsages++;
}
} else if (action == InputConstants.RELEASE) {
if (matchesKey(key, IJeiKeyMappings::getShowRecipe)) {
viewRecipe--;
}
if (matchesKey(key, IJeiKeyMappings::getShowUses)) {
viewUsages--;
}
}
return EventResult.pass();
});
}

@Override
public boolean isViewRecipe(InputConstants.Key key) {
return matchesKey(key, IJeiKeyMappings::getShowRecipe);
Expand Down
Loading

0 comments on commit 27637ad

Please sign in to comment.