-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
100 additions
and
57 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
72 changes: 72 additions & 0 deletions
72
src/main/java/com/mraof/minestuck/entity/consort/ConsortReputation.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,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); | ||
} | ||
} |
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
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
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