From 6d960d6c4195910ed772236387ab4414c0a2b558 Mon Sep 17 00:00:00 2001 From: yuesha-yc Date: Fri, 17 Jan 2025 23:41:35 -0800 Subject: [PATCH] Reformat TeamResearchData --- .../content/research/data/ClueData.java | 2 + .../content/research/data/ResearchData.java | 31 +- .../research/data/TeamResearchData.java | 429 +++++++++--------- .../command/ResearchCommand.java | 307 ++++++++----- src/main/resources/META-INF/mods.toml | 9 +- 5 files changed, 445 insertions(+), 333 deletions(-) diff --git a/src/main/java/com/teammoeg/frostedheart/content/research/data/ClueData.java b/src/main/java/com/teammoeg/frostedheart/content/research/data/ClueData.java index fb21a611f..853e1f92f 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/research/data/ClueData.java +++ b/src/main/java/com/teammoeg/frostedheart/content/research/data/ClueData.java @@ -23,8 +23,10 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import com.teammoeg.chorda.util.io.codec.CompressDifferCodec; +import lombok.ToString; import net.minecraft.nbt.CompoundTag; +@ToString public class ClueData{ public static final Codec FULL_CODEC=RecordCodecBuilder.create(t->t.group( Codec.BOOL.fieldOf("completed").forGetter(o->o.completed), diff --git a/src/main/java/com/teammoeg/frostedheart/content/research/data/ResearchData.java b/src/main/java/com/teammoeg/frostedheart/content/research/data/ResearchData.java index 1ce80a8ba..556f0fb7c 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/research/data/ResearchData.java +++ b/src/main/java/com/teammoeg/frostedheart/content/research/data/ResearchData.java @@ -31,6 +31,7 @@ import com.teammoeg.frostedheart.content.research.research.effects.Effect; import com.teammoeg.chorda.util.evaluator.IEnvironment; import com.teammoeg.chorda.util.io.CodecUtil; +import lombok.Getter; public class ResearchData implements IEnvironment { @@ -149,13 +150,41 @@ public ResearchDataPacket(int committed, boolean[] flags, int level, List clueData = new HashMap<>(); + @Getter private Map effectData = new HashMap<>(); - public static final Codec CODEC=RecordCodecBuilder.create(t->t.group( + + + public List getFieldNames() { + return List.of("active","finished","level","committed","clueData","effectData"); + } + + public Object getField(String key) { + return switch (key) { + case "active" -> active; + case "finished" -> finished; + case "level" -> level; + case "committed" -> committed; + case "clueData" -> clueData; + case "effectData" -> effectData; + default -> "no such field"; + }; + } + + @Override + public String toString() { + return "ResearchData [active=" + active + ", finished=" + finished + ", level=" + level + ", committed=" + committed + + ", clueData=" + clueData + ", effectData=" + effectData + "]"; + } + + public static final Codec CODEC=RecordCodecBuilder.create(t->t.group( Codec.INT.fieldOf("committed").forGetter(o->o.committed), CodecUtil.booleans("flags") .flag("active", o->o.active) diff --git a/src/main/java/com/teammoeg/frostedheart/content/research/data/TeamResearchData.java b/src/main/java/com/teammoeg/frostedheart/content/research/data/TeamResearchData.java index 2ef6702e2..c1f15c001 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/research/data/TeamResearchData.java +++ b/src/main/java/com/teammoeg/frostedheart/content/research/data/TeamResearchData.java @@ -19,42 +19,35 @@ package com.teammoeg.frostedheart.content.research.data; -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nullable; - import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; -import com.teammoeg.frostedheart.FHMain; import com.teammoeg.chorda.team.SpecialData; import com.teammoeg.chorda.team.SpecialDataHolder; import com.teammoeg.chorda.team.TeamDataHolder; import com.teammoeg.chorda.util.ie.IngredientUtils; +import com.teammoeg.chorda.util.io.CodecUtil; +import com.teammoeg.chorda.util.utility.OptionalLazy; +import com.teammoeg.frostedheart.FHMain; import com.teammoeg.frostedheart.content.research.FHResearch; import com.teammoeg.frostedheart.content.research.ResearchListeners.BlockUnlockList; import com.teammoeg.frostedheart.content.research.ResearchListeners.CategoryUnlockList; import com.teammoeg.frostedheart.content.research.ResearchListeners.MultiblockUnlockList; import com.teammoeg.frostedheart.content.research.ResearchListeners.RecipeUnlockList; import com.teammoeg.frostedheart.content.research.events.ResearchStatusEvent; -import com.teammoeg.frostedheart.content.research.network.FHChangeActiveResearchPacket; -import com.teammoeg.frostedheart.content.research.network.FHS2CClueProgressSyncPacket; -import com.teammoeg.frostedheart.content.research.network.FHEffectProgressSyncPacket; -import com.teammoeg.frostedheart.content.research.network.FHResearchAttributeSyncPacket; -import com.teammoeg.frostedheart.content.research.network.FHResearchDataSyncPacket; -import com.teammoeg.frostedheart.content.research.network.FHResearchDataUpdatePacket; +import com.teammoeg.frostedheart.content.research.network.*; import com.teammoeg.frostedheart.content.research.research.Research; import com.teammoeg.frostedheart.content.research.research.clues.Clue; import com.teammoeg.frostedheart.content.research.research.effects.Effect; -import com.teammoeg.chorda.util.io.CodecUtil; -import com.teammoeg.chorda.util.utility.OptionalLazy; - -import net.minecraft.server.level.ServerPlayer; import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.MinecraftForge; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + /** * Class TeamResearchData. @@ -65,68 +58,102 @@ * @author khjxiaogu * @date 2022/9/2 */ -public class TeamResearchData implements SpecialData{ +public class TeamResearchData implements SpecialData { + + public static final double GROWTH_RATE_MULTIPLIER = 0.5; + public static final Codec CODEC = RecordCodecBuilder.create(t -> + t.group( + CompoundTag.CODEC.fieldOf("vars").forGetter(o -> o.variants), + CodecUtil.discreteList(ResearchData.CODEC).fieldOf("researches").forGetter(o -> o.rdata), + CodecUtil.defaultValue(Codec.INT, 0).fieldOf("active").forGetter(o -> o.activeResearchId), + CodecUtil.defaultValue(Codec.INT, 0).fieldOf("insight").forGetter(o -> o.insight), + CodecUtil.defaultValue(Codec.INT, 0).fieldOf("insightLevel").forGetter(o -> o.insightLevel), + CodecUtil.defaultValue(Codec.INT, 0).fieldOf("usedInsightLevel").forGetter(o -> o.usedInsightLevel) + ).apply(t, TeamResearchData::new)); + /** + * The crafting.
+ */ + public RecipeUnlockList crafting = new RecipeUnlockList(); + /** + * The building.
+ */ + public MultiblockUnlockList building = new MultiblockUnlockList(); + /** + * The block.
+ */ + public BlockUnlockList block = new BlockUnlockList(); + /** + * The categories.
+ */ + public CategoryUnlockList categories = new CategoryUnlockList(); /** * The Insights point.
- * + *

* Insights are rewarded by active actions and new discoveries. - * + *

* Insights is monotone increasing. */ int insight = 0; /** * The Insights level.
- * + *

* Computed from insights point. - * + *

* Insights level is a monotone increasing function of insights point. */ int insightLevel = 0; /** * The used insights level.
- * + *

* Completing one research would increment this value. */ int usedInsightLevel = 0; - /** * The rdata.
*/ ArrayList rdata = new ArrayList<>(); - /** * The active research id.
*/ int activeResearchId = 0; - /** * The variants.
*/ CompoundTag variants = new CompoundTag(); + boolean isInited; + ; - + public TeamResearchData(SpecialDataHolder team) { + } - /** - * The crafting.
- */ - public RecipeUnlockList crafting = new RecipeUnlockList(); + public TeamResearchData(CompoundTag variants, List rdata, int activeResearchId, int insight, int insightLevel, int usedInsightLevel) { + super(); + crafting.clear(); + building.clear(); + block.clear(); + categories.clear(); + this.rdata.addAll(rdata); + this.activeResearchId = activeResearchId; + this.insight = insight; + this.insightLevel = insightLevel; + this.usedInsightLevel = usedInsightLevel; + this.variants = variants; - /** - * The building.
- */ - public MultiblockUnlockList building = new MultiblockUnlockList(); + } /** - * The block.
+ * Used to compute the insights level from insights point. + * TODO: The function is placeholder. Should be adjusted. + * + * @param insights insights point */ - public BlockUnlockList block = new BlockUnlockList(); + public static int computeLevelFromInsight(int insights) { + return (int) Math.floor(Math.sqrt(GROWTH_RATE_MULTIPLIER * insights + 9) - 3); + } - /** - * The categories.
- */ - public CategoryUnlockList categories = new CategoryUnlockList(); - public TeamResearchData(SpecialDataHolder team) { + public static int computeInsightFromLevel(int level) { + return (int) Math.ceil(((level + 3) * (level + 3) - 9) / GROWTH_RATE_MULTIPLIER); } /** @@ -142,23 +169,26 @@ public boolean canResearch() { } return false; } - public void setHolder(SpecialDataHolder holder) {}; + + public void setHolder(SpecialDataHolder holder) { + } + /** * Clear current research. * * @param sync send update packet */ - public void clearCurrentResearch(TeamDataHolder team,boolean sync) { + public void clearCurrentResearch(TeamDataHolder team, boolean sync) { if (activeResearchId == 0) return; Research r = FHResearch.researches.getById(activeResearchId); activeResearchId = 0; if (r != null) - if (team!=null) { - for (Clue c : r.getClues()) - c.end(team,r); - if(sync) - team.sendToOnline(new FHChangeActiveResearchPacket()); - } + if (team != null) { + for (Clue c : r.getClues()) + c.end(team, r); + if (sync) + team.sendToOnline(new FHChangeActiveResearchPacket()); + } } /** @@ -169,81 +199,84 @@ public void clearCurrentResearch(TeamDataHolder team,boolean sync) { @OnlyIn(Dist.CLIENT) public void clearCurrentResearch(Research r) { if (activeResearchId == FHResearch.researches.getIntId(r)) - activeResearchId=0; + activeResearchId = 0; } - /** * Commit research points to current research.
* * @param points the points
* @return unused points after commit to current research. */ - public long doResearch(TeamDataHolder team,long points) { + public long doResearch(TeamDataHolder team, long points) { OptionalLazy rs = getCurrentResearch(); if (rs.isPresent()) { Research r = rs.resolve().get(); ResearchData rd = this.getData(r); if (!rd.active || rd.finished) return points; - return rd.commitPoints(r, points, ()->{ - checkResearchComplete(team,r); - sendResearchProgressPacket(team, r); + return rd.commitPoints(r, points, () -> { + checkResearchComplete(team, r); + sendResearchProgressPacket(team, r); }); } return points; } - public boolean commitItem(ServerPlayer player,TeamDataHolder team,Research research) { + + public boolean commitItem(ServerPlayer player, TeamDataHolder team, Research research) { if (research.isInCompletable()) return false; for (Research par : research.getParents()) { if (!getData(par).isCompleted()) { return false; } } - if(!research.getRequiredItems().isEmpty()&&!IngredientUtils.costItems(player,research.getRequiredItems())) - return false; + if (!research.getRequiredItems().isEmpty() && !IngredientUtils.costItems(player, research.getRequiredItems())) + return false; getData(research).setActive(); this.sendResearchProgressPacket(team, research); return true; } - public boolean isClueCompleted(Research par,Clue clue) { - return getData(par).isClueTriggered(clue); - } - public void setClueCompleted(TeamDataHolder team,Research par,int clue, boolean trig) { - - Clue cl=par.getClues().get(clue); - if(cl!=null) { - if(isClueCompleted(par,cl)==trig)return; - getData(par).setClueTriggered(cl.getNonce(), trig); - if (trig) - cl.end(team,par); - else - cl.start(team,par); - sendClueProgressPacket(team,par,clue,trig); - } - } - public void setClueCompleted(TeamDataHolder team,Research par,Clue clue, boolean trig) { - if(isClueCompleted(par,clue)==trig)return; - getData(par).setClueTriggered(clue.getNonce(), trig); + + public boolean isClueCompleted(Research par, Clue clue) { + return getData(par).isClueTriggered(clue); + } + + public void setClueCompleted(TeamDataHolder team, Research par, int clue, boolean trig) { + + Clue cl = par.getClues().get(clue); + if (cl != null) { + if (isClueCompleted(par, cl) == trig) return; + getData(par).setClueTriggered(cl.getNonce(), trig); + if (trig) + cl.end(team, par); + else + cl.start(team, par); + sendClueProgressPacket(team, par, clue, trig); + } + } + + public void setClueCompleted(TeamDataHolder team, Research par, Clue clue, boolean trig) { + if (isClueCompleted(par, clue) == trig) return; + getData(par).setClueTriggered(clue.getNonce(), trig); if (trig) - clue.end(team,par); + clue.end(team, par); else - clue.start(team,par); - sendClueProgressPacket(team,par,par.getClues().indexOf(clue),trig); + clue.start(team, par); + sendClueProgressPacket(team, par, par.getClues().indexOf(clue), trig); } - public boolean isEffectGranted(Research research, Effect e) { - return getData(research).isEffectGranted(e); - } + public boolean isEffectGranted(Research research, Effect e) { + return getData(research).isEffectGranted(e); + } - public void setEffectGranted(Research rch, Effect effect, boolean b) { - - getData(rch).setEffectGranted(effect, b); - - } + public void setEffectGranted(Research rch, Effect effect, boolean b) { + + getData(rch).setEffectGranted(effect, b); + + } public boolean checkResearchComplete(TeamDataHolder team, Research r) { - ResearchData rd=getData(r); + ResearchData rd = getData(r); if (rd.finished) return false; boolean flag = true; @@ -256,34 +289,39 @@ public boolean checkResearchComplete(TeamDataHolder team, Research r) { if (rd.getTotalCommitted(r) >= r.getRequiredPoints() && flag) { rd.setFinished(true); this.grantEffects(team, null, r); - - this.annouceResearchComplete(team,r); + + this.annouceResearchComplete(team, r); return true; } return false; } - public void setResearchFinished(TeamDataHolder team,Research rs,boolean data) { - ResearchData rd=getData(rs); - //System.out.println(rs); - rd.setFinished(data); - - if(data) { - this.grantEffects(team, null, rs); - this.sendResearchProgressPacket(team, rs); - this.annouceResearchComplete(team,rs); - } + + public void setResearchFinished(TeamDataHolder team, Research rs, boolean data) { + ResearchData rd = getData(rs); + //System.out.println(rs); + rd.setFinished(data); + + if (data) { + this.grantEffects(team, null, rs); + this.sendResearchProgressPacket(team, rs); + this.annouceResearchComplete(team, rs); + } } + /** - * Grant effects. + * Actually grants the Research's active Effects to the team's player. + *

+ * This must be called after an Effect is set to active (true). * - * @param team the team
- * @param spe the spe
+ * @param spe the Player to grant the effects to + * @param rs the Research to grant the effects of + * @param holder the TeamDataHolder */ - public void grantEffects(TeamDataHolder holder, ServerPlayer spe,Research rs) { + public void grantEffects(TeamDataHolder holder, ServerPlayer spe, Research rs) { boolean granted = true; - ResearchData rd=getData(rs); + ResearchData rd = getData(rs); for (Effect e : rs.getEffects()) { - grantEffect(holder,rs,e, spe); + grantEffect(holder, rs, e, spe); granted &= rd.isEffectGranted(e); } if (rs.isInfinite() && granted) { @@ -293,9 +331,11 @@ public void grantEffects(TeamDataHolder holder, ServerPlayer spe,Research rs) { } } + private void sendClueProgressPacket(TeamDataHolder team, Research par, int clue, boolean trig) { - team.sendToOnline(new FHS2CClueProgressSyncPacket(trig,FHResearch.researches.getIntId(par),clue)); - } + team.sendToOnline(new FHS2CClueProgressSyncPacket(trig, FHResearch.researches.getIntId(par), clue)); + } + /** * Send effect progress packet for current effect to players in team. * Useful for data sync. This would called automatically, Their's no need to call this in effect. @@ -303,16 +343,19 @@ private void sendClueProgressPacket(TeamDataHolder team, Research par, int clue, * @param team the team
*/ private void sendEffectProgressPacket(TeamDataHolder team, Research par, int effect, boolean trig) { - FHEffectProgressSyncPacket packet = new FHEffectProgressSyncPacket(trig,FHResearch.researches.getIntId(par),effect); + FHEffectProgressSyncPacket packet = new FHEffectProgressSyncPacket(trig, FHResearch.researches.getIntId(par), effect); team.sendToOnline(packet); } + private void annouceResearchComplete(TeamDataHolder team, Research par) { - team.getTeam().ifPresent(e -> MinecraftForge.EVENT_BUS.post(new ResearchStatusEvent(par, e, getData(par).finished))); + team.getTeam().ifPresent(e -> MinecraftForge.EVENT_BUS.post(new ResearchStatusEvent(par, e, getData(par).finished))); } + private void sendResearchProgressPacket(TeamDataHolder team, Research par) { - team.sendToOnline(new FHResearchDataUpdatePacket(par,getData(par))); + team.sendToOnline(new FHResearchDataUpdatePacket(par, getData(par))); } - /** + + /** * Ensure clue data length. * * @param len the len
@@ -334,8 +377,8 @@ public void ensureEffect(int len) { * @param len the len
*/ public void ensureResearch(int len) { - rdata.ensureCapacity(len+1); - while (rdata.size() < len+1) + rdata.ensureCapacity(len + 1); + while (rdata.size() < len + 1) rdata.add(null); } @@ -350,6 +393,11 @@ public OptionalLazy getCurrentResearch() { return OptionalLazy.of(() -> FHResearch.getResearch(activeResearchId)); } + @OnlyIn(Dist.CLIENT) + public void setCurrentResearch(int id) { + this.activeResearchId = id; + } + /** * Get research data. * @@ -359,19 +407,21 @@ public OptionalLazy getCurrentResearch() { public ResearchData getData(int id) { if (id < 0) return null; ensureResearch(id); - ResearchData rnd = rdata.get(id ); + ResearchData rnd = rdata.get(id); if (rnd == null) { rnd = new ResearchData(); - rdata.set(id , rnd); + rdata.set(id, rnd); } return rnd; } - public ResearchData setData(int id,ResearchData rd) { + + public ResearchData setData(int id, ResearchData rd) { if (id <= 0) return null; ensureResearch(id); - ResearchData rnd = rdata.set(id,rd); + ResearchData rnd = rdata.set(id, rd); return rnd; } + /** * Get research data. * @@ -393,13 +443,16 @@ public ResearchData getData(String lid) { return getData(FHResearch.researches.getByName(lid)); } - - - public double getVariantDouble(ResearchVariant name) { return variants.getDouble(name.getToken()); } +/*d + public void reload() { + crafting.reload(); + building.reload(); + sendUpdate(); + }*/ public long getVariantLong(ResearchVariant name) { return variants.getLong(name.getToken()); @@ -419,29 +472,33 @@ public CompoundTag getVariants() { return variants; } + public void setVariants(CompoundTag variants) { + this.variants = variants; + } + /** * Grant effect to the team, optionally to a player. Sending packets and run {@link Effect#grant(TeamResearchData, net.minecraft.entity.player.PlayerEntity, boolean)} * * @param e the e
* @param player the player, only useful when player manually click "claim awards" or do similar things.
*/ - public void grantEffect(TeamDataHolder team,Research r,Effect e, @Nullable ServerPlayer player) { + public void grantEffect(TeamDataHolder team, Research r, Effect e, @Nullable ServerPlayer player) { if (!this.isEffectGranted(r, e)) { - e.grant(team,this, player, false); + e.grant(team, this, player, false); this.setEffectGranted(r, e, true); - sendEffectProgressPacket(team,r,r.getEffects().indexOf(e),true); + sendEffectProgressPacket(team, r, r.getEffects().indexOf(e), true); } } public void sendVariantPacket(TeamDataHolder holder) { - FHResearchAttributeSyncPacket pack=new FHResearchAttributeSyncPacket(variants); - holder.sendToOnline(pack); + FHResearchAttributeSyncPacket pack = new FHResearchAttributeSyncPacket(variants); + holder.sendToOnline(pack); } + public boolean hasVariant(ResearchVariant name) { return variants.contains(name.getToken()); } - public void putVariantDouble(ResearchVariant name, double val) { variants.putDouble(name.getToken(), val); } @@ -449,12 +506,6 @@ public void putVariantDouble(ResearchVariant name, double val) { public void putVariantLong(ResearchVariant name, long val) { variants.putLong(name.getToken(), val); } -/*d - public void reload() { - crafting.reload(); - building.reload(); - sendUpdate(); - }*/ public void removeVariant(ResearchVariant name) { variants.remove(name.getToken()); @@ -465,9 +516,9 @@ public void removeVariant(ResearchVariant name) { * * @param r the r
*/ - public void resetData(TeamDataHolder team,Research r) { - ResearchData rd=getData(r); - rd.reset(); + public void resetData(TeamDataHolder team, Research r) { + ResearchData rd = getData(r); + rd.reset(); /*int i=0; for (Clue c : r.getClues()) { @@ -483,55 +534,44 @@ public void resetData(TeamDataHolder team,Research r) { sendEffectProgressPacket(team,r, i++, false); } }*/ - if (team!=null) { - for (Clue c : r.getClues()) { - c.end(team, r); + if (team != null) { + for (Clue c : r.getClues()) { + c.end(team, r); } this.sendResearchProgressPacket(team, r); } - + } public void sendUpdate(TeamDataHolder team) { FHResearchDataSyncPacket packet = new FHResearchDataSyncPacket(this); team.sendToOnline(packet); - - } - - - @OnlyIn(Dist.CLIENT) - public void setCurrentResearch(int id) { - this.activeResearchId=id; } + /** * set current research. * * @param r value to set current research to. */ - public void setCurrentResearch(TeamDataHolder team,Research r) { + public void setCurrentResearch(TeamDataHolder team, Research r) { ResearchData rd = this.getData(r); - int index=FHResearch.researches.getIntId(r); + int index = FHResearch.researches.getIntId(r); if (rd.active && !rd.finished) { if (this.activeResearchId != index) { if (this.activeResearchId != 0) - clearCurrentResearch(team,false); + clearCurrentResearch(team, false); this.activeResearchId = index; FHChangeActiveResearchPacket packet = new FHChangeActiveResearchPacket(r); team.sendToOnline(packet); for (Clue c : r.getClues()) - c.start(team,r); - - this.checkResearchComplete(team,r); + c.start(team, r); + + this.checkResearchComplete(team, r); } } } - - public void setVariants(CompoundTag variants) { - this.variants = variants; - } - public boolean setInsight(int insight) { this.insight = insight; int newLevel = computeLevelFromInsight(this.insight); @@ -582,22 +622,10 @@ public int getUsedInsightLevel() { return usedInsightLevel; } - /** - * Used to compute the insights level from insights point. - * TODO: The function is placeholder. Should be adjusted. - * @param insights insights point - */ - public static final double GROWTH_RATE_MULTIPLIER = 0.5; - public static int computeLevelFromInsight(int insights) { - return (int) Math.floor(Math.sqrt(GROWTH_RATE_MULTIPLIER * insights + 9) - 3); - } - public static int computeInsightFromLevel(int level) { - return (int) Math.ceil( ((level + 3) * (level + 3) - 9) / GROWTH_RATE_MULTIPLIER); - } - /** * Add insights point to the team. * Increment level if needed. + * * @param newInsights insights point to add * @return whether the level is changed. */ @@ -613,8 +641,9 @@ public boolean addInsight(int newInsights) { /** * Get the fraction of how much insights point is needed to reach next level. - * @return a float in [0, 1) * + * @return a float in [0, 1) + *

* TODO: Fix somehow insight is always synced to the currentLevelInsights */ public float getInsightProgress() { @@ -626,6 +655,7 @@ public float getInsightProgress() { /** * Get available insights level. + * * @return available insights level */ public int getAvailableInsightLevel() { @@ -637,53 +667,24 @@ public int getAvailableInsightLevel() { return level; } - public static final Codec CODEC=RecordCodecBuilder.create(t-> - t.group( - CompoundTag.CODEC.fieldOf("vars").forGetter(o->o.variants), - CodecUtil.discreteList(ResearchData.CODEC).fieldOf("researches").forGetter(o->o.rdata), - CodecUtil.defaultValue(Codec.INT, 0).fieldOf("active").forGetter(o->o.activeResearchId), - CodecUtil.defaultValue(Codec.INT, 0).fieldOf("insight").forGetter(o->o.insight), - CodecUtil.defaultValue(Codec.INT, 0).fieldOf("insightLevel").forGetter(o->o.insightLevel), - CodecUtil.defaultValue(Codec.INT, 0).fieldOf("usedInsightLevel").forGetter(o->o.usedInsightLevel) -// Codec.INT.fieldOf("active").forGetter(o->o.activeResearchId), -// Codec.INT.fieldOf("insight").forGetter(o->o.insight), -// Codec.INT.fieldOf("insightLevel").forGetter(o->o.insightLevel), -// Codec.INT.fieldOf("usedInsightLevel").forGetter(o->o.usedInsightLevel) - ).apply(t, TeamResearchData::new)); - - boolean isInited; - public TeamResearchData( CompoundTag variants, List rdata, int activeResearchId, int insight, int insightLevel, int usedInsightLevel) { - super(); - crafting.clear(); - building.clear(); - block.clear(); - categories.clear(); - this.rdata.addAll(rdata); - this.activeResearchId = activeResearchId; - this.insight = insight; - this.insightLevel = insightLevel; - this.usedInsightLevel = usedInsightLevel; - this.variants = variants; - - } - - public void initResearch(TeamDataHolder team) { - for (Research r:FHResearch.getAllResearch()) { - ResearchData rd=getData(r); - if(rd.isCompleted()) { - for(Effect e:r.getEffects()) - e.grant(team,this, null, true); + public void initResearch(TeamDataHolder team) { + for (Research r : FHResearch.getAllResearch()) { + ResearchData rd = getData(r); + if (rd.isCompleted()) { + for (Effect e : r.getEffects()) + e.grant(team, this, null, true); } } - + if (activeResearchId != 0) { Research r = FHResearch.researches.getById(activeResearchId); - + for (Clue c : r.getClues()) - c.start(team,r); - + c.start(team, r); + } - } + } + public void putVariantDouble(String name, double val) { variants.putDouble(name, val); } diff --git a/src/main/java/com/teammoeg/frostedheart/infrastructure/command/ResearchCommand.java b/src/main/java/com/teammoeg/frostedheart/infrastructure/command/ResearchCommand.java index 430efc3bb..bcdbec1c3 100644 --- a/src/main/java/com/teammoeg/frostedheart/infrastructure/command/ResearchCommand.java +++ b/src/main/java/com/teammoeg/frostedheart/infrastructure/command/ResearchCommand.java @@ -26,24 +26,22 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.teammoeg.chorda.team.CTeamDataManager; +import com.teammoeg.chorda.team.TeamDataClosure; import com.teammoeg.chorda.util.lang.Components; import com.teammoeg.frostedheart.FHMain; -import com.teammoeg.chorda.team.TeamDataClosure; import com.teammoeg.frostedheart.content.research.FHResearch; import com.teammoeg.frostedheart.content.research.api.ResearchDataAPI; import com.teammoeg.frostedheart.content.research.data.TeamResearchData; -import com.teammoeg.frostedheart.content.research.inspire.EnergyCore; import com.teammoeg.frostedheart.content.research.research.Research; - import dev.ftb.mods.ftbteams.api.FTBTeamsAPI; import dev.ftb.mods.ftbteams.api.Team; +import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.commands.arguments.NbtTagArgument; import net.minecraft.commands.arguments.UuidArgument; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; -import net.minecraft.ChatFormatting; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -54,108 +52,113 @@ public class ResearchCommand { public static void register(RegisterCommandsEvent event) { CommandDispatcher dispatcher = event.getDispatcher(); LiteralArgumentBuilder add = Commands.literal("research") - // add insight - .then(Commands.literal("insight").then(Commands.literal("add").then(Commands.argument("amount", IntegerArgumentType.integer(0)).executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - trd.addInsight(ct.getArgument("amount", Integer.class)); - ct.getSource().sendSuccess(()-> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - }))) + // Insight + .then(Commands.literal("insight") + // add insight + .then(Commands.literal("add").then(Commands.argument("amount", IntegerArgumentType.integer(0)).executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + trd.addInsight(ct.getArgument("amount", Integer.class)); + ct.getSource().sendSuccess(() -> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + }))) // get insight - .then(Commands.literal("get").executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - ct.getSource().sendSuccess(()-> Components.str("Insight: " + trd.getInsight()).withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - })) + .then(Commands.literal("get").executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + ct.getSource().sendSuccess(() -> Components.str("Insight: " + trd.getInsight()).withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + })) // Get insight level - .then(Commands.literal("getLevel").executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - ct.getSource().sendSuccess(()-> Components.str("Insight Level: " + trd.getInsightLevel()).withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - })) + .then(Commands.literal("getLevel").executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + ct.getSource().sendSuccess(() -> Components.str("Insight Level: " + trd.getInsightLevel()).withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + })) // Get used insight level - .then(Commands.literal("getUsedLevel").executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - ct.getSource().sendSuccess(()-> Components.str("Used Insight Level: " + trd.getUsedInsightLevel()).withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - })) + .then(Commands.literal("getUsedLevel").executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + ct.getSource().sendSuccess(() -> Components.str("Used Insight Level: " + trd.getUsedInsightLevel()).withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + })) // set insight - .then(Commands.literal("set").then(Commands.argument("amount", IntegerArgumentType.integer(0)).executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - trd.setInsight(ct.getArgument("amount", Integer.class)); - ct.getSource().sendSuccess(()-> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - }))) + .then(Commands.literal("set").then(Commands.argument("amount", IntegerArgumentType.integer(0)).executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + trd.setInsight(ct.getArgument("amount", Integer.class)); + ct.getSource().sendSuccess(() -> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + }))) // set insight level - .then(Commands.literal("setLevel").then(Commands.argument("level", IntegerArgumentType.integer(0)).executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - trd.setInsightLevel(ct.getArgument("level", Integer.class)); - ct.getSource().sendSuccess(()-> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - }))) + .then(Commands.literal("setLevel").then(Commands.argument("level", IntegerArgumentType.integer(0)).executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + trd.setInsightLevel(ct.getArgument("level", Integer.class)); + ct.getSource().sendSuccess(() -> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + }))) // set used insight level - .then(Commands.literal("setUsedLevel").then(Commands.argument("level", IntegerArgumentType.integer(0)).executes(ct -> { - TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); - trd.setUsedInsightLevel(ct.getArgument("level", Integer.class)); - ct.getSource().sendSuccess(()-> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - }))) - + .then(Commands.literal("setUsedLevel").then(Commands.argument("level", IntegerArgumentType.integer(0)).executes(ct -> { + TeamResearchData trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()).get(); + trd.setUsedInsightLevel(ct.getArgument("level", Integer.class)); + ct.getSource().sendSuccess(() -> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + }))) ) - .then(Commands.literal("complete").then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { - for (Research r : FHResearch.getAllResearch()) - if (r.getId().startsWith(s.getRemaining())) - s.suggest(r.getId(), r.getName()); - return s.buildFuture(); - }).executes(ct -> { - String rsn = ct.getArgument("name", String.class); - - Research rs = FHResearch.getResearch(rsn); - if (rs == null) { - ct.getSource().sendFailure(Components.str("Research not found").withStyle(ChatFormatting.RED)); - return Command.SINGLE_SUCCESS; - } - TeamDataClosure rd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); - rd.get().setResearchFinished(rd.team(), rs, true); - ct.getSource().sendSuccess(()-> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - })).then(Commands.literal("all").executes(ct -> { - TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); - try { - for (Research r : FHResearch.getAllResearch()) { - if (r.isInCompletable()) continue; - trd.get().setResearchFinished(trd.team(), r, true); - } - }catch(Throwable t) {t.printStackTrace();} - ct.getSource().sendSuccess(()-> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); - - return Command.SINGLE_SUCCESS; - }))) - .then(Commands.literal("transfer").then(Commands.argument("from", UuidArgument.uuid()) - .then(Commands.argument("to", UuidArgument.uuid())).executes(ct -> { + // Complete + .then(Commands.literal("complete") + // by name + .then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { + for (Research r : FHResearch.getAllResearch()) + if (r.getId().startsWith(s.getRemaining())) + s.suggest(r.getId(), r.getName()); + return s.buildFuture(); + }).executes(ct -> { + String rsn = ct.getArgument("name", String.class); + + Research rs = FHResearch.getResearch(rsn); + if (rs == null) { + ct.getSource().sendFailure(Components.str("Research not found").withStyle(ChatFormatting.RED)); + return Command.SINGLE_SUCCESS; + } + TeamDataClosure rd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); + rd.get().setResearchFinished(rd.team(), rs, true); + ct.getSource().sendSuccess(() -> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + })) + // all + .then(Commands.literal("all").executes(ct -> { + TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); + try { + for (Research r : FHResearch.getAllResearch()) { + if (r.isInCompletable()) continue; + trd.get().setResearchFinished(trd.team(), r, true); + } + } catch (Throwable t) { + t.printStackTrace(); + } + ct.getSource().sendSuccess(() -> Components.str("Succeed!").withStyle(ChatFormatting.GREEN), false); + + return Command.SINGLE_SUCCESS; + }))) + // Transfer + .then(Commands.literal("transfer") + .then(Commands.argument("from", UuidArgument.uuid()).then(Commands.argument("to", UuidArgument.uuid())).executes(ct -> { Team team = FTBTeamsAPI.api().getManager().getTeamByID(UuidArgument.getUuid(ct, "to")).orElse(null); CTeamDataManager.INSTANCE.transfer(UuidArgument.getUuid(ct, "from"), team); - ct.getSource().sendSuccess(()-> Components.str("Transfered to " + team.getName()).withStyle(ChatFormatting.GREEN), false); + ct.getSource().sendSuccess(() -> Components.str("Transfered to " + team.getName()).withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + }))) + // Edit + .then(Commands.literal("edit") + .then(Commands.argument("enable", BoolArgumentType.bool()).executes(ct -> { + FHResearch.editor = ct.getArgument("enable", Boolean.class); + ct.getSource().sendSuccess(() -> Components.str("Editing mode set " + FHResearch.editor).withStyle(ChatFormatting.GREEN), false); return Command.SINGLE_SUCCESS; }))) - .then(Commands.literal("edit").then(Commands.argument("enable", BoolArgumentType.bool()).executes(ct -> { - FHResearch.editor = ct.getArgument("enable", Boolean.class); - ct.getSource().sendSuccess(()-> Components.str("Editing mode set " + FHResearch.editor).withStyle(ChatFormatting.GREEN), false); - return Command.SINGLE_SUCCESS; - }))) + // Default .then(Commands.literal("default").executes(ct -> Command.SINGLE_SUCCESS)) - .then(Commands.literal("energy").executes(ct -> { - EnergyCore.reportEnergy(ct.getSource().getPlayerOrException()); - return Command.SINGLE_SUCCESS; - }).then(Commands.literal("add").then(Commands.argument("amount", IntegerArgumentType.integer(0)).executes(ct -> { - - EnergyCore.addPoint(ct.getSource().getPlayerOrException(), ct.getArgument("amount", Integer.class)); - return Command.SINGLE_SUCCESS; - })))) - - .then(Commands.literal("attribute").then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { + // Attribute + .then(Commands.literal("attribute") + .then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { CompoundTag cnbt = ResearchDataAPI.getVariants(ct.getSource().getPlayerOrException()); cnbt.getAllKeys().forEach(s::suggest); return s.buildFuture(); @@ -163,16 +166,16 @@ public static void register(RegisterCommandsEvent event) { }).executes(ct -> { CompoundTag cnbt = ResearchDataAPI.getVariants(ct.getSource().getPlayerOrException()); String rsn = ct.getArgument("name", String.class); - ct.getSource().sendSuccess(()-> Components.str(String.valueOf(cnbt.get(rsn))), false); + ct.getSource().sendSuccess(() -> Components.str(String.valueOf(cnbt.get(rsn))), false); return Command.SINGLE_SUCCESS; - })).then(Commands.literal("all").executes(ct -> { + })) + .then(Commands.literal("all").executes(ct -> { CompoundTag cnbt = ResearchDataAPI.getVariants(ct.getSource().getPlayerOrException()); - ct.getSource().sendSuccess(()-> Components.str(cnbt.toString()), false); + ct.getSource().sendSuccess(() -> Components.str(cnbt.toString()), false); return Command.SINGLE_SUCCESS; })) - .then(Commands.literal("set").then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { CompoundTag cnbt = ResearchDataAPI.getVariants(ct.getSource().getPlayerOrException()); cnbt.getAllKeys().forEach(s::suggest); @@ -188,27 +191,97 @@ public static void register(RegisterCommandsEvent event) { cnbt.put(rsn, nbt); ResearchDataAPI.sendVariants(ct.getSource().getPlayerOrException()); return Command.SINGLE_SUCCESS; - }))))) - .then(Commands.literal("reset").then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { - for (Research r : FHResearch.getAllResearch()) - if (r.getId().startsWith(s.getRemaining())) - s.suggest(r.getId(), r.getName()); - return s.buildFuture(); - }).executes(ct -> { - String rsn = ct.getArgument("name", String.class); - TeamDataClosure trd=ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); - - trd.get().resetData(trd.team(),FHResearch.getResearch(rsn)); - return Command.SINGLE_SUCCESS; - })).then(Commands.literal("all").executes(ct -> { - try { - TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); - for (Research r : FHResearch.getAllResearch()) { - trd.get().resetData(trd.team(),r); - } - }catch(Throwable t) {t.printStackTrace();} - return Command.SINGLE_SUCCESS; - }))); + })))) + ) + // Reset + .then(Commands.literal("reset") + // by name + .then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { + for (Research r : FHResearch.getAllResearch()) + if (r.getId().startsWith(s.getRemaining())) + s.suggest(r.getId(), r.getName()); + return s.buildFuture(); + }).executes(ct -> { + String rsn = ct.getArgument("name", String.class); + TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); + + trd.get().resetData(trd.team(), FHResearch.getResearch(rsn)); + return Command.SINGLE_SUCCESS; + })) + // all + .then(Commands.literal("all").executes(ct -> { + try { + TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); + for (Research r : FHResearch.getAllResearch()) { + trd.get().resetData(trd.team(), r); + } + } catch (Throwable t) { + t.printStackTrace(); + } + return Command.SINGLE_SUCCESS; + })) + ) + // Get Research Information: + // keyword: "info" + // then, get research information by name + // keyword: "name" + .then(Commands.literal("info") + .then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { + for (Research r : FHResearch.getAllResearch()) + if (r.getId().startsWith(s.getRemaining())) + s.suggest(r.getId(), r.getName()); + return s.buildFuture(); + }) + .executes(ct -> { + String rsn = ct.getArgument("name", String.class); + Research rs = FHResearch.getResearch(rsn); + if (rs == null) { + ct.getSource().sendFailure(Components.str("Research not found").withStyle(ChatFormatting.RED)); + return Command.SINGLE_SUCCESS; + } + TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); + ct.getSource().sendSuccess(() -> rs.getName().copy().append(Components.str( ": " + trd.get().getData(rs))), false); + return Command.SINGLE_SUCCESS; + })) + ) + + // Get more detailed sub-information by getters + // keyword: "get" + // then, get research information by name + // keyword: "name" + // then, get sub-information by getter + // keyword: "field_name" + .then(Commands.literal("get") + .then(Commands.argument("name", StringArgumentType.string()).suggests((ct, s) -> { + for (Research r : FHResearch.getAllResearch()) + if (r.getId().startsWith(s.getRemaining())) + s.suggest(r.getId(), r.getName()); + return s.buildFuture(); + }) + .then(Commands.argument("field_name", StringArgumentType.string()).suggests((ct, s) -> { + Research rs = FHResearch.getResearch(ct.getArgument("name", String.class)); + if (rs == null) return s.buildFuture(); + for (String key : rs.getData().getFieldNames()) + if (key.startsWith(s.getRemaining())) + s.suggest(key); + return s.buildFuture(); + }) + .executes(ct -> { + String rsn = ct.getArgument("name", String.class); + String field = ct.getArgument("field_name", String.class); + Research rs = FHResearch.getResearch(rsn); + if (rs == null) { + ct.getSource().sendFailure(Components.str("Research not found").withStyle(ChatFormatting.RED)); + return Command.SINGLE_SUCCESS; + } + TeamDataClosure trd = ResearchDataAPI.getData(ct.getSource().getPlayerOrException()); + ct.getSource().sendSuccess(() -> Components.str(rs.getName() + ": " + trd.get().getData(rs).getField(field)).withStyle(ChatFormatting.GREEN), false); + return Command.SINGLE_SUCCESS; + })) + )); + + + // Register for (String string : new String[]{FHMain.MODID, FHMain.ALIAS, FHMain.TWRID}) { dispatcher.register(Commands.literal(string).requires(s -> s.hasPermission(2)).then(add)); } diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index eaafb8d67..f836ec117 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -63,6 +63,13 @@ See our official website. ordering = "NONE" side = "BOTH" +[[dependencies.frostedheart]] + modId="chorda" + mandatory=true + versionRange="[0.1.0,)" + ordering="BEFORE" + side="BOTH" + [[dependencies.frostedheart]] modId="immersiveengineering" mandatory=true @@ -136,7 +143,7 @@ See our official website. [[mods]] #mandatory modId = "chorda" #mandatory -version = "${file.jarVersion}" #mandatory +version = "0.1.0" #mandatory displayName = "Chorda" #mandatory displayURL="https://www.curseforge.com/minecraft/mc-mods/chorda" logoFile="logo.png"