Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Mar 28, 2024
2 parents c9ea0d0 + 995894d commit fa45e45
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 74 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2004.1.2]

### Changed
* `/tpl` is now `/teleport_last` (Technically this happened last version)
* `/anvil`, `/crafting`, `/smithing`, `/stonecutter` have been moved under the `/open` namespace
* E.g. `/open anvil` will open an Anvil GUI
* `/listhomes` now allows you to click to teleport to a home (When OP) and has had the output improved.

### Fixed
* `/leaderboard` will now correctly show offline players
* `/leaderboard` will no longer show `#010` :joy:

## [2004.1.1]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,15 @@ private static int tpLast(ServerPlayer player, GameProfile to) {
new TeleportPos(toPlayer).teleport(player);
return 1;
}

// dest player not online; teleport to where they were last seen
return FTBEPlayerData.getOrCreate(to).map(dataTo -> {
FTBEPlayerData.addTeleportHistory(player);
dataTo.getLastSeenPos().teleport(player);
return 1;
}).orElse(0);
return FTBEPlayerData.getOrCreate(player.getServer(), to.getId())
.map(data -> {
FTBEPlayerData.addTeleportHistory(player);
data.getLastSeenPos().teleport(player);

return 1;
}).orElse(0);
}

private static int tpx(ServerPlayer player, ServerLevel to) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.ftb.mods.ftbessentials.commands.impl;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
Expand Down Expand Up @@ -273,12 +272,12 @@ private static int resetCooldowns(CommandSourceStack source, String name, UUID p
return 0;
}

FTBEPlayerData.getOrCreate(new GameProfile(playerId, "")).ifPresent(data -> {
data.setLastKitUseTime(name, 0L);
source.sendSuccess(() -> Component.literal("Cooldown for '" + name + "' reset for UUID " + playerId).withStyle(ChatFormatting.YELLOW), false);
});

return 1;
return FTBEPlayerData.getOrCreate(source.getServer(), playerId)
.map(data -> {
data.setLastKitUseTime(name, 0L);
source.sendSuccess(() -> Component.literal("Cooldown for '" + name + "' reset for UUID " + playerId).withStyle(ChatFormatting.YELLOW), false);
return 1;
}).orElse(0);
}

private static int resetCooldowns(CommandSourceStack source, String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ftb.mods.ftbessentials.commands.impl.cheat;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import dev.ftb.mods.ftbessentials.commands.CommandUtils;
import dev.ftb.mods.ftbessentials.commands.FTBCommand;
import dev.ftb.mods.ftbessentials.config.FTBEConfig;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -20,12 +21,20 @@
public class VirtualInventoryCommand implements FTBCommand {
@Override
public boolean enabled() {
return false;
// Not the best but it'll do
boolean enabled = false;
enabled |= FTBEConfig.ANVIL.isEnabled();
enabled |= FTBEConfig.CRAFTING_TABLE.isEnabled();
enabled |= FTBEConfig.SMITHING_TABLE.isEnabled();
enabled |= FTBEConfig.STONECUTTER.isEnabled();

return enabled;
}

@Override
public List<LiteralArgumentBuilder<CommandSourceStack>> register() {
var openCommand = Commands.literal("open");
var openCommand = Commands.literal("open")
.requires(CommandUtils.isGamemaster());

if (FTBEConfig.ANVIL.isEnabled()) {
openCommand.then(createMenu("anvil", "block.minecraft.anvil", VirtualAnvilMenu::new));
Expand All @@ -48,7 +57,6 @@ public List<LiteralArgumentBuilder<CommandSourceStack>> register() {

public static LiteralArgumentBuilder<CommandSourceStack> createMenu(String name, String translate, VirtualMenuFactory factory) {
return Commands.literal(name)
.requires(ctx -> ctx.hasPermission(Commands.LEVEL_GAMEMASTERS))
.executes(context -> {
var player = context.getSource().getPlayerOrException();
player.openMenu(new SimpleMenuProvider((id, inv, p) -> factory.create(id, inv, (ServerPlayer) p), Component.translatable(translate)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package dev.ftb.mods.ftbessentials.commands.impl.misc;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import dev.ftb.mods.ftbessentials.commands.FTBCommand;
import dev.ftb.mods.ftbessentials.config.FTBEConfig;
import dev.ftb.mods.ftbessentials.mixin.PlayerListAccess;
import dev.ftb.mods.ftbessentials.util.FTBEPlayerData;
import dev.ftb.mods.ftbessentials.util.FTBEWorldData;
import dev.ftb.mods.ftbessentials.util.Leaderboard;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -20,13 +18,17 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.storage.LevelResource;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

public class LeaderboardCommand implements FTBCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(LeaderboardCommand.class);

@Override
public boolean enabled() {
return FTBEConfig.LEADERBOARD.isEnabled();
Expand All @@ -38,27 +40,28 @@ public List<LiteralArgumentBuilder<CommandSourceStack>> register() {
}

public static <T extends Number> int leaderboard(CommandSourceStack source, Leaderboard<T> leaderboard, boolean reverse) {
try (var stream = Files.list(FTBEWorldData.instance.mkdirs("playerdata"))) {
stream.filter(path -> path.toString().endsWith(".json"))
.map(Path::getFileName)
.map(path -> new GameProfile(UUID.fromString(path.toString().replace(".json", "")), null))
.filter(profile -> !FTBEPlayerData.playerExists(profile.getId()))
.map(FTBEPlayerData::getOrCreate)
.filter(Optional::isPresent)
.forEach(data -> data.get().load());
} catch (Exception ex) {
ex.printStackTrace();
}
var knownPlayers = FTBEPlayerData.getAllKnownPlayers();
var playerData = knownPlayers
.stream()
.map(uuid -> FTBEPlayerData.getOrCreate(source.getServer(), uuid))
.filter(Optional::isPresent)
.map(Optional::get)
.toList();

List<Pair<FTBEPlayerData, T>> list = new ArrayList<>();
int self = -1;

FTBEPlayerData.forEachPlayer(playerData -> {
ServerStatsCounter stats = getPlayerStats(source.getServer(), playerData.getUuid());
Path worldPath = source.getServer().getWorldPath(LevelResource.PLAYER_STATS_DIR);
if (!Files.exists(worldPath)) {
return 1;
}

playerData.forEach(pd -> {
ServerStatsCounter stats = getPlayerStats(source.getServer(), pd.getUuid());

T num = leaderboard.getValue(stats);
if (leaderboard.test(num)) {
list.add(Pair.of(playerData, num));
list.add(Pair.of(pd, num));
}
});

Expand All @@ -77,7 +80,7 @@ public static <T extends Number> int leaderboard(CommandSourceStack source, Lead
}
}

source.sendSuccess(() -> Component.literal("== Leaderboard [" + leaderboard.getName() + "] ==").withStyle(ChatFormatting.DARK_GREEN), false);
source.sendSuccess(() -> Component.literal("== Leaderboard [" + leaderboard.formattedName() + "] ==").withStyle(ChatFormatting.DARK_GREEN), false);

if (list.isEmpty()) {
source.sendSuccess(() -> Component.literal("No data!").withStyle(ChatFormatting.GRAY), false);
Expand All @@ -86,28 +89,26 @@ public static <T extends Number> int leaderboard(CommandSourceStack source, Lead

for (int i = 0; i < Math.min(20, list.size()); i++) {
Pair<FTBEPlayerData, T> pair = list.get(i);
String num = String.valueOf(i + 1);

if (i < 10) {
num = "0" + num;
}
String num = String.format("%02d", i + 1);

MutableComponent component = Component.literal("");
component.withStyle(ChatFormatting.GRAY);

if (i == 0) {
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xD4AF37))));
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(ChatFormatting.GOLD)));
} else if (i == 1) {
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xC0C0C0))));
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xB0D9FF))));
} else if (i == 2) {
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0x9F7A34))));
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xCD7F32))));
} else {
component.append(Component.literal("#" + num + " "));
component.append(Component.literal("#" + num + " ").withStyle(Style.EMPTY.withColor(TextColor.fromRgb(0xB4B4B4))));
}

var color = TextColor.fromRgb(0xCD7F32);

component.append(Component.literal(pair.getLeft().getName()).withStyle(i == self ? ChatFormatting.GREEN : ChatFormatting.YELLOW));
component.append(Component.literal(": "));
component.append(Component.literal(leaderboard.asString(pair.getRight())));
component.append(Component.literal(leaderboard.asString(pair.getRight())).withStyle(ChatFormatting.WHITE));
source.sendSuccess(() -> component, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import dev.ftb.mods.ftbessentials.util.FTBEPlayerData;
import dev.ftb.mods.ftbessentials.util.SavedTeleportManager;
import dev.ftb.mods.ftbessentials.util.TeleportPos;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.GameProfileArgument;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.*;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;
Expand Down Expand Up @@ -54,7 +55,7 @@ public List<LiteralArgumentBuilder<CommandSourceStack>> register() {
.requires(FTBEConfig.HOME)
.executes(context -> listHomes(context.getSource(), context.getSource().getPlayerOrException().getGameProfile()))
.then(Commands.argument("player", GameProfileArgument.gameProfile())
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2))
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(Commands.LEVEL_GAMEMASTERS))
.executes(context -> listHomes(context.getSource(), GameProfileArgument.getGameProfiles(context, "player").iterator().next()))
)
);
Expand Down Expand Up @@ -98,15 +99,28 @@ public int delHome(ServerPlayer player, String name) {
}

public static int listHomes(CommandSourceStack source, GameProfile of) {
return FTBEPlayerData.getOrCreate(of).map(data -> {
if (data.homeManager().getNames().isEmpty()) {
source.sendSuccess(() -> Component.literal("None"), false);
} else {
TeleportPos origin = new TeleportPos(source.getLevel().dimension(), BlockPos.containing(source.getPosition()));
data.homeManager().destinations().forEach(entry ->
source.sendSuccess(() -> Component.literal(entry.name() + ": " + entry.destination().distanceString(origin)), false));
}
return 1;
}).orElse(0);
return FTBEPlayerData.getOrCreate(source.getServer(), of.getId())
.map(data -> {
if (data.homeManager().getNames().isEmpty()) {
source.sendSuccess(() -> Component.literal("None"), false);
} else {
source.sendSuccess(() -> Component.literal("Homes for " + of.getName() + "\n---").withStyle(ChatFormatting.GOLD), false);

TeleportPos origin = new TeleportPos(source.getLevel().dimension(), BlockPos.containing(source.getPosition()));
data.homeManager().destinations().forEach(entry ->
source.sendSuccess(() -> {
MutableComponent literal = Component.empty().append(Component.literal(entry.name()).withStyle(ChatFormatting.AQUA).withStyle(ChatFormatting.BOLD)).append(Component.literal( ": " + entry.destination().distanceString(origin) + " away"));
if (source.hasPermission(Commands.LEVEL_GAMEMASTERS)) {
literal.withStyle(Style.EMPTY
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tp @s " + entry.destination().posAsString()))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("Click to teleport")))
);
}
return literal;
}, false));
}

return 1;
}).orElse(0);
}
}
Loading

0 comments on commit fa45e45

Please sign in to comment.