Skip to content

Commit

Permalink
Share both research and discovered aspects list with the knowledge sh…
Browse files Browse the repository at this point in the history
…aring tome (#41)

* Update buildscript, switch to RFG tag generation

* Share both research and discovered aspects list with the knowledge sharing book
  • Loading branch information
eigenraven authored Jul 17, 2024
1 parent 2058c30 commit 6670fcd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 48 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ enableGenericInjection = false
# Generate a class with a String field for the mod version named as defined below.
# If generateGradleTokenClass is empty or not missing, no such class will be generated.
# If gradleTokenVersion is empty or missing, the field will not be present in the class.
generateGradleTokenClass =
generateGradleTokenClass = thaumic.tinkerer.Tags

# Name of the token containing the project's current version to generate/replace.
gradleTokenVersion = GRADLETOKEN_VERSION
gradleTokenVersion = VERSION

# [DEPRECATED] Mod ID replacement token.
gradleTokenModId =
Expand All @@ -70,7 +70,7 @@ gradleTokenGroupName =
# The string's content will be replaced with your mod's version when compiled. You should use this to specify your mod's
# version in @Mod([...], version = VERSION, [...]).
# Leave these properties empty to skip individual token replacements.
replaceGradleTokenInFile = LibMisc.java
replaceGradleTokenInFile =

# In case your mod provides an API for other mods to implement you may declare its package here. Otherwise, you can
# leave this property empty.
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.22'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.23'
}


110 changes: 69 additions & 41 deletions src/main/java/thaumic/tinkerer/common/item/ItemShareBook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package thaumic.tinkerer.common.item;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -17,9 +16,12 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.api.aspects.AspectList;
import thaumcraft.api.research.ResearchPage;
import thaumcraft.common.Thaumcraft;
import thaumcraft.common.config.ConfigItems;
import thaumcraft.common.lib.research.PlayerKnowledge;
import thaumcraft.common.lib.research.ResearchManager;
import thaumic.tinkerer.common.ThaumicTinkerer;
import thaumic.tinkerer.common.core.handler.ConfigHandler;
Expand All @@ -36,7 +38,9 @@
public class ItemShareBook extends ItemBase {

private static final String TAG_PLAYER = "player";
private static final String NON_ASIGNED = "[none]";
private static final String TAG_RESEARCH = "research";
private static final String TAG_ASPECTS = "aspects";
private static final String NON_ASSIGNED = "[none]";

public ItemShareBook() {
super();
Expand Down Expand Up @@ -83,44 +87,57 @@ public ThaumicTinkererRecipe getRecipeItem() {
}

@Override
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) {
String name = getPlayerName(par1ItemStack);
if (name.endsWith(NON_ASIGNED)) {
setPlayerName(par1ItemStack, par3EntityPlayer.getGameProfile().getName());
setPlayerResearch(par1ItemStack, par3EntityPlayer.getGameProfile().getName());
if (!par2World.isRemote)
par3EntityPlayer.addChatMessage(new ChatComponentTranslation("ttmisc.shareTome.write"));
} else sync: {
List<String> researchesDone = ResearchManager.getResearchForPlayer(name);

if (researchesDone == null) {
if (par2World.isRemote) researchesDone = getPlayerResearch(par1ItemStack);
else {
par3EntityPlayer.addChatMessage(new ChatComponentTranslation("ttmisc.shareTome.sync"));
break sync;
}
public ItemStack onItemRightClick(ItemStack bookStack, World world, EntityPlayer player) {
String name = getPlayerName(bookStack);
if (name.endsWith(NON_ASSIGNED)) {
setPlayerName(bookStack, player.getGameProfile().getName());
savePlayerResearch(ItemNBTHelper.getNBT(bookStack), player.getGameProfile().getName());
if (!world.isRemote) {
player.addChatMessage(new ChatComponentTranslation("ttmisc.shareTome.write"));
}
} else {
// Try to load latest research for the saved player, fall back to the stored NBT if it fails.
final NBTTagCompound researchSource;
if (ResearchManager.getResearchForPlayer(name) != null) {
researchSource = new NBTTagCompound();
savePlayerResearch(researchSource, name);
} else {
researchSource = ItemNBTHelper.getNBT(bookStack);
}

for (String key : researchesDone)
ThaumicTinkerer.tcProxy.getResearchManager().completeResearch(par3EntityPlayer, key);
loadPlayerResearch(researchSource, player);

if (!par2World.isRemote)
par3EntityPlayer.addChatMessage(new ChatComponentTranslation("ttmisc.shareTome.sync"));
if (!world.isRemote) {
player.addChatMessage(new ChatComponentTranslation("ttmisc.shareTome.sync"));
}
}

return par1ItemStack;
return bookStack;
}

private List<String> getPlayerResearch(ItemStack par1ItemStack) {
List<String> retVals = new ArrayList<>();
NBTTagCompound cmp = ItemNBTHelper.getNBT(par1ItemStack);
if (!cmp.hasKey("research")) return retVals;
NBTTagList list = cmp.getTagList("research", Constants.NBT.TAG_STRING);
for (int i = 0; i < list.tagCount(); i++) {

retVals.add(list.getStringTagAt(i));
private void loadPlayerResearch(NBTTagCompound cmp, EntityPlayer player) {
// Ensure full TC player data gets loaded
final String playerName = player.getGameProfile().getName();
ResearchManager.getResearchForPlayer(playerName);
if (cmp.hasKey(TAG_RESEARCH)) {
final NBTTagList list = cmp.getTagList(TAG_RESEARCH, Constants.NBT.TAG_STRING);
for (int i = 0; i < list.tagCount(); i++) {
final String research = list.getStringTagAt(i);
ThaumicTinkerer.tcProxy.getResearchManager().completeResearch(player, research);
}
}
if (cmp.hasKey(TAG_ASPECTS)) {
final PlayerKnowledge pk = ThaumicTinkerer.tcProxy.getPlayerKnowledge();
final NBTTagList list = cmp.getTagList(TAG_ASPECTS, Constants.NBT.TAG_STRING);
for (int i = 0; i < list.tagCount(); i++) {
final String aspectTag = list.getStringTagAt(i);
final Aspect aspect = Aspect.getAspect(aspectTag);
if (aspect != null) {
pk.addDiscoveredAspect(playerName, aspect);
}
}
ResearchManager.scheduleSave(player);
}
return retVals;
}

@Override
Expand All @@ -134,7 +151,7 @@ public EnumRarity getRarity(ItemStack par1ItemStack) {
public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) {
String name = getPlayerName(par1ItemStack);
par3List.add(
name.equals(NON_ASIGNED) ? StatCollector.translateToLocal("ttmisc.shareTome.noAssign")
name.equals(NON_ASSIGNED) ? StatCollector.translateToLocal("ttmisc.shareTome.noAssign")
: String.format(StatCollector.translateToLocal("ttmisc.shareTome.playerName"), name));
}

Expand All @@ -144,21 +161,32 @@ public boolean getShareTag() {
}

private String getPlayerName(ItemStack stack) {
return ItemNBTHelper.getString(stack, TAG_PLAYER, NON_ASIGNED);
return ItemNBTHelper.getString(stack, TAG_PLAYER, NON_ASSIGNED);
}

private void setPlayerName(ItemStack stack, String playerName) {
ItemNBTHelper.setString(stack, TAG_PLAYER, playerName);
}

private void setPlayerResearch(ItemStack stack, String playername) {
List<String> researchesDone = ResearchManager.getResearchForPlayer(playername);
NBTTagCompound cmp = ItemNBTHelper.getNBT(stack);
NBTTagList list = new NBTTagList();
for (String tag : researchesDone) {
list.appendTag(new NBTTagString(tag));
private void savePlayerResearch(NBTTagCompound target, String playername) {
// also loads the aspect list
final List<String> researchesDone = ResearchManager.getResearchForPlayer(playername);
// Save all unlocked research notes.
final NBTTagList researchList = new NBTTagList();
for (final String tag : researchesDone) {
researchList.appendTag(new NBTTagString(tag));
}
target.setTag(TAG_RESEARCH, researchList);
// Save all discovered aspect types.
final PlayerKnowledge pk = Thaumcraft.proxy.getPlayerKnowledge();
final NBTTagList aspectsToSave = new NBTTagList();
final AspectList aspectsDiscovered = pk.getAspectsDiscovered(playername);
if (aspectsDiscovered == null) {
return;
}
cmp.setTag("research", list);
aspectsDiscovered.aspects.keySet().stream().map(Aspect::getTag).sorted().map(NBTTagString::new)
.forEach(aspectsToSave::appendTag);
target.setTag(TAG_ASPECTS, aspectsToSave);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/thaumic/tinkerer/common/lib/LibMisc.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
*/
package thaumic.tinkerer.common.lib;

import thaumic.tinkerer.Tags;

public final class LibMisc {

public static final String MOD_ID = "ThaumicTinkerer";
public static final String MOD_NAME = "Thaumic Tinkerer";
public static final String VERSION = "GRADLETOKEN_VERSION";
public static final String VERSION = Tags.VERSION;

public static final String NETWORK_CHANNEL = MOD_ID;

Expand Down

0 comments on commit 6670fcd

Please sign in to comment.