Skip to content

Commit

Permalink
Gracefully handle Local and Global sytem to work with Entity Awarenes…
Browse files Browse the repository at this point in the history
…s System...

I think
  • Loading branch information
Iteranya committed Nov 22, 2024
1 parent 5063b1f commit 3479179
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static void clientSideRenderDialogueScreen(VisualNovelEngine vnEngine) {
Minecraft.getInstance().execute(() -> {
try {
//Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,player));
Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,null));
Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,null,null));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/org/arsparadox/mobtalkerredux/MobTalkerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.arsparadox.mobtalkerredux.command.MobFreezer;
import org.arsparadox.mobtalkerredux.vn.controller.VisualNovelEngine;
import org.arsparadox.mobtalkerredux.vn.controller.vnmodules.PlayerInventoryHandler;
import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader;
Expand All @@ -37,18 +34,15 @@ public MobTalkerItem() {
// Check if the entity has a custom name
if (target.getCustomName() != null) {
String entityName = target.getCustomName().getString().toLowerCase().replace(" ", "_");
String entityType = target.getType().toString();
String entityType = target.getType().toShortString();
if (!world.isClientSide()) {
// Safely check and cast to ServerPlayer
if (player instanceof ServerPlayer serverPlayer) {
if(target instanceof Mob mob){
MobFreezer.freezeMob(mob);
}
}
} else { // Client-side: Open dialogue screen
Minecraft minecraft = Minecraft.getInstance();
minecraft.execute(() -> {
serverSideExecute(player, entityName+".json", entityName+".json",target);
sendClientMessage(player,"The Entity's type is: "+entityType);
VisualNovelEngine vn = serverSideExecute(player, entityType, entityName,target);
clientSideRenderDialogueScreen(vn,target,player);
});
}
return InteractionResult.SUCCESS;
Expand All @@ -60,7 +54,7 @@ public MobTalkerItem() {
return InteractionResult.PASS; // Return PASS if the entity doesn't have a custom name
}

private static void serverSideExecute(Player player, String entityType,String entityName, LivingEntity target) {
private static VisualNovelEngine serverSideExecute(Player player, String entityType,String entityName, LivingEntity target) {
//String uid = player.getName().toString();//literal{Dev}
String uid = player.getName().getString();//Dev
PlayerInventoryHandler inventory = new PlayerInventoryHandler(player);
Expand All @@ -81,8 +75,9 @@ private static void serverSideExecute(Player player, String entityType,String en
globalSave,
localSave
);
return vnEngine;
// sendClientMessage(player, "Trying to load the file mobtalkerredux/" + scriptFileName);
clientSideRenderDialogueScreen(vnEngine,target);
//clientSideRenderDialogueScreen(vnEngine,target,player);
}
else{
//sendClientMessage(player, "Failed to find the file mobtalkerredux/" + scriptFileName);
Expand All @@ -91,12 +86,13 @@ private static void serverSideExecute(Player player, String entityType,String en
//sendClientMessage(player, "Failed to find the file mobtalkerredux/" + scriptFileName);
throw new RuntimeException(e);
}
return null;
}

private static void clientSideRenderDialogueScreen(VisualNovelEngine vnEngine, LivingEntity target) {
private static void clientSideRenderDialogueScreen(VisualNovelEngine vnEngine, LivingEntity target,Player player) {
Minecraft.getInstance().execute(() -> {
try {
Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,target));
Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,target,player));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import net.minecraftforge.registries.RegistryObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.arsparadox.mobtalkerredux.vn.model.TextureLoader;

import java.util.stream.Collectors;

Expand Down Expand Up @@ -48,7 +47,7 @@ public MobTalkerRedux() {
private void setup(final FMLCommonSetupEvent event) {

LOGGER.info("HELLO FROM PREINIT");
TextureLoader.loadTexturesFromConfig();

}

private void enqueueIMC(final InterModEnqueueEvent event) {
Expand Down
100 changes: 7 additions & 93 deletions src/main/java/org/arsparadox/mobtalkerredux/command/MobFreezer.java
Original file line number Diff line number Diff line change
@@ -1,101 +1,15 @@
package org.arsparadox.mobtalkerredux.command;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.arsparadox.mobtalkerredux.vn.view.DialogueScreen;

public class MobFreezer {
private static final Map<UUID, Boolean> frozenMobs = new HashMap<>();
private static final UUID MOVEMENT_SPEED_MODIFIER = UUID.fromString("708396DC-7DEA-4EDD-B915-A3B97ADFF457");

/**
* Freezes a mob in place, disabling AI and movement
* @param mob The mob to freeze
* @return true if the mob was frozen, false if already frozen
*/
public static boolean freezeMob(Mob mob) {
if (mob == null || frozenMobs.containsKey(mob.getUUID())) {
return false;
}

// Store current AI state
frozenMobs.put(mob.getUUID(), mob.isNoAi());

// Disable AI
mob.setNoAi(true);

// Stop all current goals
// mob.goalSelector.removeAllGoals();
// mob.targetSelector.removeAllGoals();

// Freeze movement speed
mob.getAttribute(Attributes.MOVEMENT_SPEED).addTransientModifier(
new AttributeModifier(MOVEMENT_SPEED_MODIFIER, "Freeze movement", -1.0D, AttributeModifier.Operation.MULTIPLY_TOTAL)
);

// Stop any current movement/path
mob.getNavigation().stop();
mob.setDeltaMovement(0, mob.getDeltaMovement().y, 0); // Preserve Y movement for gravity

return true;
}

/**
* Freezes a mob by UUID if it exists in the world
* @param level The server level to search in
* @param uuid The UUID of the mob to freeze
* @return true if the mob was found and frozen
*/
public static boolean freezeMobByUUID(ServerLevel level, UUID uuid) {
if (level.getEntity(uuid) instanceof Mob mob) {
return freezeMob(mob);
}
return false;
}

/**
* Unfreezes a previously frozen mob
* @param mob The mob to unfreeze
* @return true if the mob was unfrozen, false if it wasn't frozen
*/
public static boolean unfreezeMob(Mob mob) {
if (mob == null || !frozenMobs.containsKey(mob.getUUID())) {
return false;
}

// Restore original AI state
mob.setNoAi(frozenMobs.remove(mob.getUUID()));

// Remove movement speed modifier
mob.getAttribute(Attributes.MOVEMENT_SPEED)
.removeModifier(MOVEMENT_SPEED_MODIFIER);

// The mob's AI goals will be reinstated automatically when needed
return true;
}

/**
* Unfreezes a mob by UUID if it exists in the world
* @param level The server level to search in
* @param uuid The UUID of the mob to unfreeze
* @return true if the mob was found and unfrozen
*/
public static boolean unfreezeMobByUUID(ServerLevel level, UUID uuid) {
if (level.getEntity(uuid) instanceof Mob mob) {
return unfreezeMob(mob);
}
return false;
public static void freezeMob(DialogueScreen ds) {
ds.mob.setNoAi(true);
ds.mob.setNoGravity(true);
}

/**
* Checks if a mob is currently frozen
*/
public static boolean isFrozen(UUID uuid) {
return frozenMobs.containsKey(uuid);
public static void unfreezeMob(DialogueScreen ds) {
ds.mob.setNoAi(false);
ds.mob.setNoGravity(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ private void processAction(Map<String, Object> action) {
}
break;
case "unlock_dialogues":
List<String> events = (List<String>) this.globalVariables.getOrDefault("unlocked_events", new ArrayList<>());
List<String> events = (List<String>) this.localVariables.getOrDefault("unlocked_events", new ArrayList<>());
events.addAll((List<String>) action.get("events"));
this.globalVariables.put("unlocked_events", events);
this.localVariables.put("unlocked_events", events);
this.currentState.incrementAndGet();
break;
case "play_sound":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void processIdleChat(
// Alright, Null Handling Time
// Fuck...
//System.out.println(vn.globalVariables.get("unlocked_events"));
List<String> chats = (List<String>) vn.globalVariables.getOrDefault("unlocked_events", new ArrayList<>());
List<String> chats = (List<String>) vn.localVariables.getOrDefault("unlocked_events", new ArrayList<>());
if (!chats.isEmpty()) {

Random random = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,26 @@ public static String getWorldName() {
*/
public static List<Map<String, Object>> loadScript(String name,String type, String playerUID) throws IOException {
// Try loading from the save folder (level or player UID folder)
name = name.toLowerCase();

name = name.toLowerCase()+".json";
type = type.toLowerCase()+".json";

// If not found in save or config, try loading from resources
System.out.println("Loading from resources: " + name);
List<Map<String, Object>> script = loadFromResource(name);
if(script!=null){
return loadFromResource(name);
return script;
}else{
return loadFromResource(type);
}

}

public static List<Map<String, Object>> loadSave(String fileName, String playerUID) {
public static List<Map<String, Object>> loadSave(String entityName, String playerUID) {
// Try loading from the save folder (level or player UID folder)
fileName = fileName.toLowerCase();
File saveFile = new File(getSaveFilePath(fileName, playerUID,getWorldName()));
entityName = entityName.toLowerCase()+".json";
File saveFile = new File(getSaveFilePath(entityName, playerUID,getWorldName()));
if (saveFile.exists()) {
System.out.println("Loading from save folder: " + fileName);
System.out.println("Loading from save folder: " + entityName);
return loadJsonFromFile(saveFile.getPath());
}
return null;
Expand All @@ -149,7 +149,7 @@ public static List<Map<String, Object>> loadGlobal(String playerUID) {
* @param playerName Player-specific UID for saving to a specific folder.
*/
public static void saveState(List<Map<String, Object>> gameState, String fileName, String playerName) {
fileName = fileName.toLowerCase();
fileName = fileName.toLowerCase()+".json";
playerName = playerName.toLowerCase();
String filePath = getSaveFilePath(fileName, playerName,getWorldName());
try (Writer writer = new FileWriter(filePath)) {
Expand Down

This file was deleted.

Loading

0 comments on commit 3479179

Please sign in to comment.