-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scripted dialogs, UI for modifying additional npc data and availa…
…bility level check
- Loading branch information
1 parent
842d726
commit 773bacf
Showing
20 changed files
with
556 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/ru/glassspirit/cnpcntrpg/forge/DataNpcRpg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/ru/glassspirit/cnpcntrpg/mixin/IMixinDataStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/ru/glassspirit/cnpcntrpg/mixin/IMixinDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/ru/glassspirit/cnpcntrpg/mixin/IMixinDialogOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/ru/glassspirit/cnpcntrpg/mixin/impl/MixinAvailablilty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...irit/cnpcntrpg/mixin/MixinCustomNpcs.java → ...cnpcntrpg/mixin/impl/MixinCustomNpcs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...t/cnpcntrpg/mixin/MixinDataInventory.java → ...cntrpg/mixin/impl/MixinDataInventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/ru/glassspirit/cnpcntrpg/mixin/impl/MixinDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/ru/glassspirit/cnpcntrpg/mixin/impl/MixinDialogOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...cntrpg/mixin/MixinEntityNPCInterface.java → ...g/mixin/impl/MixinEntityNPCInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.