Skip to content

Commit

Permalink
Refactored the way how the mod works with gamemodes check
Browse files Browse the repository at this point in the history
  • Loading branch information
remmintan committed Dec 7, 2024
1 parent 4906725 commit 9f05534
Show file tree
Hide file tree
Showing 42 changed files with 178 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.remmintan.mods.minefortress.core

import com.chocohead.mm.api.ClassTinkerers
import net.minecraft.client.MinecraftClient
import net.minecraft.client.network.ClientPlayerEntity
import net.minecraft.entity.LivingEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.world.GameMode

val FORTRESS: GameMode = ClassTinkerers.getEnum(GameMode::class.java, "FORTRESS")

fun isFortressGamemode(player: PlayerEntity?): Boolean {
if (player is ServerPlayerEntity) {
return player.interactionManager.gameMode == FORTRESS
}
if (player is ClientPlayerEntity) {
return isClientInFortressGamemode()
}
return false
}

fun isFortressGamemode(livingEntity: LivingEntity?): Boolean {
if (livingEntity is PlayerEntity) {
return isFortressGamemode(livingEntity)
}
return false
}

fun isClientInFortressGamemode(): Boolean {
val interactionManager = MinecraftClient.getInstance().interactionManager
return interactionManager != null && interactionManager.currentGameMode == FORTRESS
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.text.Text
import net.minecraft.util.Identifier

class BuildingScreen(handler: BuildingScreenHandler, playerInventory: PlayerInventory, title: Text) :
HandledScreen<BuildingScreenHandler>(handler, playerInventory, title) {



override fun drawBackground(context: DrawContext?, delta: Float, mouseX: Int, mouseY: Int) {

}

companion object {
val TEXTURE = Identifier("textures/gui/container/creative_inventory/tabs.png")
const val TAB_TEXTURE_PREFIX: String = "textures/gui/container/creative_inventory/tab_"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ package net.remmintan.mods.minefortress.gui

import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.screen.ArrayPropertyDelegate
import net.minecraft.screen.PropertyDelegate
import net.minecraft.screen.ScreenHandler

class BuildingScreenHandler(syncId: Int) : ScreenHandler(BUILDING_SCREEN_HANDLER_TYPE, syncId) {
class BuildingScreenHandler(
syncId: Int,
propertyDelegate: PropertyDelegate = ArrayPropertyDelegate(3)
) : ScreenHandler(BUILDING_SCREEN_HANDLER_TYPE, syncId) {

init {
addProperties(propertyDelegate)
}

override fun quickMove(player: PlayerEntity?, slot: Int): ItemStack {
TODO("Not implemented")
return ItemStack.EMPTY
}

override fun canUse(player: PlayerEntity?): Boolean = true
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/minefortress/MineFortressMod.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.minefortress;


import com.chocohead.mm.api.ClassTinkerers;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.Identifier;
import net.minecraft.world.GameMode;
import net.remmintan.mods.minefortress.blocks.FortressBlocks;
import net.remmintan.mods.minefortress.networking.registries.ServerNetworkReceivers;
import org.minefortress.commands.CommandsManager;
Expand All @@ -22,7 +20,6 @@

public class MineFortressMod implements ModInitializer {

public static final GameMode FORTRESS = ClassTinkerers.getEnum(GameMode.class, "FORTRESS");
public static final String BLUEPRINTS_FOLDER_NAME = "minefortress-blueprints";
public static final String BLUEPRINTS_EXTENSION = ".zip";
public static final String MOD_ID = "minefortress";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.Box;
import net.minecraft.world.GameMode;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.FortressState;
import net.remmintan.mods.minefortress.core.interfaces.entities.pawns.IWarrior;
import net.remmintan.mods.minefortress.core.utils.CoreModUtils;
Expand All @@ -25,7 +26,6 @@
import org.joml.AxisAngle4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.minefortress.MineFortressMod;
import org.minefortress.entity.BasePawnEntity;
import org.minefortress.entity.renderer.models.PawnModel;

Expand Down Expand Up @@ -84,7 +84,7 @@ public void render(BasePawnEntity pawn, float f, float g, MatrixStack matrixStac
.map(ClientPlayerInteractionManager::getCurrentGameMode)
.orElse(GameMode.DEFAULT);

if(currentGamemode == MineFortressMod.FORTRESS) {
if (FortressGamemodeUtilsKt.isClientInFortressGamemode()) {
final boolean hovering = client.crosshairTarget instanceof EntityHitResult entityHitResult && entityHitResult.getEntity() == pawn;
final var fightSelecting = isThisPawnSelected(pawn);
var color = getHealthFoodLevelColor(pawn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import net.minecraft.recipe.RecipeType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.interfaces.client.IClientManagersProvider;
import org.minefortress.fortress.resources.gui.AbstractFortressRecipeScreen;
import org.minefortress.fortress.resources.gui.FortressRecipeBookWidget;
import org.minefortress.interfaces.IFortressMinecraftClient;

public class FortressCraftingScreen extends AbstractFortressRecipeScreen<FortressCraftingScreenHandler> {
private static final Identifier TEXTURE = new Identifier("textures/gui/container/crafting_table.png");
Expand All @@ -36,15 +36,11 @@ public RecipeBookWidget getRecipeBookWidget() {
protected boolean professionRequirementSatisfied() {
final var fortressClient = clientManagersProvider();
final var clientManager = fortressClient.get_ClientFortressManager();
return fortressMinecraftClient().is_FortressGamemode() && clientManager.getProfessionManager().hasProfession("crafter");
return FortressGamemodeUtilsKt.isClientInFortressGamemode() && clientManager.getProfessionManager().hasProfession("crafter");
}

private IClientManagersProvider clientManagersProvider() {
return (IClientManagersProvider) MinecraftClient.getInstance();
}

private IFortressMinecraftClient fortressMinecraftClient() {
return (IFortressMinecraftClient) MinecraftClient.getInstance();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.ScreenType;
import net.remmintan.mods.minefortress.core.interfaces.client.IClientManagersProvider;
import net.remmintan.mods.minefortress.networking.c2s.ServerboundOpenCraftingScreenPacket;
Expand Down Expand Up @@ -106,7 +107,7 @@ public RecipeBookWidget getRecipeBookWidget() {
protected boolean professionRequirementSatisfied() {
final var provider = getManagerProvider();
final var clientManager = provider.get_ClientFortressManager();
return getFortressClient().is_FortressGamemode() && clientManager.getProfessionManager().hasProfession("blacksmith");
return FortressGamemodeUtilsKt.isClientInFortressGamemode() && clientManager.getProfessionManager().hasProfession("blacksmith");
}

private IClientManagersProvider getManagerProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package org.minefortress.interfaces;

import net.minecraft.client.MinecraftClient;
import net.remmintan.mods.minefortress.core.interfaces.client.IHoveredBlockProvider;
import net.remmintan.mods.minefortress.core.interfaces.professions.IHireInfo;
import net.remmintan.panama.renderer.BlueprintRenderer;
import net.remmintan.panama.renderer.SelectionRenderer;
import net.remmintan.panama.renderer.TasksRenderer;
import org.minefortress.renderer.gui.hud.FortressHud;

import java.util.Map;

public interface IFortressMinecraftClient extends IHoveredBlockProvider {

void open_HireScreen(MinecraftClient client, String screenName1, Map<String, IHireInfo> professions1);
FortressHud get_FortressHud();
BlueprintRenderer get_BlueprintRenderer();
SelectionRenderer get_SelectionRenderer();
TasksRenderer get_TasksRenderer();
boolean is_FortressGamemode();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.thread.ReentrantThreadExecutor;
import net.remmintan.gobi.SelectionManager;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.FortressState;
import net.remmintan.mods.minefortress.core.interfaces.blueprints.IBlockDataProvider;
import net.remmintan.mods.minefortress.core.interfaces.blueprints.world.BlueprintsDimensionUtilsKt;
Expand All @@ -37,7 +38,6 @@
import net.remmintan.panama.renderer.SelectionRenderer;
import net.remmintan.panama.renderer.TasksRenderer;
import org.jetbrains.annotations.Nullable;
import org.minefortress.MineFortressMod;
import org.minefortress.blueprints.manager.ClientBlueprintManager;
import org.minefortress.fight.ClientPawnsSelectionManager;
import org.minefortress.fortress.ClientFortressManager;
Expand Down Expand Up @@ -182,14 +182,9 @@ public ISelectionManager get_SelectionManager() {
return selectionManager;
}

@Override
public boolean is_FortressGamemode() {
return this.interactionManager != null && this.interactionManager.getCurrentGameMode() == MineFortressMod.FORTRESS;
}

@Inject(method = "handleInputEvents", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/tutorial/TutorialManager;onInventoryOpened()V", shift = At.Shift.BEFORE))
private void handleInputEvents(CallbackInfo ci) {
if(this.interactionManager != null && this.interactionManager.getCurrentGameMode() == MineFortressMod.FORTRESS) {
if (FortressGamemodeUtilsKt.isClientInFortressGamemode()) {
if(this.options.sprintKey.isPressed()) {
if(this.get_BlueprintManager().isSelecting()) {
this.get_BlueprintManager().rotateSelectedStructureClockwise();
Expand All @@ -202,7 +197,7 @@ private void handleInputEvents(CallbackInfo ci) {

@Inject(method = "setScreen", at = @At("HEAD"), cancellable = true)
public void setScreenMix(Screen screen, CallbackInfo ci) {
if(is_FortressGamemode()) {
if (FortressGamemodeUtilsKt.isClientInFortressGamemode()) {
if(this.options.sprintKey.isPressed() && screen instanceof InventoryScreen) {
ci.cancel();
}
Expand All @@ -211,7 +206,7 @@ public void setScreenMix(Screen screen, CallbackInfo ci) {

@Inject(method="doItemPick", at=@At("HEAD"), cancellable = true)
public void doItemPick(CallbackInfo ci) {
if(this.is_FortressGamemode()) {
if (FortressGamemodeUtilsKt.isClientInFortressGamemode()) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.world.World;
import org.minefortress.utils.ModUtils;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -31,7 +31,7 @@ protected FortressAbstractBlockState(Block owner, ImmutableMap<Property<?>, Comp

@Inject(method = "onUse", at = @At("HEAD"), cancellable = true)
void onUse(World world, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) {
if(ModUtils.isFortressGamemode(player) && this.getBlock() instanceof BlockEntityProvider) {
if (FortressGamemodeUtilsKt.isFortressGamemode(player) && this.getBlock() instanceof BlockEntityProvider) {
cir.setReturnValue(ActionResult.PASS);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.interfaces.blueprints.world.BlueprintsDimensionUtilsKt;
import net.remmintan.mods.minefortress.core.interfaces.professions.IServerProfessionsManager;
import net.remmintan.mods.minefortress.core.interfaces.server.IFortressServer;
import org.minefortress.MineFortressMod;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand Down Expand Up @@ -40,7 +40,7 @@ void tickReturn(CallbackInfo ci) {

if(!this.getWorld().isClient) {
final var closestPlayer = this.getWorld().getClosestPlayer(this, 100.0D);
if(closestPlayer instanceof ServerPlayerEntity srvP && srvP.interactionManager.getGameMode() == MineFortressMod.FORTRESS) {
if (closestPlayer != null && FortressGamemodeUtilsKt.isFortressGamemode(closestPlayer)) {
final var fortressServer = (IFortressServer) closestPlayer.getServer();
if(fortressServer != null) {
final var modServerManager = fortressServer.get_FortressModServerManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.interfaces.blueprints.IClientBlueprintManager;
import net.remmintan.mods.minefortress.core.utils.CoreModUtils;
import org.minefortress.interfaces.IFortressMinecraftClient;
import org.minefortress.renderer.CameraTools;
import org.minefortress.utils.ModUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -39,7 +39,7 @@ public FortressClientPlayerEntityMixin(ClientWorld world, GameProfile profile) {
@Override
public HitResult raycast(double maxDistance, float tickDelta, boolean includeFluids) {
final IFortressMinecraftClient fortressClient = (IFortressMinecraftClient) this.client;
if(!fortressClient.is_FortressGamemode() || this.client.options.pickItemKey.isPressed()){
if (!FortressGamemodeUtilsKt.isClientInFortressGamemode() || this.client.options.pickItemKey.isPressed()) {
return super.raycast(maxDistance, tickDelta, includeFluids);
}

Expand All @@ -58,7 +58,7 @@ public HitResult raycast(double maxDistance, float tickDelta, boolean includeFlu

@Inject(method = "dropSelectedItem", at = @At("HEAD"), cancellable = true)
public void dropSelectedItem(boolean entireStack, CallbackInfoReturnable<Boolean> cir) {
if(ModUtils.isClientInFortressGamemode()) {
if (FortressGamemodeUtilsKt.isClientInFortressGamemode()) {
if(client.options.sprintKey.isPressed()) {
final var fortressClient = CoreModUtils.getMineFortressManagersProvider();
final IClientBlueprintManager clientBlueprintManager = fortressClient.get_BlueprintManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.client.render.entity.PlayerEntityRenderer;
import net.minecraft.client.render.entity.model.PlayerEntityModel;
import net.minecraft.client.util.math.MatrixStack;
import org.minefortress.utils.ModUtils;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -24,7 +24,7 @@ public FortressPlayerEntityRendererMixin(EntityRendererFactory.Context ctx, Play
method = "render(Lnet/minecraft/client/network/AbstractClientPlayerEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/entity/PlayerEntityRenderer;setModelPose(Lnet/minecraft/client/network/AbstractClientPlayerEntity;)V"))
public void render(AbstractClientPlayerEntity abstractClientPlayerEntity, float f, float g, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, CallbackInfo ci) {
if(ModUtils.isFortressGamemode(abstractClientPlayerEntity))
if (FortressGamemodeUtilsKt.isClientInFortressGamemode())
this.getModel().setVisible(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import net.minecraft.world.TeleportTarget;
import net.minecraft.world.World;
import net.remmintan.mods.minefortress.blueprints.BlueprintsDimensionKt;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import net.remmintan.mods.minefortress.core.interfaces.blueprints.world.BlueprintsDimensionUtilsKt;
import net.remmintan.mods.minefortress.core.interfaces.entities.player.FortressServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
import org.minefortress.MineFortressMod;
import org.minefortress.utils.FortressSpawnLocating;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -31,9 +32,13 @@ public abstract class FortressServerPlayerEntityMixin extends PlayerEntity imple

@Shadow @Final public MinecraftServer server;
@Shadow public ServerPlayNetworkHandler networkHandler;
@Unique
private Vec3d persistedPos;
@Unique
private Vec3d persistedVelocity;
@Unique
private float persistedYaw;
@Unique
private float persistedPitch;


Expand Down Expand Up @@ -101,7 +106,7 @@ public void getTeleportTarget(ServerWorld destination, CallbackInfoReturnable<Te
@Redirect(method = "moveToSpawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/SpawnLocating;findOverworldSpawn(Lnet/minecraft/server/world/ServerWorld;II)Lnet/minecraft/util/math/BlockPos;"))
public BlockPos moveToSpawnFindOverworldSpawn(ServerWorld world, int x, int z) {
final BlockPos actualSpawn = FortressSpawnLocating.findOverworldSpawn(world, x, z);
if(actualSpawn != null && this.server.getDefaultGameMode() == MineFortressMod.FORTRESS){
if (actualSpawn != null && this.server.getDefaultGameMode() == FortressGamemodeUtilsKt.getFORTRESS()) {
return actualSpawn.up(20);
} else {
return actualSpawn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import org.minefortress.utils.ModUtils;
import net.remmintan.mods.minefortress.core.FortressGamemodeUtilsKt;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -12,7 +12,7 @@ public class PlayerManagerMixin {

@Redirect(method = "respawnPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;refreshPositionAndAngles(DDDFF)V"))
public void respawnPlayer(ServerPlayerEntity instance, double x, double y, double z, float yaw, float pitch) {
if(ModUtils.isFortressGamemode(instance))
if (FortressGamemodeUtilsKt.isFortressGamemode(instance))
instance.refreshPositionAndAngles(x, y, z, 45, 60);
}
}
Loading

0 comments on commit 9f05534

Please sign in to comment.