Skip to content

Commit

Permalink
Add scripted dialogs, UI for modifying additional npc data and availa…
Browse files Browse the repository at this point in the history
…bility level check
  • Loading branch information
GlassSpirit committed Dec 7, 2019
1 parent 842d726 commit 773bacf
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 11 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
modGroup=ru.glassspirit
modVersion=1.2
modVersion=1.3
modBaseName=CNPC-NTRPG
forgeVersion=1.12.2-14.23.5.2847
spongeForgeVersion=1.12.2-2825-7.1.6
spongeForgeVersion=1.12.2-2838-7.1.7-RC3928
mcpVersion=stable_39
5 changes: 4 additions & 1 deletion src/main/java/ru/glassspirit/cnpcntrpg/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class Configuration {
public boolean NPC_KILLS_EXP_RPG = true;

@Setting(value = "npc_kills_exp_minecraft", comment = "Minecraft experience for killing NPCs")
public boolean NPC_KILLS_EXP_MINECRAFT = true;
public boolean NPC_KILLS_EXP_MINECRAFT = false;

@Setting(value = "availability_rpg_level", comment = "Use RPG character level for availability checks")
public boolean AVAILABILITY_RPG_LEVEL = true;

}
71 changes: 71 additions & 0 deletions src/main/java/ru/glassspirit/cnpcntrpg/forge/DataNpcRpg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.glassspirit.cnpcntrpg.forge;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.entity.data.DataScript;
import ru.glassspirit.cnpcntrpg.mixin.IMixinDataStats;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

public class DataNpcRpg extends DataScript {

public static final Map<UUID, UUID> playersEditingRpgData = new HashMap<>();
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();

private EntityNPCInterface npc;

public DataNpcRpg(EntityNPCInterface npc, boolean init) {
super(npc);
this.npc = npc;
if (init) {
setEnabled(true);
initNpcData();
initProperties();
}
}

private void initNpcData() {
ScriptContainer mainContainer = new ScriptContainer(this);
mainContainer.appandConsole("This is RPG data of NPC.");
mainContainer.appandConsole("Do not create new tabs or attach scripts (THIS IS NOT A SCRIPTING GUI!!!)");
mainContainer.appandConsole("1. First tab is for additional NPC data");
mainContainer.appandConsole("2. Second tab is for default nt-rpg properties");

Map<String, Object> dataMap = new TreeMap<>();
dataMap.put("Level", ((IMixinDataStats) npc.stats).getLevel());

mainContainer.script += gson.toJson(dataMap);
this.getScripts().add(mainContainer);
}

private void initProperties() {
ScriptContainer container = new ScriptContainer(this);

container.script += "properties doesn't work yet"; //TODO properties
this.getScripts().add(container);
}

public void apply() {
applyNpcData();
applyProperties();
}

private void applyNpcData() {
String data = this.getScripts().get(0).script;
Map<String, Object> dataMap = gson.fromJson(data, new TypeToken<TreeMap<String, Object>>() {
}.getType());

((IMixinDataStats) npc.stats).setLevel(((Double) dataMap.get("Level")).intValue());
}

private void applyProperties() {
//TODO properties
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.glassspirit.cnpcntrpg.mixin;

public interface IMixinDataStats {

int getLevel();

void setLevel(int level);

}
19 changes: 19 additions & 0 deletions src/main/java/ru/glassspirit/cnpcntrpg/mixin/IMixinDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.glassspirit.cnpcntrpg.mixin;

import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.controllers.data.Dialog;
import noppes.npcs.entity.EntityNPCInterface;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public interface IMixinDialog {

Map<UUID, Dialog> playerTemporaryDialogMap = new HashMap<>();

boolean isScripted();

void processScripts(EntityPlayer player, EntityNPCInterface npc);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.glassspirit.cnpcntrpg.mixin;

public interface IMixinDialogOption {

void setScript(Object script);

Object getScript();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.glassspirit.cnpcntrpg.mixin.impl;

import cz.neumimto.rpg.sponge.NtRpgPlugin;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.controllers.data.Availability;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.glassspirit.cnpcntrpg.sponge.CnpcRpgSponge;

@Mixin(value = Availability.class, remap = false)
public class MixinAvailablilty {

@Shadow
public int minPlayerLevel;

@Inject(method = "isAvailable(Lnet/minecraft/entity/player/EntityPlayer;)Z", at = @At("TAIL"), cancellable = true)
private void onIsAvailable(EntityPlayer player, CallbackInfoReturnable<Boolean> ci) {
if (CnpcRpgSponge.configuration.AVAILABILITY_RPG_LEVEL) {
ci.setReturnValue(NtRpgPlugin.GlobalScope.characterService.getCharacter(player.getUniqueID()).getLevel() >= this.minPlayerLevel);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.glassspirit.cnpcntrpg.mixin;
package ru.glassspirit.cnpcntrpg.mixin.impl;

import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import noppes.npcs.CustomNpcs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.glassspirit.cnpcntrpg.mixin;
package ru.glassspirit.cnpcntrpg.mixin.impl;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.glassspirit.cnpcntrpg.mixin;
package ru.glassspirit.cnpcntrpg.mixin.impl;

import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.entity.data.DataStats;
Expand All @@ -7,9 +7,10 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.glassspirit.cnpcntrpg.mixin.IMixinDataStats;

@Mixin(DataStats.class)
public abstract class MixinDataStats {
public abstract class MixinDataStats implements IMixinDataStats {

private int level;

Expand All @@ -23,10 +24,12 @@ private void injectReadNbt(NBTTagCompound tag, CallbackInfo ci) {
this.level = tag.getInteger("Level");
}

@Override
public int getLevel() {
return level;
}

@Override
public void setLevel(int l) {
if (l > 0) this.level = l;
else level = 0;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/ru/glassspirit/cnpcntrpg/mixin/impl/MixinDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.glassspirit.cnpcntrpg.mixin.impl;

import cz.neumimto.rpg.sponge.NtRpgPlugin;
import net.minecraft.entity.player.EntityPlayer;
import noppes.npcs.api.NpcAPI;
import noppes.npcs.controllers.data.Dialog;
import noppes.npcs.entity.EntityNPCInterface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import ru.glassspirit.cnpcntrpg.mixin.IMixinDialog;

import javax.script.Invocable;
import javax.script.ScriptEngine;

@Mixin(value = Dialog.class, remap = false)
public class MixinDialog implements IMixinDialog {

private static String scriptMark = "$$$";

@Shadow
public String text;

@Override
public boolean isScripted() {
return this.text.contains(scriptMark);
}

@Override
public void processScripts(EntityPlayer player, EntityNPCInterface npc) {
StringBuilder text = new StringBuilder(this.text);
while (isScripted()) {
int startIndex = text.indexOf(scriptMark);
int endIndex = text.indexOf(scriptMark, startIndex + scriptMark.length());
if (endIndex == -1) break; // Something went wrong!

String function = text.substring(startIndex + scriptMark.length(), endIndex);
String result = "";
try {
ScriptEngine engine = NtRpgPlugin.GlobalScope.jsLoader.getEngine();
Object func = engine.eval(" function(dialog, player, npc) { " + function + " }");
engine.eval("var runScriptDialog = function(f, dialog, player, npc) {\n" +
" return f(dialog, player, npc);\n" +
"}");
Invocable i = (Invocable) engine;
Object obj = i.invokeFunction("runScriptDialog", func, this, NpcAPI.Instance().getIEntity(player), NpcAPI.Instance().getIEntity(npc));
if (obj != null) result = obj.toString();
} catch (Exception e) {
e.printStackTrace();
}
text.replace(startIndex, endIndex + scriptMark.length(), result);
}
this.text = text.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.glassspirit.cnpcntrpg.mixin.impl;

import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.data.DialogOption;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import ru.glassspirit.cnpcntrpg.mixin.IMixinDialogOption;

@Mixin(value = DialogOption.class, remap = false)
public class MixinDialogOption implements IMixinDialogOption {

@Shadow
public int optionType;
@Shadow
public String command;
@Shadow
public String title;
@Shadow
public int dialogId;
@Shadow
public int optionColor;

private Object script;

@Override
public Object getScript() {
return script;
}

@Override
public void setScript(Object script) {
this.script = script;
this.optionType = 5;
}

/**
* @author GlassSpirit 05.12.2019
* @reason Store script data
*/
@Overwrite
public void readNBT(NBTTagCompound compound) {
if (compound != null) {
this.title = compound.getString("Title");
this.dialogId = compound.getInteger("Dialog");
this.optionColor = compound.getInteger("DialogColor");
this.optionType = compound.getInteger("OptionType");
this.command = compound.getString("DialogCommand");
if (this.optionColor == 0) {
this.optionColor = 14737632;
}
this.script = compound.getString("Script");
}
}

/**
* @author GlassSpirit 05.12.2019
* @reason Store script data
*/
@Overwrite
public NBTTagCompound writeNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setString("Title", this.title);
compound.setInteger("OptionType", this.optionType);
compound.setInteger("Dialog", this.dialogId);
compound.setInteger("DialogColor", this.optionColor);
compound.setString("DialogCommand", this.command);
if (this.script != null) compound.setString("Script", this.script.toString());
return compound;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.glassspirit.cnpcntrpg.mixin;
package ru.glassspirit.cnpcntrpg.mixin.impl;

import noppes.npcs.api.event.NpcEvent;
import noppes.npcs.entity.EntityNPCInterface;
Expand Down
Loading

0 comments on commit 773bacf

Please sign in to comment.