Skip to content

Commit

Permalink
ConsortReputation attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
kirderf1 committed Apr 20, 2024
1 parent 5fb11fc commit c3a7c6a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public AnimatableInstanceCache getAnimatableInstanceCache()

private boolean shouldFleeFrom(LivingEntity entity)
{
return entity instanceof ServerPlayer && EntitySelector.NO_CREATIVE_OR_SPECTATOR.test(entity) && PlayerSavedData.getData((ServerPlayer) entity).getConsortReputation(homeDimension) <= -1000;
return entity instanceof ServerPlayer player && EntitySelector.NO_CREATIVE_OR_SPECTATOR.test(entity)
&& ConsortReputation.get(player).getConsortReputation(homeDimension) <= -1000;
}

protected void applyAdditionalAITasks()
Expand Down Expand Up @@ -153,7 +154,7 @@ protected InteractionResult mobInteract(Player player, InteractionHand hand)
return InteractionResult.FAIL;

PlayerData playerData = PlayerSavedData.getData(serverPlayer);
if(playerData == null || playerData.getConsortReputation(homeDimension) <= -1000)
if(playerData == null || ConsortReputation.get(playerData).getConsortReputation(homeDimension) <= -1000)
return InteractionResult.FAIL;

handleConsortRepFromTalking(serverPlayer);
Expand Down Expand Up @@ -191,7 +192,7 @@ private void handleConsortRepFromTalking(ServerPlayer player)
PlayerIdentifier identifier = IdentifierHandler.encode(player);
if(!talkRepPlayerList.contains(identifier))
{
PlayerSavedData.getData(player).addConsortReputation(1, homeDimension);
ConsortReputation.get(player).addConsortReputation(1, homeDimension);
talkRepPlayerList.add(identifier);
}
}
Expand Down Expand Up @@ -347,7 +348,7 @@ public SpawnGroupData finalizeSpawn(ServerLevelAccessor worldIn, DifficultyInsta
public boolean skipAttackInteraction(Entity entityIn)
{
if(!(entityIn instanceof FakePlayer) && entityIn instanceof ServerPlayer player)
PlayerSavedData.getData(player).addConsortReputation(-5, homeDimension);
ConsortReputation.get(player).addConsortReputation(-5, homeDimension);
return super.skipAttackInteraction(entityIn);
}

Expand All @@ -356,7 +357,7 @@ public void die(DamageSource cause)
{
LivingEntity livingEntity = this.getKillCredit();
if(livingEntity instanceof ServerPlayer player && (!(player instanceof FakePlayer)))
PlayerSavedData.getData(player).addConsortReputation(-100, homeDimension);
ConsortReputation.get(player).addConsortReputation(-100, homeDimension);
super.die(cause);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.mraof.minestuck.entity.consort;

import com.mraof.minestuck.player.PlayerData;
import com.mraof.minestuck.player.PlayerSavedData;
import com.mraof.minestuck.util.MSCapabilities;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.util.INBTSerializable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public final class ConsortReputation implements INBTSerializable<ListTag>
{
private final Map<ResourceLocation, Integer> consortReputation = new HashMap<>();

public int getConsortReputation(ResourceKey<Level> dim)
{
return consortReputation.getOrDefault(dim.location(), 0);
}

public void addConsortReputation(int amount, ResourceKey<Level> dim)
{
int oldRep = getConsortReputation(dim);
int newRep = Mth.clamp(oldRep + amount, -10000, 10000);

if(newRep != oldRep)
consortReputation.put(dim.location(), newRep);
}

@Override
public ListTag serializeNBT()
{
ListTag list = new ListTag();
for(Map.Entry<ResourceLocation, Integer> entry : consortReputation.entrySet())
{
CompoundTag dimensionRep = new CompoundTag();
dimensionRep.putString("dim", entry.getKey().toString());
dimensionRep.putInt("rep", entry.getValue());
list.add(dimensionRep);
}
return list;
}

@Override
public void deserializeNBT(ListTag list)
{
for(int i = 0; i < list.size(); i++)
{
CompoundTag dimensionRep = list.getCompound(i);
ResourceLocation dimension = ResourceLocation.tryParse(dimensionRep.getString("dim"));
if(dimension != null)
consortReputation.put(dimension, dimensionRep.getInt("rep"));
}
}

public static ConsortReputation get(ServerPlayer player)
{
return get(Objects.requireNonNull(PlayerSavedData.getData(player)));
}

public static ConsortReputation get(PlayerData playerData)
{
return playerData.getData(MSCapabilities.CONSORT_REPUTATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mraof.minestuck.advancements.MSCriteriaTriggers;
import com.mraof.minestuck.entity.consort.ConsortEntity;
import com.mraof.minestuck.entity.consort.ConsortReputation;
import com.mraof.minestuck.entity.consort.ConsortRewardHandler;
import com.mraof.minestuck.entity.dialogue.condition.Condition;
import com.mraof.minestuck.inventory.ConsortMerchantInventory;
Expand Down Expand Up @@ -370,7 +371,7 @@ public void triggerEffect(LivingEntity entity, ServerPlayer player)

PlayerData data = PlayerSavedData.getData(player);
if(data != null)
data.addConsortReputation(this.reputation, consortEntity.getHomeDimension());
ConsortReputation.get(data).addConsortReputation(this.reputation, consortEntity.getHomeDimension());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mraof.minestuck.entity.carapacian.CarapacianEntity;
import com.mraof.minestuck.entity.carapacian.EnumEntityKingdom;
import com.mraof.minestuck.entity.consort.ConsortEntity;
import com.mraof.minestuck.entity.consort.ConsortReputation;
import com.mraof.minestuck.entity.consort.EnumConsort;
import com.mraof.minestuck.entity.dialogue.DialogueComponent;
import com.mraof.minestuck.entity.dialogue.DialogueEntity;
Expand Down Expand Up @@ -735,10 +736,12 @@ public boolean test(LivingEntity entity, ServerPlayer player)
Level level = player.level();

PlayerData data = PlayerSavedData.getData(player);
if(data != null)
return greaterThan ? data.getConsortReputation(level.dimension()) > amount : data.getConsortReputation(level.dimension()) < amount;
if(data == null)
return false;

return false;
return greaterThan
? ConsortReputation.get(data).getConsortReputation(level.dimension()) > amount
: ConsortReputation.get(data).getConsortReputation(level.dimension()) < amount;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import com.mraof.minestuck.entity.AttackingAnimatedEntity;
import com.mraof.minestuck.entity.EntityListFilter;
import com.mraof.minestuck.entity.ai.HurtByTargetAlliedGoal;
import com.mraof.minestuck.entity.consort.ConsortReputation;
import com.mraof.minestuck.entity.item.GristEntity;
import com.mraof.minestuck.entity.item.VitalityGelEntity;
import com.mraof.minestuck.player.*;
import com.mraof.minestuck.player.Echeladder;
import com.mraof.minestuck.player.EcheladderBonusType;
import com.mraof.minestuck.player.IdentifierHandler;
import com.mraof.minestuck.player.PlayerIdentifier;
import com.mraof.minestuck.util.MSNBTUtil;
import com.mraof.minestuck.util.MSTags;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -233,7 +237,7 @@ public void die(DamageSource cause)
{
LivingEntity entity = this.getKillCredit();
if(entity instanceof ServerPlayer player && (!(player instanceof FakePlayer)))
PlayerSavedData.getData(player).addConsortReputation(consortRep, level().dimension());
ConsortReputation.get(player).addConsortReputation(consortRep, level().dimension());

super.die(cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mraof.minestuck.advancements.MSCriteriaTriggers;
import com.mraof.minestuck.entity.consort.ConsortEntity;
import com.mraof.minestuck.entity.consort.ConsortReputation;
import com.mraof.minestuck.player.PlayerBoondollars;
import com.mraof.minestuck.player.PlayerData;
import com.mraof.minestuck.player.PlayerSavedData;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void handlePurchase(ServerPlayer player, boolean all, int index)
} else
{
PlayerBoondollars.takeBoondollars(playerData, amountPurchased * prices[index]);
playerData.addConsortReputation(5, consort.getHomeDimension());
ConsortReputation.get(playerData).addConsortReputation(5, consort.getHomeDimension());
ItemStack items = stack.split(amountPurchased);
if(stack.isEmpty())
{
Expand Down Expand Up @@ -213,13 +214,13 @@ else if(consortRep < 8000)

public ContainerData createPricesFor(ServerPlayer player)
{
PlayerData data = PlayerSavedData.getData(player);
ConsortReputation reputation = ConsortReputation.get(player);
return new ContainerData()
{
@Override
public int get(int index)
{
return calculatePrice(prices[index], data.getConsortReputation(consort.getHomeDimension()));
return calculatePrice(prices[index], reputation.getConsortReputation(consort.getHomeDimension()));
}

@Override
Expand Down
44 changes: 1 addition & 43 deletions src/main/java/com/mraof/minestuck/player/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
import com.mraof.minestuck.Minestuck;
import com.mraof.minestuck.world.MSDimensions;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.level.Level;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.attachment.AttachmentHolder;
Expand All @@ -19,8 +14,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.Map;

/**
* Stores and sends any data connected to a specific player.
Expand All @@ -43,8 +36,6 @@ public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)

private final MinecraftServer mcServer;

private final Map<ResourceLocation, Integer> consortReputation = new HashMap<>();

PlayerData(MinecraftServer mcServer, PlayerIdentifier player)
{
this.mcServer = mcServer;
Expand All @@ -58,15 +49,6 @@ public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)

if(nbt.contains(ATTACHMENTS_NBT_KEY, Tag.TAG_COMPOUND))
this.deserializeAttachments(nbt.getCompound(ATTACHMENTS_NBT_KEY));

ListTag list = nbt.getList("consort_reputation", Tag.TAG_COMPOUND);
for(int i = 0; i < list.size(); i++)
{
CompoundTag dimensionRep = list.getCompound(i);
ResourceLocation dimension = ResourceLocation.tryParse(dimensionRep.getString("dim"));
if(dimension != null)
consortReputation.put(dimension, dimensionRep.getInt("rep"));
}
}

CompoundTag writeToNBT()
Expand All @@ -78,33 +60,9 @@ CompoundTag writeToNBT()
if(attachments != null)
nbt.put(ATTACHMENTS_NBT_KEY, attachments);

ListTag list = new ListTag();
for(Map.Entry<ResourceLocation, Integer> entry : consortReputation.entrySet())
{
CompoundTag dimensionRep = new CompoundTag();
dimensionRep.putString("dim", entry.getKey().toString());
dimensionRep.putInt("rep", entry.getValue());
list.add(dimensionRep);
}
nbt.put("consort_reputation", list);

return nbt;
}

public int getConsortReputation(ResourceKey<Level> dim)
{
return consortReputation.getOrDefault(dim.location(), 0);
}

public void addConsortReputation(int amount, ResourceKey<Level> dim)
{
int oldRep = getConsortReputation(dim);
int newRep = Mth.clamp(oldRep + amount, -10000, 10000);

if(newRep != oldRep)
consortReputation.put(dim.location(), newRep);
}

@Nullable
public ServerPlayer getPlayer()
{
Expand All @@ -115,4 +73,4 @@ public MinecraftServer getMinecraftServer()
{
return this.mcServer;
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/mraof/minestuck/util/MSCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mraof.minestuck.blockentity.machine.*;
import com.mraof.minestuck.computer.editmode.EditTools;
import com.mraof.minestuck.computer.editmode.EditmodeLocations;
import com.mraof.minestuck.entity.consort.ConsortReputation;
import com.mraof.minestuck.entity.dialogue.DialogueComponent;
import com.mraof.minestuck.fluid.MSFluidType;
import com.mraof.minestuck.inventory.captchalogue.CaptchaDeckHandler;
Expand Down Expand Up @@ -55,6 +56,8 @@ public final class MSCapabilities
() -> AttachmentType.builder(restricted(() -> false, ServerPlayer.class)).serialize(Codec.BOOL).build());
public static final Supplier<AttachmentType<EditmodeLocations>> EDITMODE_LOCATIONS = ATTACHMENT_REGISTER.register("editmode_locations",
() -> AttachmentType.serializable(EditmodeLocations::new).build());
public static final Supplier<AttachmentType<ConsortReputation>> CONSORT_REPUTATION = ATTACHMENT_REGISTER.register("consort_reputation",
() -> AttachmentType.serializable(restricted(ConsortReputation::new, PlayerData.class)).build());

public static final Supplier<AttachmentType<ItemStackHandler>> MUSIC_PLAYER_INVENTORY_ATTACHMENT = ATTACHMENT_REGISTER.register("music_player_inventory",
() -> AttachmentType.serializable(restricted(() -> new ItemStackHandler(1), ItemStack.class)).build());
Expand Down

0 comments on commit c3a7c6a

Please sign in to comment.