Skip to content

Commit

Permalink
Dynamic isOnEMC check (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFrydae authored Jul 4, 2024
1 parent 8734053 commit 55b76c0
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 42 deletions.
23 changes: 22 additions & 1 deletion common/src/main/java/coffee/waffle/emcutils/Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package coffee.waffle.emcutils;

import coffee.waffle.emcutils.container.EmpireServer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -16,11 +18,30 @@
public class Util {
public static final String MODID = "emcutils";
public static final Logger LOG = LoggerFactory.getLogger(MODID);
public static boolean isOnEMC = false;
public static EmpireServer currentServer;
public static String onJoinCommand;
public static int playerGroupId = 0;

public static boolean isOnEMC() {
if (MinecraftClient.getInstance().isInSingleplayer()) {
return false;
}

ClientPlayNetworkHandler networkHandler = MinecraftClient.getInstance().getNetworkHandler();

if (networkHandler == null) {
return false;
}

if (networkHandler.getConnection() == null) {
return false;
}

String address = networkHandler.getConnection().getAddressAsString(true);

return address.contains("emc.gs") || address.contains("empire.us") || address.contains("empireminecraft.com");
}

public static void setCurrentServer(String name) {
if (name.equalsIgnoreCase("utop")) name = "utopia";
for (EmpireServer server : EmpireServer.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ChatChannels {
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;

public static void handleChatScreenRender(Screen screen, DrawContext context) {
if (Util.isOnEMC && Config.chatButtonsEnabled()) {
if (Util.isOnEMC() && Config.chatButtonsEnabled()) {
for (ChatChannel channel : ChatChannel.values()) {
if (channel == ChatChannel.SUPPORTER && Util.playerGroupId < 2) break;
if (channel == ChatChannel.MODERATOR && Util.playerGroupId < 5) break;
Expand All @@ -36,7 +36,7 @@ public static void handleChatScreenRender(Screen screen, DrawContext context) {
}

public static void handleChatScreenMouseClicked(Screen screen, double mouseX, double mouseY) {
if (Util.isOnEMC && Config.chatButtonsEnabled()) {
if (Util.isOnEMC() && Config.chatButtonsEnabled()) {
for (ChatChannel channel : ChatChannel.values()) {
if (channel == ChatChannel.SUPPORTER && Util.playerGroupId < 2) break;
if (channel == ChatChannel.MODERATOR && Util.playerGroupId < 5) break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static List<PlayerListEntry> sortPlayers(List<PlayerListEntry> original)
List<EnhancedTabListEntry> enhanced = Lists.newArrayList();
List<EnhancedTabListEntry> currentServer = Lists.newArrayList();

if (!Util.isOnEMC) {
if (!Util.isOnEMC()) {
return original;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import coffee.waffle.emcutils.Util;
import coffee.waffle.emcutils.event.TooltipCallback;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.minecraft.component.DataComponentTypes;
Expand All @@ -16,7 +15,7 @@
public class UsableItems {
public static void init() {
TooltipCallback.ITEM.register((itemStack, list, tooltipContext, type) -> {
if (!Util.isOnEMC || !isUsableItemWithCooldown(itemStack)) return;
if (!Util.isOnEMC() || !isUsableItemWithCooldown(itemStack)) return;

for (Text text : list) {
if (text.getString().startsWith("Usable in: ") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class ClientPlayNetworkHandlerMixin {

@Inject(at = @At("TAIL"), method = "onGameJoin")
void emcutils$onJoinEMC(GameJoinS2CPacket packet, CallbackInfo info) {
if (Util.isOnEMC && !emcutils$online) {
if (Util.isOnEMC() && !emcutils$online) {
ChatListener.init();
CommandListener.init();
UsableItems.init();
Expand All @@ -52,7 +52,7 @@ abstract class ClientPlayNetworkHandlerMixin {

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreens;open(Lnet/minecraft/screen/ScreenHandlerType;Lnet/minecraft/client/MinecraftClient;ILnet/minecraft/text/Text;)V"), method = "onOpenScreen", cancellable = true)
void emcutils$changeToVaultScreen(OpenScreenS2CPacket packet, CallbackInfo ci) {
if (Util.isOnEMC && packet.getName().getString().startsWith("Page: ") && packet.getScreenHandlerType() == ScreenHandlerType.GENERIC_9X6) {
if (Util.isOnEMC() && packet.getName().getString().startsWith("Page: ") && packet.getScreenHandlerType() == ScreenHandlerType.GENERIC_9X6) {
var pageTitle = packet.getName().copy();
if (pageTitle.getString().split(" ")[1].contains("69")) {
pageTitle = pageTitle.append(Text.of(" ... nice"));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class PlayerEntityMixin {
private void emcutils$getDisplayName(CallbackInfoReturnable<Text> cir) {
if (MinecraftClient.getInstance().isInSingleplayer()) return; // Don't run anything for singleplayer

if (Util.isOnEMC) {
if (Util.isOnEMC()) {
PlayerEntity e = ((PlayerEntity) (Object) this);

try {
Expand Down
5 changes: 2 additions & 3 deletions common/src/main/resources/emcutils-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"client": [
"ChatScreenMixin",
"ClientPlayNetworkHandlerMixin",
"ConnectScreenMixin",
"IdentifierMixin",
"PlayerListHudMixin",
"PlayerEntityMixin"
"PlayerEntityMixin",
"PlayerListHudMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
org.gradle.jvmargs=-Xmx4G
org.gradle.parallel=true

mod_version=9.0.1
mod_version=9.0.2
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void initialize(@NotNull final IClientAPI api) {
LOG.info(MODID + " found JourneyMap - enabling integrations");

ClientEventRegistry.MAPPING_EVENT.subscribe(MODID, event -> {
if (event.getStage() == MappingEvent.Stage.MAPPING_STARTED && Util.isOnEMC) {
if (event.getStage() == MappingEvent.Stage.MAPPING_STARTED && Util.isOnEMC()) {
// Disable cave maps on EMC
assert client.world != null;
if (!client.world.getRegistryKey().getValue().getPath().contains("nether")) {
Expand All @@ -42,12 +42,12 @@ public void initialize(@NotNull final IClientAPI api) {

// Turn off radars on EMC
ClientEventRegistry.ENTITY_RADAR_UPDATE_EVENT.subscribe(MODID, event -> {
if (Util.isOnEMC) event.cancel();
if (Util.isOnEMC()) event.cancel();
});

// Add residence TP button
ClientEventRegistry.FULLSCREEN_POPUP_MENU_EVENT.subscribe(MODID, event -> {
if (Util.isOnEMC) event.getPopupMenu().addMenuItem("Teleport to Residence", new TeleportToResidenceAction());
if (Util.isOnEMC()) event.getPopupMenu().addMenuItem("Teleport to Residence", new TeleportToResidenceAction());
});
}

Expand All @@ -59,7 +59,7 @@ public String getModId() {
private static class TeleportToResidenceAction implements ModPopupMenu.Action {
@Override
public void doAction(final @NotNull BlockPos pos) {
if (Util.isOnEMC) {
if (Util.isOnEMC()) {
EmpireResidence res = Util.currentServer.getResidenceByLoc(new Vec3d(pos.getX(), pos.getY(), pos.getZ()));
if (res != null) MinecraftClient.getInstance().player.networkHandler.sendCommand(res.visitCommand);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ abstract class GuiMapNameMixin {
public void emcutils$xaero$setSubworldName(CallbackInfo ci) {
var server = Util.currentServer.name.toLowerCase();
var world = MinecraftClient.getInstance().world.getRegistryKey().getValue().getPath();
if (Util.isOnEMC) this.currentNameFieldContent = String.format("%s - %s", server, world);
if (Util.isOnEMC()) this.currentNameFieldContent = String.format("%s - %s", server, world);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
abstract class MapTeleporterMixin {
@Inject(method = "teleport", at = @At(value = "INVOKE_ASSIGN", target = "Lxaero/map/world/MapWorld;getTeleportCommandFormat()Ljava/lang/String;", remap = false), cancellable = true, remap = false)
private void emcutils$xaero$enableMapTeleportation(Screen screen, MapWorld mapWorld, int x, int y, int z, RegistryKey<World> d, CallbackInfo ci) {
if (Util.isOnEMC) {
if (Util.isOnEMC()) {
EmpireResidence res = Util.currentServer.getResidenceByLoc(new Vec3d(x, y, z));

if (res != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
abstract class ModSettingsMixin {
@Inject(method = "caveMapsDisabled", at = @At("HEAD"), cancellable = true, remap = false)
private void emcutils$xaero$disableCaveMaps(CallbackInfoReturnable<Boolean> cir) {
if (Util.isOnEMC) cir.setReturnValue(true);
if (Util.isOnEMC()) cir.setReturnValue(true);
}

@Inject(method = "getEntityRadar", at = @At("HEAD"), cancellable = true, remap = false)
private void emcutils$xaero$disableRadar(CallbackInfoReturnable<Boolean> cir) {
if (Util.isOnEMC) cir.setReturnValue(false);
if (Util.isOnEMC()) cir.setReturnValue(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class WaypointsManagerMixin {
@Inject(method = "teleportToWaypoint(Lxaero/common/minimap/waypoints/Waypoint;Lxaero/common/minimap/waypoints/WaypointWorld;Lnet/minecraft/client/gui/screen/Screen;Z)V", at = @At("HEAD"), cancellable = true)
public void emcutils$xaero$teleportToResidence(Waypoint w, WaypointWorld world, Screen screen, boolean respectHiddenCoords, CallbackInfo ci) {
if (world != null) {
if (Util.isOnEMC) {
if (Util.isOnEMC()) {
EmpireResidence res = Util.currentServer.getResidenceByLoc(new Vec3d(w.getX(), 64, w.getZ()));

if (res != null) {
Expand Down

0 comments on commit 55b76c0

Please sign in to comment.