Skip to content

Commit

Permalink
Merge pull request #11 from Iteranya/1.20.1_Built_In_Script
Browse files Browse the repository at this point in the history
1.20.1 built in script
  • Loading branch information
Iteranya authored Nov 21, 2024
2 parents da75723 + 380c05d commit 5dbd6f6
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class DemoCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
Expand All @@ -39,8 +41,11 @@ private static void serverSideExecute(ServerPlayer player, String scriptFileName
String uid = player.getName().getString();
PlayerInventoryHandler inventory = new PlayerInventoryHandler(player);
boolean day = player.level().isDay();

try {
VisualNovelEngine vnEngine = new VisualNovelEngine(ScriptLoader.loadScript(scriptFileName,uid), scriptFileName, uid,day,inventory);
List<Map<String,Object>> script = ScriptLoader.loadScript(scriptFileName,uid);
List<Map<String,Object>> save = ScriptLoader.loadSave(scriptFileName,uid);
VisualNovelEngine vnEngine = new VisualNovelEngine(script, scriptFileName, uid,day,inventory,save);
sendClientMessage(player, "Trying to load the file config/mobtalkerredux/" + scriptFileName);
clientSideRenderDialogueScreen(vnEngine,player);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public InteractionResult interactLivingEntity(ItemStack stack, Player player, Li
try{
// Check if the entity has a custom name
if (target.getCustomName() != null) {
String entityName = target.getCustomName().getString();
String entityName = target.getCustomName().getString().toLowerCase().replace(" ", "_");

if (!world.isClientSide()) { // Only run on the server side

Expand Down Expand Up @@ -61,8 +61,9 @@ private static void serverSideExecute(Player player, String scriptFileName) {
boolean day = (timeOfDay >= 0 && timeOfDay < 12000);
try {
List<Map<String,Object>> script = ScriptLoader.loadScript(scriptFileName,uid);
List<Map<String,Object>> save = ScriptLoader.loadSave(scriptFileName,uid);
if(script!=null){
VisualNovelEngine vnEngine = new VisualNovelEngine(script, scriptFileName, uid,day,inventory);
VisualNovelEngine vnEngine = new VisualNovelEngine(script, scriptFileName, uid,day,inventory,save);
sendClientMessage(player, "Trying to load the file mobtalkerredux/" + scriptFileName);
clientSideRenderDialogueScreen(vnEngine);
}
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/command/MobFreezer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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;

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;
}

/**
* Checks if a mob is currently frozen
*/
public static boolean isFrozen(UUID uuid) {
return frozenMobs.containsKey(uuid);
}
}
110 changes: 110 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/command/TeamHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.arsparadox.mobtalkerredux.command;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.scores.PlayerTeam;
import net.minecraft.world.scores.Scoreboard;
import net.minecraft.world.scores.Team;
import net.minecraft.server.level.ServerLevel;

public class TeamHandler {
public static final String DEFAULT_TEAM_NAME = "harem_mobs";

/**
* Creates a team if it doesn't exist
*/
public static PlayerTeam getOrCreateTeam(ServerLevel level, String teamName) {
Scoreboard scoreboard = level.getScoreboard();
PlayerTeam team = scoreboard.getPlayerTeam(teamName);

if (team == null) {
team = scoreboard.addPlayerTeam(teamName);
setupTeamDefaults(team);
}

return team;
}

/**
* Sets up default team options
*/
private static void setupTeamDefaults(PlayerTeam team) {
team.setSeeFriendlyInvisibles(true);
team.setAllowFriendlyFire(false);
team.setCollisionRule(Team.CollisionRule.NEVER);
}

/**
* Adds an entity to the same team as a player
*/
public static void addToPlayerTeam(Player player, Entity entity) {
if (player.level().isClientSide()) return;

ServerLevel level = (ServerLevel) player.level();
Scoreboard scoreboard = level.getScoreboard();

String teamName = getPlayerTeamName(player);
if (teamName == null) {
teamName = DEFAULT_TEAM_NAME;
PlayerTeam team = getOrCreateTeam(level, teamName);
scoreboard.addPlayerToTeam(player.getScoreboardName(), team);
}

scoreboard.addPlayerToTeam(entity.getScoreboardName(), scoreboard.getPlayerTeam(teamName));
}

/**
* Adds an entity to a specific team
*/
public static void addToTeam(ServerLevel level, Entity entity, String teamName) {
PlayerTeam team = getOrCreateTeam(level, teamName);
level.getScoreboard().addPlayerToTeam(entity.getScoreboardName(), team);
}

/**
* Removes an entity from its team
*/
public static void removeFromTeam(Entity entity) {
if (entity.level().isClientSide()) return;

ServerLevel level = (ServerLevel) entity.level();
Scoreboard scoreboard = level.getScoreboard();
scoreboard.removePlayerFromTeam(entity.getScoreboardName());
}

/**
* Checks if an entity is in any team
*/
public static boolean isInAnyTeam(Entity entity) {
if (entity.level().isClientSide()) return false;

ServerLevel level = (ServerLevel) entity.level();
return level.getScoreboard().getPlayersTeam(entity.getScoreboardName()) != null;
}

/**
* Checks if two entities are in the same team
*/
public static boolean areInSameTeam(Entity entity1, Entity entity2) {
if (entity1.level().isClientSide()) return false;

ServerLevel level = (ServerLevel) entity1.level();
Scoreboard scoreboard = level.getScoreboard();

PlayerTeam team1 = scoreboard.getPlayersTeam(entity1.getScoreboardName());
PlayerTeam team2 = scoreboard.getPlayersTeam(entity2.getScoreboardName());

return team1 != null && team1 == team2;
}

/**
* Gets the name of the team a player is in
*/
public static String getPlayerTeamName(Player player) {
if (player.level().isClientSide()) return null;

ServerLevel level = (ServerLevel) player.level();
PlayerTeam team = level.getScoreboard().getPlayersTeam(player.getScoreboardName());
return team != null ? team.getName() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class VisualNovelEngine {

public AtomicBoolean shutdown = new AtomicBoolean(false);
public List<Map<String, Object>> gameData;
public List<Map<String, Object>> saves;
public AtomicLong currentState = new AtomicLong(0);
public Map<String, Object> variables = new HashMap<>();

Expand All @@ -38,10 +39,11 @@ public class VisualNovelEngine {
public PlayerInventoryHandler inventoryHandler;


public VisualNovelEngine(List<Map<String, Object>> gameData,String scriptName, String uid, boolean day,PlayerInventoryHandler inventory) {
public VisualNovelEngine(List<Map<String, Object>> gameData,String scriptName, String uid, boolean day,PlayerInventoryHandler inventory,List<Map<String, Object>> save) {
this.uid.setLength(0);
this.uid.append(uid);
this.gameData = gameData;
this.saves = save;
this.state = new DialogueState(null,null,null);
this.scriptName.setLength(0);
this.scriptName.append(scriptName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ public static void processConditional(Map<String, Object> condition, VisualNovel
Object value = condition.get("value");
long end = (long) condition.get("end");

System.out.println("Is it day time???: "+vn.isDay);

switch (conditionType) {
case "equal":
result = (var != null) && var.equals(value);
result = (var != null && value != null) &&
((var instanceof Number && value instanceof Number)
? ((Number) var).doubleValue() == ((Number) value).doubleValue()
: var.equals(value));
break;
case "not_equal":
result = (var == null) || !var.equals(value);
result = (var == null || value == null) ||
((var instanceof Number && value instanceof Number)
? ((Number) var).doubleValue() != ((Number) value).doubleValue()
: !var.equals(value));
break;
case "less_than":
result = (var instanceof Number && value instanceof Number) && ((Number) var).doubleValue() < ((Number) value).doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
public class VariableHandler {

public static void initializeVariable(VisualNovelEngine vn) {
if(!"variable".equals(vn.gameData.get(vn.gameData.size() - 1).get("type"))){
if(vn.saves==null){
vn.variables.put("type","variable");
vn.gameData.add(vn.variables);
System.out.println("Initialize Variable");
}else{
// Copy all entries from the last gameData map to variables
vn.variables.clear();
vn.variables.putAll(vn.gameData.get(vn.gameData.size() - 1)); // OH SO THAT'S WHY THE REFERENCE IS DIFFERENT!!!
vn.variables.putAll(vn.saves.get(vn.saves.size() - 1)); // OH SO THAT'S WHY THE REFERENCE IS DIFFERENT!!!

if(vn.variables.get("checkpoint")!=null && !((String) vn.variables.get("checkpoint")).isEmpty()){
vn.currentState.set(StateHandler.findLabelId((String) vn.variables.get("checkpoint"),vn.gameData));
Expand All @@ -30,7 +30,7 @@ public static void modifyVariable(String variable, String operation, Object valu
((Number) value).doubleValue();
variables.put(variable, result);
}
} else if (operation.equals("substract_var")) {
} else if (operation.equals("subtract_var")) {
if (variables.get(variable) instanceof Number && value instanceof Number) {
double result = ((Number) variables.get(variable)).doubleValue() -
((Number) value).doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ public static String getWorldName() {
public static List<Map<String, Object>> loadScript(String fileName, String playerUID) throws IOException {
// Try loading from the save folder (level or player UID folder)
fileName = fileName.toLowerCase();
File saveFile = new File(getSaveFilePath(fileName, playerUID,getWorldName()));
if (saveFile.exists()) {
System.out.println("Loading from save folder: " + fileName);
return loadJsonFromFile(saveFile.getPath());
}

// If not found in save, try loading from config
File configFile = new File(getConfigFilePath(fileName));
Expand All @@ -125,6 +120,17 @@ public static List<Map<String, Object>> loadScript(String fileName, String playe
return loadFromResource(fileName);
}

public static List<Map<String, Object>> loadSave(String fileName, String playerUID) {
// Try loading from the save folder (level or player UID folder)
fileName = fileName.toLowerCase();
File saveFile = new File(getSaveFilePath(fileName, playerUID,getWorldName()));
if (saveFile.exists()) {
System.out.println("Loading from save folder: " + fileName);
return loadJsonFromFile(saveFile.getPath());
}
return null;
}

/**
* Saves the current game state to a JSON file in the save folder.
* @param gameState The data to save.
Expand Down

0 comments on commit 5dbd6f6

Please sign in to comment.