Skip to content

Commit

Permalink
Merge pull request #339 from vincent4vx/feature-additional-admin-comm…
Browse files Browse the repository at this point in the history
…ands

 feat(admin): Improve commands + add spawn and learnspell
  • Loading branch information
vincent4vx authored Mar 29, 2024
2 parents cbdee17 + ea8318b commit 4e08d7e
Show file tree
Hide file tree
Showing 27 changed files with 965 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class MonsterGroupData {
private final int id;
private final Duration respawnTime;
private final @NonNegative int maxSize;
private final @Positive int maxCount;
private final @NonNegative int maxCount;
private final List<Monster> monsters;
private final @Nullable String comment;
private final Position winFightTeleport;
Expand All @@ -45,7 +45,7 @@ public final class MonsterGroupData {
private final @Positive int totalRate;

@SuppressWarnings("cast.unsafe") // @todo remove when nullable monsters list will be denied
public MonsterGroupData(int id, Duration respawnTime, @NonNegative int maxSize, @Positive int maxCount, List<Monster> monsters, @Nullable String comment, Position winFightTeleport, boolean fixedTeamNumber) {
public MonsterGroupData(int id, Duration respawnTime, @NonNegative int maxSize, @NonNegative int maxCount, List<Monster> monsters, @Nullable String comment, Position winFightTeleport, boolean fixedTeamNumber) {
this.id = id;
this.respawnTime = respawnTime;
this.maxSize = maxSize;
Expand Down Expand Up @@ -108,10 +108,11 @@ public List<Monster> monsters() {
* Maximum number of occurrence of the group
*
* If the value is 1, only one group is spawn, and can respawn only when the previous group has start a fight
* If this value is 0, the group will not spawn automatically
*
* For dungeon groups, this value should be 1
*/
public @Positive int maxCount() {
public @NonNegative int maxCount() {
return maxCount;
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/fr/quatrevieux/araknemu/game/admin/AdminModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@
import fr.quatrevieux.araknemu.game.admin.executor.argument.HydratorsAggregate;
import fr.quatrevieux.araknemu.game.admin.global.GlobalContext;
import fr.quatrevieux.araknemu.game.admin.global.Help;
import fr.quatrevieux.araknemu.game.admin.player.AddXp;
import fr.quatrevieux.araknemu.game.admin.player.GetItem;
import fr.quatrevieux.araknemu.game.admin.player.LearnSpell;
import fr.quatrevieux.araknemu.game.admin.player.PlayerContext;
import fr.quatrevieux.araknemu.game.admin.player.PlayerContextResolver;
import fr.quatrevieux.araknemu.game.admin.player.Spawn;
import fr.quatrevieux.araknemu.game.admin.player.teleport.CellResolver;
import fr.quatrevieux.araknemu.game.admin.player.teleport.Goto;
import fr.quatrevieux.araknemu.game.admin.player.teleport.LocationResolver;
Expand All @@ -74,7 +77,11 @@
import fr.quatrevieux.araknemu.game.exploration.map.GeolocationService;
import fr.quatrevieux.araknemu.game.fight.FightService;
import fr.quatrevieux.araknemu.game.item.ItemService;
import fr.quatrevieux.araknemu.game.monster.environment.MonsterEnvironmentService;
import fr.quatrevieux.araknemu.game.monster.group.MonsterGroupFactory;
import fr.quatrevieux.araknemu.game.player.PlayerService;
import fr.quatrevieux.araknemu.game.player.experience.PlayerExperienceService;
import fr.quatrevieux.araknemu.game.spell.SpellService;
import org.apache.logging.log4j.LogManager;

import java.nio.file.Paths;
Expand Down Expand Up @@ -181,6 +188,9 @@ public void configure(PlayerContext context) {
new PlayerResolver(container.get(PlayerService.class), container.get(ExplorationMapService.class)),
new CellResolver(),
}));
add(new AddXp(context.player(), container.get(PlayerExperienceService.class)));
add(new LearnSpell(context.player(), container.get(SpellService.class)));
add(new Spawn(context.player(), container.get(FightService.class), container.get(MonsterEnvironmentService.class), container.get(MonsterGroupFactory.class)));
}
}),
ctx -> container.with(ctx.player()),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/fr/quatrevieux/araknemu/game/admin/account/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import fr.quatrevieux.araknemu.game.account.GameAccount;
import fr.quatrevieux.araknemu.game.admin.AbstractCommand;
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
import fr.quatrevieux.araknemu.game.admin.formatter.Link;
import fr.quatrevieux.araknemu.network.game.GameSession;

/**
* Info command for account
Expand Down Expand Up @@ -77,6 +79,15 @@ public void execute(AdminPerformer performer, Void arguments) {
performer.error("Logged: No");
}

account.session().map(GameSession::player).ifPresent(player -> {
performer.info(
"Player: {}",
Link.Type.EXECUTE
.create("@" + player.name() + " info")
.text(player.name())
);
});

if (!account.isMaster()) {
performer.error("Standard account");
} else {
Expand Down
35 changes: 29 additions & 6 deletions src/main/java/fr/quatrevieux/araknemu/game/admin/player/AddXp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
import fr.quatrevieux.araknemu.game.admin.AbstractCommand;
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
import fr.quatrevieux.araknemu.game.player.GamePlayer;
import org.checkerframework.checker.index.qual.Positive;
import fr.quatrevieux.araknemu.game.player.experience.PlayerExperienceService;
import org.checkerframework.checker.index.qual.NonNegative;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

/**
* Add experience to player
*/
public final class AddXp extends AbstractCommand<AddXp.Arguments> {
private final GamePlayer player;
private final PlayerExperienceService service;

public AddXp(GamePlayer player) {
public AddXp(GamePlayer player, PlayerExperienceService service) {
this.player = player;
this.service = service;
}

@Override
Expand All @@ -43,6 +47,7 @@ protected void build(Builder builder) {
formatter -> formatter
.description("Add experience to player")
.example("@John addxp 1000000", "Add 1 million xp to John")
.example("@John addxp --level 150", "Add xp to John to reach level 150")
)
.requires(Permission.MANAGE_PLAYER)
.arguments(Arguments::new)
Expand All @@ -56,17 +61,35 @@ public String name() {

@Override
public void execute(AdminPerformer performer, Arguments arguments) {
player.properties().experience().add(arguments.quantity);
long quantity = arguments.quantity;

performer.success("Add {} xp to {} (level = {})", arguments.quantity, player.name(), player.properties().experience().level());
if (arguments.level > 0) {
if (arguments.level <= player.properties().experience().level()) {
performer.error("The player level ({}) is already higher than the target level ({})", player.properties().experience().level(), arguments.level);
return;
}

quantity = Math.max(service.byLevel(arguments.level).experience() - player.properties().experience().current(), 0);
}

player.properties().experience().add(quantity);

performer.success("Add {} xp to {} (level = {})", quantity, player.name(), player.properties().experience().level());
}

public static final class Arguments {
@Argument(
required = true,
required = false,
metaVar = "QUANTITY",
usage = "The experience quantity to add. Must be an unsigned number."
)
private @Positive long quantity;
private @NonNegative long quantity = 0;

@Option(
name = "--level",
aliases = {"-l"},
usage = "The target level. If set, the quantity will be calculated to reach this level. Must be a positive number."
)
private @NonNegative int level = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* This file is part of Araknemu.
*
* Araknemu is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Araknemu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2017-2024 Vincent Quatrevieux
*/

package fr.quatrevieux.araknemu.game.admin.player;

import fr.quatrevieux.araknemu.common.account.Permission;
import fr.quatrevieux.araknemu.core.dbal.repository.EntityNotFoundException;
import fr.quatrevieux.araknemu.game.admin.AbstractCommand;
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
import fr.quatrevieux.araknemu.game.player.GamePlayer;
import fr.quatrevieux.araknemu.game.player.spell.SpellBook;
import fr.quatrevieux.araknemu.game.spell.SpellLevels;
import fr.quatrevieux.araknemu.game.spell.SpellService;
import org.kohsuke.args4j.Argument;

/**
* Learn a spell to a player
*/
public final class LearnSpell extends AbstractCommand<LearnSpell.Arguments> {
private final GamePlayer player;
private final SpellService service;

public LearnSpell(GamePlayer player, SpellService service) {
this.player = player;
this.service = service;
}

@Override
protected void build(AbstractCommand<Arguments>.Builder builder) {
builder
.help(
formatter -> formatter
.description("Add the given spell to a player")
.example("@John learnspell 366", "John will learn the spell Moon Hammer")
)
.requires(Permission.MANAGE_PLAYER)
.arguments(Arguments::new)
;
}

@Override
public String name() {
return "learnspell";
}

@Override
public void execute(AdminPerformer performer, Arguments arguments) throws AdminException {
final SpellLevels toLearn;

try {
toLearn = service.get(arguments.spellId);
} catch (EntityNotFoundException e) {
performer.error("Spell {} not found", arguments.spellId);
return;
}

final SpellBook spells = player.properties().spells();

if (!spells.canLearn(toLearn)) {
performer.error("Cannot learn spell {} ({})", toLearn.name(), arguments.spellId);
return;
}

spells.learn(toLearn);
performer.success("The spell {} ({}) has been learned", toLearn.name(), arguments.spellId);
}

public static final class Arguments {
@Argument(required = true, metaVar = "SPELLID", usage = "The spell ID to learn.")
private int spellId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ protected SimpleContext createContext() {
.add(new Info(player))
.add(new SetLife(player))
.add(new AddStats(player))
.add(new AddXp(player))
.add(new Restriction(player))
.add(new Save(player))
.add(new Message(player))
Expand Down
Loading

0 comments on commit 4e08d7e

Please sign in to comment.