Skip to content

Commit

Permalink
Aight, day night integration
Browse files Browse the repository at this point in the history
And the Mob Talker Item

Oh my god, when was the last time I pushed this thing?

Ah, whatever...
  • Loading branch information
Iteranya committed Nov 10, 2024
1 parent 7e8dbda commit 2ee28dd
Show file tree
Hide file tree
Showing 31 changed files with 491 additions and 60 deletions.
33 changes: 0 additions & 33 deletions src/main/java/org/arsparadox/mobtalkerredux/DebugTile.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ private static void serverSideExecute(ServerPlayer player, String scriptFileName
try {
VisualNovelEngine vnEngine = new VisualNovelEngine(ScriptLoader.loadScript(scriptFileName,uid), scriptFileName, uid,day);
sendClientMessage(player, "Trying to load the file config/mobtalkerredux/" + scriptFileName);
clientSideRenderDialogueScreen(vnEngine);
clientSideRenderDialogueScreen(vnEngine,player);
} catch (IOException e) {
sendClientMessage(player, "Failed to find the file config/mobtalkerredux/" + scriptFileName);
throw new RuntimeException(e);
}
}

private static void clientSideRenderDialogueScreen(VisualNovelEngine vnEngine) {
private static void clientSideRenderDialogueScreen(VisualNovelEngine vnEngine,ServerPlayer player) {
Minecraft.getInstance().execute(() -> {
try {
//Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,player));
Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/MobTalkerItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.arsparadox.mobtalkerredux;

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
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.vn.controller.VisualNovelEngine;
import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader;
import org.arsparadox.mobtalkerredux.vn.view.DialogueScreen;

import java.io.FileNotFoundException;
import java.io.IOException;

public class MobTalkerItem extends Item {

public MobTalkerItem() {
super(new Item.Properties());
}

@Override
public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity target, InteractionHand hand) {
Level world = player.level();

// Check if the entity has a custom name
if (target.getCustomName() != null) {
String entityName = target.getCustomName().getString();
boolean day = world.isDay();

if (!world.isClientSide()) { // Only run on the server side
player.sendSystemMessage(Component.literal("Hewwo World~"));
} else { // Client-side: Open dialogue screen
Minecraft minecraft = Minecraft.getInstance();
minecraft.execute(() -> {

serverSideExecute(player, entityName+".json");

});
}
return InteractionResult.SUCCESS;
}

return InteractionResult.PASS; // Return PASS if the entity doesn't have a custom name
}

private static void serverSideExecute(Player player, String scriptFileName) {
//String uid = player.getName().toString();//literal{Dev}
String uid = player.getName().getString();//Dev
long timeOfDay = player.level().getDayTime() % 24000; // Minecraft-style day/night cycle in ticks
boolean day = (timeOfDay >= 0 && timeOfDay < 12000);
try {
VisualNovelEngine vnEngine = new VisualNovelEngine(ScriptLoader.loadScript(scriptFileName,uid), scriptFileName, uid,day);
sendClientMessage(player, "Trying to load the file config/mobtalkerredux/" + scriptFileName);
clientSideRenderDialogueScreen(vnEngine);
} catch (IOException e) {
sendClientMessage(player, "Failed to find the file config/mobtalkerredux/" + scriptFileName);
throw new RuntimeException(e);
}
}

private static void clientSideRenderDialogueScreen(VisualNovelEngine vnEngine) {
Minecraft.getInstance().execute(() -> {
try {
//Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine,player));
Minecraft.getInstance().setScreen(new DialogueScreen(vnEngine));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
});
}

private static void sendClientMessage(Player player, String message) {
player.sendSystemMessage(Component.literal(message));
}
}

20 changes: 19 additions & 1 deletion src/main/java/org/arsparadox/mobtalkerredux/MobTalkerRedux.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.arsparadox.mobtalkerredux;

import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
Expand All @@ -15,8 +17,10 @@
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
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 All @@ -42,7 +46,9 @@ public MobTalkerRedux() {
}

private void setup(final FMLCommonSetupEvent event) {

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

private void enqueueIMC(final InterModEnqueueEvent event) {
Expand All @@ -67,12 +73,22 @@ public static class RegistryEvents {
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);

// Move command registration to Forge event bus
public static final RegistryObject<Item> MOB_TALKER_ITEM = ITEMS.register("mob_talker_item", MobTalkerItem::new);

// Command registration stays on Forge event bus
@SubscribeEvent
public static void onRegisterCommands(RegisterCommandsEvent event) {
DemoCommand.register(event.getDispatcher());
}

@SubscribeEvent
public static void buildContents(BuildCreativeModeTabContentsEvent event) {
// Add to tools tab
if (event.getTabKey() == CreativeModeTabs.TOOLS_AND_UTILITIES) {
event.accept(MOB_TALKER_ITEM);
}
}

public static void register(IEventBus eventBus) {
BLOCKS.register(eventBus);
ITEMS.register(eventBus);
Expand All @@ -81,6 +97,8 @@ public static void register(IEventBus eventBus) {
public static void initialize() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
register(modEventBus);
// Register only the build contents event to mod bus
modEventBus.addListener(RegistryEvents::buildContents);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.arsparadox.mobtalkerredux.vn.controller;

import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;

public class CommandRequestHandler {
public static boolean handleCommandRequest(ServerPlayer player, String command) {
MinecraftServer server = player.getServer();

// Early return if server is null
if (server == null) return false;

// Check if it's single player
boolean isSinglePlayer = server.isSingleplayer();

if (isSinglePlayer) {
// Single player - just run the command with full permissions
return ForgeCommandRunner.runCommand(server, command);
}

// Multiplayer validation
if (!isCommandAllowed(command)) {
// Command not in whitelist
player.sendSystemMessage(Component.literal("This command is not allowed!"));
return false;
}

if (!hasPermission(player)) {
// Player doesn't have permission
player.sendSystemMessage(Component.literal("You don't have permission to use visual novel commands!"));
return false;
}

// Rate limiting check
if (isRateLimited(player)) {
player.sendSystemMessage(Component.literal("Please wait before using another command!"));
return false;
}

// If we get here, command is allowed - run it with appropriate permission level
// Note: You might want to run with player's actual permission level instead of level 4
return ForgeCommandRunner.runCommand(server, command);
}

private static boolean isCommandAllowed(String command) {
// Example validation - you should implement your own logic
// Maybe check against a config-defined whitelist
// Maybe block dangerous commands like /op, /stop, etc.
return !command.startsWith("/op") &&
!command.startsWith("/stop") &&
!command.startsWith("/ban");
}

private static boolean hasPermission(ServerPlayer player) {
// Example permission check
// Could check against your mod's permission system
// Or check player's op status
return player.hasPermissions(2); // Level 2 is typical for command blocks
}

private static boolean isRateLimited(ServerPlayer player) {
// Implement rate limiting logic
// Could track last command time per player
// Return true if player is sending too many commands
return false; // Simplified for example
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.arsparadox.mobtalkerredux.vn.controller;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.MinecraftServer;



///
public class ForgeCommandRunner {
public static CommandSourceStack getServerCommandSourceStack(MinecraftServer server) {
// Creates a command source stack with the server's permissions and context
return server.createCommandSourceStack()
.withPermission(4);
}

public static boolean runCommand(MinecraftServer server, String command) {
CommandSourceStack sourceStack = getServerCommandSourceStack(server);
try {
CommandDispatcher<CommandSourceStack> dispatcher = server.getCommands().getDispatcher();
dispatcher.execute(command, sourceStack);
return true;
} catch (Exception e) {
e.printStackTrace();
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,13 @@ private void updateChoices(List<Map<String, Object>> choices) {
this.isEngineRunning = false;
}

private Object processCommand(Map<String, Object> value) {
private void processCommand(Map<String, Object> value) {
String action = (String) value.get("action");
if ("get_gamemode".equals(action)) {
return "Survival";
} else if ("custom_command".equals(action)) {
// NOT IMPLEMENTED YET
return "Nothing for now";
}
return "Nothing for now";
state.setCommand(action);
}

@SuppressWarnings("unchecked")
private void modifyVariable(String variable, String operation, Object value) {
if (value instanceof Map) {
value = processCommand((Map<String, Object>) value);
}

if (operation.equals("increment_var")) {
if (this.variables.get(variable) instanceof Number && value instanceof Number) {
double result = ((Number) this.variables.get(variable)).doubleValue() +
Expand Down Expand Up @@ -185,9 +175,7 @@ private void processConditional(Map<String, Object> condition) {
Object value = condition.get("value");
long end = (long) condition.get("end");

if (value instanceof Map) {
value = processCommand((Map<String, Object>) value);
}
System.out.println("Is it day time???: "+isDay);

switch (conditionType) {
case "equal":
Expand Down Expand Up @@ -263,6 +251,7 @@ private boolean processAction(Map<String, Object> action) {
break;
case "command":
processCommand(action);
this.currentState++;
break;
case "label":
this.currentState++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class DialogueState {
private String label;
private String content;
private String background;
private String command;
private List <SpriteState> sprites = new ArrayList<>();
private List<Map<String, Object>> choices;

Expand Down Expand Up @@ -53,4 +54,12 @@ public void clearBackground() {
public String getBackground() {
return this.background;
}

public String getCommand(){
return this.command;
}

public void setCommand(String action) {
this.command = action;
}
}
Loading

0 comments on commit 2ee28dd

Please sign in to comment.