Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Origin related nametag prefix #521

Open
wants to merge 6 commits into
base: 1.20
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/io/github/apace100/origins/Origins.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

public class Origins implements ModInitializer, OrderedResourceListenerInitializer {

public static final String MODID = "origins";
Expand Down Expand Up @@ -131,6 +134,10 @@ public static class ServerConfig implements ConfigData {

public boolean performVersionCheck = true;

public boolean enableNametagPrefix = false;

public List<String> nametagPrefixBlacklist = new ArrayList<>();

public JsonObject origins = new JsonObject();

public boolean isOriginDisabled(Identifier originId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ public void onInitializeClient() {

PowerClearCallback.EVENT.register(PowerKeyManager::clearCache);
}

}
64 changes: 32 additions & 32 deletions src/main/java/io/github/apace100/origins/command/OriginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,42 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
* @throws CommandSyntaxException if the entity is not found or if the entity is <b>not</b> an instance of {@link ServerPlayerEntity}
*/
private static int setOrigin(CommandContext<ServerCommandSource> commandContext) throws CommandSyntaxException {

Collection<ServerPlayerEntity> targets = EntityArgumentType.getPlayers(commandContext, "targets");
OriginLayer originLayer = LayerArgumentType.getLayer(commandContext, "layer");
Origin origin = OriginArgumentType.getOrigin(commandContext, "origin");
ServerCommandSource serverCommandSource = commandContext.getSource();

int processedTargets = 0;

if (origin.equals(Origin.EMPTY) || originLayer.getOrigins().contains(origin.getIdentifier())) {

for (ServerPlayerEntity target : targets) {

OriginComponent originComponent = ModComponents.ORIGIN.get(target);
boolean hadOriginBefore = originComponent.hadOriginBefore();

originComponent.setOrigin(originLayer, origin);
originComponent.sync();

OriginComponent.partialOnChosen(target, hadOriginBefore, origin);

processedTargets++;

}
if (processedTargets == 1) serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.set.success.single", targets.iterator().next().getDisplayName().getString(), Text.translatable(originLayer.getTranslationKey()), origin.getName()), true);

if (processedTargets == 1) serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.set.success.single", targets.iterator().next().getName().getString(), Text.translatable(originLayer.getTranslationKey()), origin.getName()), true);
else {
int finalProcessedTargets = processedTargets;
serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.set.success.multiple", finalProcessedTargets, Text.translatable(originLayer.getTranslationKey()), origin.getName()), true);
}

}

else serverCommandSource.sendError(Text.translatable("commands.origin.unregistered_in_layer", origin.getIdentifier(), originLayer.getIdentifier()));

return processedTargets;

}

/**
Expand All @@ -129,34 +129,34 @@ private static int setOrigin(CommandContext<ServerCommandSource> commandContext)
* @throws CommandSyntaxException if the entity is not found or if the entity is <b>not</b> an instance of {@link ServerPlayerEntity}
*/
private static int hasOrigin(CommandContext<ServerCommandSource> commandContext) throws CommandSyntaxException {

Collection<ServerPlayerEntity> targets = EntityArgumentType.getPlayers(commandContext, "targets");
OriginLayer originLayer = LayerArgumentType.getLayer(commandContext, "layer");
Origin origin = OriginArgumentType.getOrigin(commandContext, "origin");
ServerCommandSource serverCommandSource = commandContext.getSource();

int processedTargets = 0;

if (origin.equals(Origin.EMPTY) || originLayer.getOrigins().contains(origin.getIdentifier())) {

for (ServerPlayerEntity target : targets) {
OriginComponent originComponent = ModComponents.ORIGIN.get(target);
if ((origin.equals(Origin.EMPTY) || originComponent.hasOrigin(originLayer)) && originComponent.getOrigin(originLayer).equals(origin)) processedTargets++;
}

if (processedTargets == 0) serverCommandSource.sendError(Text.translatable("commands.execute.conditional.fail"));
else if (processedTargets == 1) serverCommandSource.sendFeedback(() -> Text.translatable("commands.execute.conditional.pass"), true);
else {
int finalProcessedTargets = processedTargets;
serverCommandSource.sendFeedback(() -> Text.translatable("commands.execute.conditional.pass_count", finalProcessedTargets), true);
}

}

else serverCommandSource.sendError(Text.translatable("commands.origin.unregistered_in_layer", origin.getIdentifier(), originLayer.getIdentifier()));

return processedTargets;

}

/**
Expand All @@ -166,18 +166,18 @@ private static int hasOrigin(CommandContext<ServerCommandSource> commandContext)
* @throws CommandSyntaxException if the entity is not found or if the entity is <b>not</b> an instance of {@link ServerPlayerEntity}
*/
private static int getOrigin(CommandContext<ServerCommandSource> commandContext) throws CommandSyntaxException {

ServerPlayerEntity target = EntityArgumentType.getPlayer(commandContext, "target");
ServerCommandSource serverCommandSource = commandContext.getSource();

OriginComponent originComponent = ModComponents.ORIGIN.get(target);
OriginLayer originLayer = LayerArgumentType.getLayer(commandContext, "layer");
Origin origin = originComponent.getOrigin(originLayer);
serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.get.result", target.getDisplayName().getString(), Text.translatable(originLayer.getTranslationKey()), origin.getName(), origin.getIdentifier()), true);

serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.get.result", target.getName().getString(), Text.translatable(originLayer.getTranslationKey()), origin.getName(), origin.getIdentifier()), true);

return 1;

}

/**
Expand Down Expand Up @@ -251,7 +251,7 @@ private static int randomizeOrigin(CommandContext<ServerCommandSource> commandCo
if (targets.size() > 1) serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.random.success.multiple", targets.size(), Text.translatable(originLayer.getTranslationKey())), true);
else if (targets.size() == 1) {
Origin finalOrigin = origin;
serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.random.success.single", targets.iterator().next().getDisplayName().getString(), finalOrigin.getName(), Text.translatable(originLayer.getTranslationKey())), false);
serverCommandSource.sendFeedback(() -> Text.translatable("commands.origin.random.success.single", targets.iterator().next().getName().getString(), finalOrigin.getName(), Text.translatable(originLayer.getTranslationKey())), false);
}

return targets.size();
Expand Down Expand Up @@ -325,13 +325,13 @@ private static Origin getRandomOrigin(ServerPlayerEntity target, OriginLayer ori

Origins.LOGGER.info(
"Player {} was randomly assigned the origin {} for layer {}",
target.getDisplayName().getString(),
target.getName().getString(),
origin.getIdentifier().toString(),
originLayer.getIdentifier().toString()
);

return origin;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void readFromNbt(NbtCompound compoundTag) {
OriginLayer defaultOriginLayer = OriginLayers.getLayer(new Identifier(Origins.MODID, "origin"));
this.origins.put(defaultOriginLayer, OriginRegistry.get(Identifier.tryParse(compoundTag.getString("Origin"))));
} catch(IllegalArgumentException e) {
Origins.LOGGER.warn("Player " + player.getDisplayName().getContent() + " had old origin which could not be migrated: " + compoundTag.getString("Origin"));
Origins.LOGGER.warn("Player " + player.getName().getString() + " had old origin which could not be migrated: " + compoundTag.getString("Origin"));
}
} else {
NbtList originLayerList = (NbtList) compoundTag.get("OriginLayers");
Expand All @@ -121,21 +121,21 @@ public void readFromNbt(NbtCompound compoundTag) {
try {
layer = OriginLayers.getLayer(layerId);
} catch(IllegalArgumentException e) {
Origins.LOGGER.warn("Could not find origin layer with id " + layerId.toString() + ", which existed on the data of player " + player.getDisplayName().getContent() + ".");
Origins.LOGGER.warn("Could not find origin layer with id " + layerId.toString() + ", which existed on the data of player " + player.getName().getString() + ".");
}
if(layer != null) {
Identifier originId = Identifier.tryParse(layerTag.getString("Origin"));
Origin origin = null;
try {
origin = OriginRegistry.get(originId);
} catch(IllegalArgumentException e) {
Origins.LOGGER.warn("Could not find origin with id " + originId.toString() + ", which existed on the data of player " + player.getDisplayName().getContent() + ".");
Origins.LOGGER.warn("Could not find origin with id " + originId.toString() + ", which existed on the data of player " + player.getName().getString() + ".");
PowerHolderComponent powerComponent = PowerHolderComponent.KEY.get(player);
powerComponent.removeAllPowersFromSource(originId);
}
if(origin != null) {
if(!layer.contains(origin) && !origin.isSpecial()) {
Origins.LOGGER.warn("Origin with id " + origin.getIdentifier().toString() + " is not in layer " + layer.getIdentifier().toString() + " and is not special, but was found on " + player.getDisplayName().getContent() + ", setting to EMPTY.");
Origins.LOGGER.warn("Origin with id " + origin.getIdentifier().toString() + " is not in layer " + layer.getIdentifier().toString() + " and is not special, but was found on " + player.getName().getString() + ", setting to EMPTY.");
origin = Origin.EMPTY;
PowerHolderComponent powerComponent = PowerHolderComponent.KEY.get(player);
powerComponent.removeAllPowersFromSource(originId);
Expand Down Expand Up @@ -176,7 +176,7 @@ public void readFromNbt(NbtCompound compoundTag) {
} catch(ClassCastException e) {
// Occurs when power was overriden by data pack since last world load
// to be a power type which uses different data class.
Origins.LOGGER.warn("Data type of \"" + powerTypeId + "\" changed, skipping data for that power on player " + player.getName().getContent());
Origins.LOGGER.warn("Data type of \"" + powerTypeId + "\" changed, skipping data for that power on player " + player.getName().getString());
}
}
} catch(IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.apace100.origins.mixin;

import io.github.apace100.origins.Origins;
import io.github.apace100.origins.registry.ModComponents;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.Map;

@Mixin(PlayerEntity.class)
public abstract class NametagPrefixMixin {

@ModifyArg(method = "getDisplayName", at = @At(value = "INVOKE", target = "Lnet/minecraft/scoreboard/Team;decorateName(Lnet/minecraft/scoreboard/AbstractTeam;Lnet/minecraft/text/Text;)Lnet/minecraft/text/MutableText;"), index = 1)
private Text appendOriginsNametagPrefix(Text name) {

if (!Origins.config.enableNametagPrefix) {
return name;
}

MutableText combinedPrefix = Text.literal("");
ModComponents.ORIGIN.get(this).getOrigins().entrySet().stream()
.filter(entry -> !(entry.getKey().isHidden() || Origins.config.nametagPrefixBlacklist.contains(entry.getKey().getIdentifier().toString())))
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> combinedPrefix.append(entry.getValue().getNametagPrefix()).append(" "));

return combinedPrefix.equals(Text.EMPTY) ? name : combinedPrefix.append(name);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ private static void chooseOrigin(MinecraftServer minecraftServer, ServerPlayerEn
if(component.hasAllOrigins() && !hadAllOrigins) {
OriginComponent.onChosen(playerEntity, hadOriginBefore);
}
Origins.LOGGER.info("Player " + playerEntity.getDisplayName().getContent() + " chose Origin: " + originId + ", for layer: " + layerId);
Origins.LOGGER.info("Player " + playerEntity.getName().getString() + " chose Origin: " + originId + ", for layer: " + layerId);
} else {
Origins.LOGGER.info("Player " + playerEntity.getDisplayName().getContent() + " tried to choose unchoosable Origin for layer " + layerId + ": " + originId + ".");
Origins.LOGGER.info("Player " + playerEntity.getName().getString() + " tried to choose unchoosable Origin for layer " + layerId + ": " + originId + ".");
component.setOrigin(layer, Origin.EMPTY);
}
confirmOrigin(playerEntity, layer, component.getOrigin(layer));
component.sync();
} else {
Origins.LOGGER.warn("Player " + playerEntity.getDisplayName().getContent() + " chose unknown origin: " + originId);
Origins.LOGGER.warn("Player " + playerEntity.getName().getString() + " chose unknown origin: " + originId);
}
} else {
Origins.LOGGER.warn("Player " + playerEntity.getDisplayName().getContent() + " tried to choose origin for layer " + layerId + " while having one already.");
Origins.LOGGER.warn("Player " + playerEntity.getName().getString() + " tried to choose origin for layer " + layerId + " while having one already.");
}
});
}
Expand All @@ -83,15 +83,15 @@ private static void chooseRandomOrigin(MinecraftServer minecraftServer, ServerPl
if(component.hasAllOrigins() && !hadAllOrigins) {
OriginComponent.onChosen(playerEntity, hadOriginBefore);
}
Origins.LOGGER.info("Player " + playerEntity.getDisplayName().getContent() + " was randomly assigned the following Origin: " + randomOrigin + ", for layer: " + layerId);
Origins.LOGGER.info("Player " + playerEntity.getName().getString() + " was randomly assigned the following Origin: " + randomOrigin + ", for layer: " + layerId);
} else {
Origins.LOGGER.info("Player " + playerEntity.getDisplayName().getContent() + " tried to choose a random Origin for layer " + layerId + ", which is not allowed!");
Origins.LOGGER.info("Player " + playerEntity.getName().getString() + " tried to choose a random Origin for layer " + layerId + ", which is not allowed!");
component.setOrigin(layer, Origin.EMPTY);
}
confirmOrigin(playerEntity, layer, component.getOrigin(layer));
component.sync();
} else {
Origins.LOGGER.warn("Player " + playerEntity.getDisplayName().getContent() + " tried to choose origin for layer " + layerId + " while having one already.");
Origins.LOGGER.warn("Player " + playerEntity.getName().getString() + " tried to choose origin for layer " + layerId + " while having one already.");
}
});
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/io/github/apace100/origins/origin/Origin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -40,7 +41,8 @@ public class Origin {
.add("loading_priority", SerializableDataTypes.INT, 0)
.add("upgrades", OriginsDataTypes.UPGRADES, null)
.add("name", SerializableDataTypes.STRING, "")
.add("description", SerializableDataTypes.STRING, "");
.add("description", SerializableDataTypes.STRING, "")
.add("nametag_prefix", SerializableDataTypes.TEXT, null);

public static final Origin EMPTY;

Expand Down Expand Up @@ -80,6 +82,7 @@ public static HashMap<OriginLayer, Origin> get(PlayerEntity player) {

private String nameTranslationKey;
private String descriptionTranslationKey;
private Text nametagPrefix;

public Origin(Identifier id, ItemStack icon, Impact impact, int order, int loadingPriority) {
this.identifier = id;
Expand Down Expand Up @@ -141,6 +144,11 @@ public Origin setDescription(String description) {
return this;
}

public Origin setNametagPrefix(@Nullable Text nametagPrefix) {
this.nametagPrefix = nametagPrefix;
return this;
}

public boolean hasPowerType(PowerType<?> powerType) {
if(powerType.getIdentifier() == null) {
return false;
Expand Down Expand Up @@ -206,6 +214,10 @@ public MutableText getDescription() {
return Text.translatable(getOrCreateDescriptionTranslationKey());
}

public Text getNametagPrefix() {
return nametagPrefix == null ? Text.literal("[").append(Text.translatable(this.getOrCreateNameTranslationKey())).append("]") : nametagPrefix;
}

public int getOrder() {
return this.order;
}
Expand All @@ -220,6 +232,7 @@ public void write(PacketByteBuf buffer) {
data.set("powers", powerTypes.stream().map(PowerType::getIdentifier).collect(Collectors.toList()));
data.set("name", getOrCreateNameTranslationKey());
data.set("description", getOrCreateDescriptionTranslationKey());
data.set("nametag_prefix", nametagPrefix);
data.set("upgrades", upgrades);
DATA.write(buffer, data);
}
Expand Down Expand Up @@ -251,6 +264,7 @@ public static Origin createFromData(Identifier id, SerializableData.Instance dat

origin.setName(data.getString("name"));
origin.setDescription(data.getString("description"));
origin.setNametagPrefix(data.get("nametag_prefix"));

return origin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ChooseOriginScreen(ArrayList<OriginLayer> layerList, int currentLayerInde
ItemStack displayItem = origin.getDisplayItem();
if(displayItem.getItem() == Items.PLAYER_HEAD) {
if(!displayItem.hasNbt() || !displayItem.getNbt().contains("SkullOwner")) {
displayItem.getOrCreateNbt().putString("SkullOwner", player.getDisplayName().getString());
displayItem.getOrCreateNbt().putString("SkullOwner", player.getName().getString());
}
}
this.originSelection.add(origin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ViewOriginScreen() {
ItemStack displayItem = origin.getDisplayItem();
if(displayItem.getItem() == Items.PLAYER_HEAD) {
if(!displayItem.hasNbt() || !displayItem.getNbt().contains("SkullOwner")) {
displayItem.getOrCreateNbt().putString("SkullOwner", player.getDisplayName().getString());
displayItem.getOrCreateNbt().putString("SkullOwner", player.getName().getString());
}
}
if((origin != Origin.EMPTY || layer.getOriginOptionCount(player) > 0) && !layer.isHidden()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/origins.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"ConduitOnLandMixin",
"LikeWaterMixin",
"LoginMixin",
"NametagPrefixMixin",
"NoCobwebSlowdownMixin",
"OriginUpgradeMixin",
"ScareCreepersMixin",
Expand Down