Skip to content

Commit

Permalink
Merge pull request #7 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 10, 2024
2 parents fddfd6b + 2ee28dd commit dc62992
Show file tree
Hide file tree
Showing 36 changed files with 696 additions and 533 deletions.
33 changes: 0 additions & 33 deletions src/main/java/org/arsparadox/mobtalkerredux/DebugTile.java

This file was deleted.

56 changes: 33 additions & 23 deletions src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,55 @@
import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader;
import org.arsparadox.mobtalkerredux.vn.view.DialogueScreen;

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

public class DemoCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("mob_talker")
// Basic command: /demo
.executes(context -> {
if (context.getSource().getEntity() instanceof ServerPlayer player) {
Minecraft.getInstance().execute(() -> {
try {
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo(),"demo.json")));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
);
serverSideExecute(player, "demo.json");
}
return 1;
})
// Command with argument: /demo <name>
.then(Commands.argument("scriptFile", StringArgumentType.word())
.executes(context -> {
String name = StringArgumentType.getString(context, "scriptFile");
String scriptFileName = StringArgumentType.getString(context, "scriptFile");
if (context.getSource().getEntity() instanceof ServerPlayer player) {
Minecraft.getInstance().execute(() -> {
try {
player.sendSystemMessage(Component.literal("Trying to load the file config/mobtalkerredux/"+name));
Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadScript(name),"demo.json")));

} catch (IOException e) {
player.sendSystemMessage(Component.literal("Failed to find the file config/mobtalkerredux/"+name));
throw new RuntimeException(e);

}
}
);
serverSideExecute(player, scriptFileName);
}
return 1;
}))
);
}

private static void serverSideExecute(ServerPlayer player, String scriptFileName) {
String uid = player.getName().toString();

boolean day = player.level().isDay();
try {
VisualNovelEngine vnEngine = new VisualNovelEngine(ScriptLoader.loadScript(scriptFileName,uid), scriptFileName, uid,day);
sendClientMessage(player, "Trying to load the file config/mobtalkerredux/" + scriptFileName);
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,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);
}
});
}

private static void sendClientMessage(ServerPlayer player, String message) {
player.sendSystemMessage(Component.literal(message));
}
}
53 changes: 0 additions & 53 deletions src/main/java/org/arsparadox/mobtalkerredux/HelloWorldItem.java

This file was deleted.

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

0 comments on commit dc62992

Please sign in to comment.