Skip to content

Commit

Permalink
Merge pull request #15 from Iteranya/1.20.1_Built_In_Script
Browse files Browse the repository at this point in the history
HUMU!!!
  • Loading branch information
Iteranya authored Nov 23, 2024
2 parents 40e6759 + 6497f0b commit 97e6c82
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 30 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/arsparadox/mobtalkerredux/CustomItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public class CustomItem extends Item {
// No Like, Literally Custom Item

public CustomItem(Properties properties) {
super(properties);
public CustomItem() {
super(new Item.Properties());
}

@Override
Expand All @@ -24,11 +24,10 @@ public Component getName(ItemStack stack) {
return super.getName(stack);
}

// Override texture retrieval
@Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
if (stack.hasTag() && stack.getTag().contains("TextureKey")) {
tooltip.add(Component.translatable("Texture: " + stack.getTag().getString("TextureKey")));
if (stack.hasTag() && stack.getTag().contains("Description")) {
tooltip.add(Component.translatable("Description: " + stack.getTag().getString("Description")));
}
super.appendHoverText(stack, level, tooltip, flag);
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/CustomItemUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.arsparadox.mobtalkerredux;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

public class CustomItemUtils {
// Basic version - just give a variant
public static void giveCustomItem(Player player, String variantName) {
ItemStack itemStack = new ItemStack(MobTalkerRedux.RegistryEvents.CUSTOM_ITEM.get());
CompoundTag tag = new CompoundTag();
tag.putString("CustomName", "item.mobtalkerredux." + variantName);
tag.putString("TextureKey", "custom_item_" + variantName);
tag.putInt("CustomModelData", getVariantId(variantName));
itemStack.setTag(tag);

giveItemToPlayer(player, itemStack);
}


// Private helper methods
private static void giveItemToPlayer(Player player, ItemStack stack) {
if (!player.getInventory().add(stack)) {
// If inventory is full, spawn item in world
player.drop(stack, false);
}
}

private static int getVariantId(String variantName) {
// You can expand this method to map variant names to custom model data IDs
return switch (variantName.toLowerCase()) {
case "variant1" -> 1;
case "variant2" -> 2;
case "variant3" -> 3;
default -> 1;
};
}
}
8 changes: 4 additions & 4 deletions src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import org.arsparadox.mobtalkerredux.vn.controller.vnmodules.PlayerInventoryHandler;
import org.arsparadox.mobtalkerredux.vn.controller.VisualNovelEngine;
import org.arsparadox.mobtalkerredux.vn.controller.vnmodules.PlayerInventoryHandler;
import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader;
import org.arsparadox.mobtalkerredux.vn.view.DialogueScreen;

Expand All @@ -22,7 +22,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("mob_talker")
.executes(context -> {
if (context.getSource().getEntity() instanceof ServerPlayer player) {
serverSideExecute(player, "demo.json");
serverSideExecute(player, "demo");
}
return 1;
})
Expand All @@ -43,12 +43,12 @@ private static void serverSideExecute(ServerPlayer player, String scriptFileName
boolean day = player.level().isDay();

try {
List<Map<String,Object>> script = ScriptLoader.loadScript(scriptFileName,null,uid);
List<Map<String,Object>> script = ScriptLoader.loadScript(scriptFileName,"default",uid);
List<Map<String,Object>> global = ScriptLoader.loadGlobal(uid);
VisualNovelEngine vnEngine = new VisualNovelEngine(
script,
scriptFileName,
null,
"demo",
uid,
day,
inventory,
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/arsparadox/mobtalkerredux/MobTalkerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.arsparadox.mobtalkerredux.command.TeamHandler;
import org.arsparadox.mobtalkerredux.vn.controller.VisualNovelEngine;
import org.arsparadox.mobtalkerredux.vn.controller.vnmodules.PlayerInventoryHandler;
import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader;
Expand Down Expand Up @@ -39,16 +40,21 @@ public MobTalkerItem() {
// Safely check and cast to ServerPlayer
} else { // Client-side: Open dialogue screen
Minecraft minecraft = Minecraft.getInstance();
//CustomItemUtils.giveCustomItem(player,"variant1");
minecraft.execute(() -> {
sendClientMessage(player,"The Entity's type is: "+entityType);
VisualNovelEngine vn = serverSideExecute(player, entityType, entityName,target);
clientSideRenderDialogueScreen(vn,target,player);
if(vn!=null){
clientSideRenderDialogueScreen(vn,target,player);

}

});
}
return InteractionResult.SUCCESS;
}
} catch (Exception ignored) {

System.out.println(ignored.toString());
}

return InteractionResult.PASS; // Return PASS if the entity doesn't have a custom name
Expand All @@ -75,6 +81,7 @@ private static VisualNovelEngine serverSideExecute(Player player, String entityT
globalSave,
localSave
);
TeamHandler.addToPlayerTeam(player,target);
return vnEngine;
// sendClientMessage(player, "Trying to load the file mobtalkerredux/" + scriptFileName);
//clientSideRenderDialogueScreen(vnEngine,target,player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static class RegistryEvents {
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);

public static final RegistryObject<Item> MOB_TALKER_ITEM = ITEMS.register("mob_talker_item", MobTalkerItem::new);
public static final RegistryObject<Item> CUSTOM_ITEM = ITEMS.register("custom_item", CustomItem::new);

// Command registration stays on Forge event bus
@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import org.arsparadox.mobtalkerredux.vn.controller.vnmodules.SaveHandler;
import org.arsparadox.mobtalkerredux.vn.data.DialogueState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -152,9 +149,9 @@ private void processAction(Map<String, Object> action) {
}
break;
case "unlock_dialogues":
List<String> events = (List<String>) this.localVariables.getOrDefault("unlocked_events", new ArrayList<>());
Set<String> events = new HashSet<>((List<String>) this.localVariables.getOrDefault("unlocked_events", new ArrayList<>()));
events.addAll((List<String>) action.get("events"));
this.localVariables.put("unlocked_events", events);
this.localVariables.put("unlocked_events", new ArrayList<>(events));
this.currentState.incrementAndGet();
break;
case "play_sound":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public class DialogueScreen extends Screen{
public Mob mob;
private Player player;

private boolean hiddenDialogue = false;




public DialogueScreen(VisualNovelEngine vn, LivingEntity target, Player player) throws FileNotFoundException {
super(Component.empty());
this.vn = vn;
this.se = new SoundUtils();
this.mob = (Mob)target;
this.player = player;


}
Expand Down Expand Up @@ -120,15 +120,34 @@ public void updateSprites(DialogueState state){
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) {
vn.isEngineRunning.set(true);
vn.runEngine();
//Tell Engine to update the Globals, as in like, get the current state and put it in the global in this class
update();
if(!hiddenDialogue){
vn.isEngineRunning.set(true);
vn.runEngine();
//Tell Engine to update the Globals, as in like, get the current state and put it in the global in this class
update();
}

}

return super.mouseClicked(mouseX, mouseY, button);
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == 32 || keyCode == 257) { // 32 = Space, 257 = Enter
if(!hiddenDialogue){
vn.isEngineRunning.set(true);
vn.runEngine();
update();
}

}
else if(keyCode == 72){
hiddenDialogue = !hiddenDialogue;
}
return super.keyPressed(keyCode, scanCode, modifiers);
}

private void startScene() {
vn.isEngineRunning.set(true);
vn.runEngine();//run the engine, let it do some loops and backflips, engine's globals shouldn't affect this sacred place
Expand All @@ -151,14 +170,14 @@ public void render(GuiGraphics poseStack, int mouseX, int mouseY, float partialT
// Render the Sprites and Everything In Foreground
if (spritesToRender != null && !spritesToRender.isEmpty()) {
ForegroundComponent.processForeground(
poseStack, this.width, this.height, spritesToRender
poseStack, this.width, this.height, spritesToRender,vn.localVariables
);
}

// Render the Dialogue Box
if (content != null && !content.isEmpty()) {
if (content != null && !content.isEmpty() && !hiddenDialogue) {
poseStack = DialogueBoxManager.processGui(
poseStack,this.width,this.height,content,label,this.font
poseStack,this.width,this.height,content,label,this.font,vn.localVariables
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DialogueBoxManager {

public static GuiGraphics processGui(GuiGraphics poseStack, int width,int height,String content, String label, Font font){
public static GuiGraphics processGui(GuiGraphics poseStack, int width,int height,String content, String label, Font font, Map<String,Object> variables){
int CHARACTER_NAME_OFFSET = 40;
int DISPLAYED_SPRITE_HEIGHT = 300;
int dialogueBoxHeight = 80;
int DIALOGUE_BOX_PADDING = 15;
if (content != null && !content.isEmpty()) {
// Set dialogue box dimensions and position
content = replaceTemplateVariables(variables,content);
int boxWidth = Math.min(600, width - 40); // Max width of 600 or screen width - 40
int boxX = (width - boxWidth) / 2;
int boxY = height - dialogueBoxHeight - 5; // 20 pixels from bottom
Expand Down Expand Up @@ -178,6 +182,27 @@ private static List<String> wrapText(String text, int maxWidth, Font font) {
return lines;
}

public static String replaceTemplateVariables(Map<String, Object> variables, String template) {
if (template == null || variables == null) {
return template;
}

String result = template;
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(template);

while (matcher.find()) {
String key = matcher.group(1);
Object value = variables.get(key);

if (value != null) {
result = result.replace("<" + key + ">", value.toString());
}
}

return result;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.arsparadox.mobtalkerredux.vn.data.SpriteState;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ForegroundComponent {
// MINECRAFT RENDERING SYSTEM IS A NIGHTMARE!!!
Expand All @@ -25,11 +28,13 @@ public class ForegroundComponent {
// Yeah...
// So in the FSM, determining position should be like:
// (Screen Ratio, Image Ratio, Coordinate Position) -> (16x9, 3x5, 8x1)
public static GuiGraphics processForeground(GuiGraphics poseStack, int width, int height, List<SpriteState> spritesToRender){
public static GuiGraphics processForeground(GuiGraphics poseStack, int width, int height, List<SpriteState> spritesToRender, Map<String, Object> variables){

for (SpriteState sprite : spritesToRender) {
String spriteLocationRaw = sprite.getLocation();
String spriteLocation = replaceTemplateVariables(variables,spriteLocationRaw);
ResourceLocation currentSprite = new ResourceLocation(
"mobtalkerredux", "textures/" + sprite.getLocation()
"mobtalkerredux", "textures/" + spriteLocation
);
RenderSystem.setShaderTexture(0, currentSprite);

Expand Down Expand Up @@ -77,4 +82,27 @@ public static GuiGraphics processForeground(GuiGraphics poseStack, int width, in
return poseStack;
}



public static String replaceTemplateVariables(Map<String, Object> variables, String template) {
if (template == null || variables == null) {
return template;
}

String result = template;
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(template);

while (matcher.find()) {
String key = matcher.group(1);
Object value = variables.getOrDefault(key, "default");

result = result.replace("<" + key + ">", value.toString());
}

return result;
}



}
6 changes: 5 additions & 1 deletion src/main/resources/assets/mobtalkerredux/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"item.mobtalkerredux.mob_talker_item": "The Mob Talker"
"item.mobtalkerredux.mob_talker_item": "The Mob Talker",
"item.mobtalkerredux.custom_item": "Custom Item",
"item.mobtalkerredux.variant1": "Variant 1 Item",
"item.mobtalkerredux.variant2": "Variant 2 Item"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"parent": "item/generated",
"textures": {
"layer0": "mobtalkerredux:item/custom_item"
},
"overrides": [
{
"predicate": {
"custom_model_data": 1
},
"model": "mobtalkerredux:item/custom_item_variant1"
},
{
"predicate": {
"custom_model_data": 2
},
"model": "mobtalkerredux:item/custom_item_variant2"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "mobtalkerredux:item/custom_item_variant1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "mobtalkerredux:item/custom_item_variant2"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 97e6c82

Please sign in to comment.