Skip to content

Commit

Permalink
Merge pull request #10 from Iteranya/1.20.1_Built_In_Script
Browse files Browse the repository at this point in the history
Added Voice Feature and a Crafting Recipe for the Mob Talker Item
  • Loading branch information
Iteranya authored Nov 18, 2024
2 parents ade9f30 + 6552af9 commit da75723
Show file tree
Hide file tree
Showing 19 changed files with 504 additions and 340 deletions.
35 changes: 35 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/CustomItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.arsparadox.mobtalkerredux;

import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;

import javax.annotation.Nullable;
import java.util.List;

public class CustomItem extends Item {
// No Like, Literally Custom Item

public CustomItem(Properties properties) {
super(properties);
}

@Override
public Component getName(ItemStack stack) {
if (stack.hasTag() && stack.getTag().contains("CustomName")) {
return Component.translatable(stack.getTag().getString("CustomName"));
}
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")));
}
super.appendHoverText(stack, level, tooltip, flag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
}

private static void serverSideExecute(ServerPlayer player, String scriptFileName) {
String uid = player.getName().toString();
String uid = player.getName().getString();
PlayerInventoryHandler inventory = new PlayerInventoryHandler(player);
boolean day = player.level().isDay();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public InteractionResult interactLivingEntity(ItemStack stack, Player player, Li
// 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ private void processAction(Map<String, Object> action) {
removeSprite((String) action.get("sprite"), this);
return;
case "dialogue":
String sound = (String) action.get("voice");
updateDialogue(
(String) action.get("label"),
(String) action.get("content"),
(String) action.get("voice"),
this);
return;
case "modify_variable":
Expand Down Expand Up @@ -120,6 +122,15 @@ private void processAction(Map<String, Object> action) {
this.variables.put("unlocked_events", events);
this.currentState.incrementAndGet();
break;
case "play_sound":
updateSound(this, (String) action.get("sound"));
case "play_music":
if(action.get("music")!=null){
updateMusic(this, (String) action.get("music"));
}else{
stopMusic(this);
}

case "next":
processNext(action,this);
this.currentState.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.arsparadox.mobtalkerredux.vn.controller.vnmodules;

public class SoundHandler {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ public static Map<String, Object> getDictById(long targetId, List<Map<String, Ob
}

public static void updateDialogue(
String label, String content, VisualNovelEngine vn
String label, String content, String sound,VisualNovelEngine vn
) {
vn.state.setLabel(label);
vn.state.setContent(content);
if(sound!=null){
vn.state.setSound(sound);
}
vn.isEngineRunning.set(false);
vn.currentState.incrementAndGet();
}
Expand All @@ -54,5 +57,15 @@ public static void changeStateByLabel(String label,AtomicLong currentState, List
currentState.set(findLabelId(label,gameData));
}

public static void updateMusic(VisualNovelEngine vn,String music){
vn.state.setMusic(music);
}

public static void stopMusic(VisualNovelEngine vn){
vn.state.setMusic(null);
}

public static void updateSound(VisualNovelEngine vn, String sound){
vn.state.setSound(sound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class DialogueState {
private String content;
private String background;
private String command;
private String music;
private String sound;
private List <SpriteState> sprites = new ArrayList<>();
private List<Map<String, Object>> choices;

Expand Down Expand Up @@ -62,4 +64,18 @@ public String getCommand(){
public void setCommand(String action) {
this.command = action;
}

public String getMusic(){return this.music;}

public void setMusic(String music) {
this.music = music;
}

public String getSound() {
return sound;
}

public void setSound(String sound) {
this.sound = sound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public class DialogueScreen extends Screen{

private String background;
private String command;
private String music;
private String sound;
private SoundUtils se;




public DialogueScreen(VisualNovelEngine vn) throws FileNotFoundException {
super(Component.empty());
this.vn = vn;
this.se = new SoundUtils();

TextureLoader.loadTexturesFromConfig();
//this.player = player;
//dialogueBox = new DialogueBoxComponent();
Expand All @@ -62,6 +67,46 @@ public void update(){
// if(player.server.isSingleplayer()){
// //ForgeCommandRunner.runCommand(player.server,command);
// }

if(state.getSound()!=null){
if(!state.getSound().equals(sound)){
sound = state.getSound();
playSound(sound);
System.out.println("Current Sound: "+state.getSound());
}
}
if(state.getMusic()!=null){
if(!state.getMusic().equals(music)){
music = state.getMusic();
playMusic(music);
System.out.println("Current Music: "+music);
}
}else{
playMusic(music);
music = state.getMusic();
}


}

public void playMusic(String music){
if(music!=null){
ResourceLocation musicPath = new ResourceLocation("mobtalkerredux","music."+music);
se.playMusic(musicPath);
System.out.println("Playing: "+music);
}else{
se.stopMusic();
}

}
public void playSound(String sound){
if(sound!=null){
ResourceLocation soundPath = new ResourceLocation("mobtalkerredux","sound."+sound);
se.playSound(soundPath);
System.out.println("Playing: "+sound);
}else{
se.stopSound();
}
}

public void updateSprites(DialogueState state){
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/org/arsparadox/mobtalkerredux/vn/view/SoundUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.arsparadox.mobtalkerredux.vn.view;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;

public class SoundUtils {

private SimpleSoundInstance currentMusic = null;
private SimpleSoundInstance currentSound = null;

/**
* Plays a sound effect once at full volume
* Stops any currently playing sound effect
* @param sound The ResourceLocation of the sound to play
*/
public void playSound(ResourceLocation sound) {
if (currentSound != null) {
Minecraft.getInstance().getSoundManager().stop(currentSound);
}
currentSound = SimpleSoundInstance.forUI(
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(sound),
1.0F, // Pitch
1.5F // Volume
);
Minecraft.getInstance().getSoundManager().play(currentSound);
}

/**
* Plays a sound effect with custom volume and pitch
* Stops any currently playing sound effect
* @param sound The ResourceLocation of the sound to play
* @param volume Volume from 0.0 to 1.0
* @param pitch Pitch from 0.5 to 2.0
*/
public void playSound(ResourceLocation sound, float volume, float pitch) {
if (currentSound != null) {
Minecraft.getInstance().getSoundManager().stop(currentSound);
}
currentSound = SimpleSoundInstance.forUI(
net.minecraft.sounds.SoundEvent.createVariableRangeEvent(sound),
volume,
pitch
);
Minecraft.getInstance().getSoundManager().play(currentSound);
}

/**
* Plays music that will loop continuously
* Automatically stops any currently playing music
* @param music The ResourceLocation of the music to play
*/
public void playMusic(ResourceLocation music) {
stopMusic();
currentMusic = new SimpleSoundInstance(
music,
SoundSource.MUSIC,
1.0F, // Volume
1.0F, // Pitch
SoundInstance.createUnseededRandom(),
true, // Loop
0, // Delay
SoundInstance.Attenuation.NONE,
0.0D, // x
0.0D, // y
0.0D, // z
true // Relative
);
Minecraft.getInstance().getSoundManager().play(currentMusic);
}

/**
* Plays music with custom volume that will loop continuously
* Automatically stops any currently playing music
* @param music The ResourceLocation of the music to play
* @param volume Volume from 0.0 to 1.0
*/
public void playMusic(ResourceLocation music, float volume) {
stopMusic();
currentMusic = new SimpleSoundInstance(
music,
SoundSource.MUSIC,
volume,
1.0F,
SoundInstance.createUnseededRandom(),
true,
0,
SoundInstance.Attenuation.NONE,
0.0D,
0.0D,
0.0D,
true
);
Minecraft.getInstance().getSoundManager().play(currentMusic);
}

/**
* Stops any currently playing music
*/
public void stopMusic() {
if (currentMusic != null) {
Minecraft.getInstance().getSoundManager().stop(currentMusic);
currentMusic = null;
}
}

/**
* Stops any currently playing sound effect
*/
public void stopSound() {
if (currentSound != null) {
Minecraft.getInstance().getSoundManager().stop(currentSound);
currentSound = null;
}
}
}
Loading

0 comments on commit da75723

Please sign in to comment.