Skip to content

Commit

Permalink
Fix run command not working via console
Browse files Browse the repository at this point in the history
Fix status command erroring when a previously ran quest/quest line does not exist anymore
Add PlaceholderAPI placeholders for status/tasks/quest lines/quests
  • Loading branch information
Eufranio committed May 2, 2021
1 parent d7f8f19 commit ce51758
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 18 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ version = pluginVersion
dependencies {
compileOnly 'org.spongepowered:spongeapi:7.2.0-SNAPSHOT'
annotationProcessor 'org.spongepowered:spongeapi:7.2.0-SNAPSHOT'

compileOnly files('PBQMessages-1.0.jar')
compileOnly 'com.github.randombyte-developer:byte-items:v2.4.1'
compileOnly 'com.github.ronaldburns:PlaceholderAPI:b58a67b'
api 'com.github.Eufranio:StorageUtils:2.2'
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pluginGroup=online.pixelbuilt.pbquests
pluginId=pixelbuilt-quests
pluginVersion=3.3
pluginVersion=3.4
13 changes: 10 additions & 3 deletions src/main/java/online/pixelbuilt/pbquests/PixelBuiltQuests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import online.pixelbuilt.pbquests.config.serialization.ValueWrapper;
import online.pixelbuilt.pbquests.listeners.Listeners;
import online.pixelbuilt.pbquests.listeners.TaskListener;
import online.pixelbuilt.pbquests.placeholder.PlaceholderHandler;
import online.pixelbuilt.pbquests.quest.executor.QuestExecutorType;
import online.pixelbuilt.pbquests.quest.executor.QuestExecutorTypeRegistryModule;
import online.pixelbuilt.pbquests.reward.RewardRegistryModule;
Expand Down Expand Up @@ -46,8 +47,6 @@ public class PixelBuiltQuests {
public static List<UUID> playersBusy = Lists.newArrayList();
public static List<UUID> runningQuests = Lists.newArrayList();

private StorageManager storage;

@Inject
@ConfigDir(sharedRoot = false)
public File configDir;
Expand All @@ -56,6 +55,8 @@ public class PixelBuiltQuests {
public Logger logger;

TaskListener taskListener;
StorageManager storage;
PlaceholderHandler placeholderHandler;

@Listener
public void onPreInit(GamePreInitializationEvent e) {
Expand All @@ -70,6 +71,7 @@ public void onPreInit(GamePreInitializationEvent e) {

@Listener
public void onStarted(GameStartedServerEvent event) {
logger.info("PixelBuilt - Quests is starting!");
ConfigManager.init();

this.taskListener = new TaskListener(this);
Expand All @@ -79,9 +81,14 @@ public void onStarted(GameStartedServerEvent event) {
this.storage = new StorageManager(this);
this.storage.init();

logger.warn("PixelBuilt - Quests is starting!");
CommandManager.registerCommands();
Sponge.getEventManager().registerListeners(this, new Listeners(this));

if (Sponge.getPluginManager().isLoaded("placeholderapi")) {
logger.info("Detected PlaceholderAPI, registering placeholders");
placeholderHandler = new PlaceholderHandler(this);
placeholderHandler.init();
}
}

@Listener
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/online/pixelbuilt/pbquests/commands/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;

public class Run extends BaseCommand {

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
QuestLine line = args.requireOne("quest line");
Quest quest = args.requireOne("quest");
quest.getExecutor().execute(quest, line, args.<Player>getOne("player").orElse((Player) src));
Player player = args.<Player>getOne("player").orElse(null);
if (player == null) {
if (!(src instanceof Player))
throw new CommandException(Text.of(
"You must specify a player to run this command when running it from console!"
));
player = (Player) src;
}
quest.getExecutor().execute(quest, line, player);
return CommandResult.success();
}
}
7 changes: 3 additions & 4 deletions src/main/java/online/pixelbuilt/pbquests/commands/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
PlayerData data = storageManager.getData(((Player) src).getUniqueId());
List<Text> text = Lists.newArrayList();

data.startedQuests.forEach(s -> {
String[] arr = s.split(",");
QuestLine line = ConfigManager.getLine(arr[0]);
Quest quest = ConfigManager.getQuest(Integer.parseInt(arr[1]));
data.getQuestsStarted().forEach(tuple -> {
QuestLine line = tuple.getFirst();
Quest quest = tuple.getSecond();
text.add(Text.of(
TextColors.YELLOW, "* " + line.getName(),
TextColors.GRAY, "/",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package online.pixelbuilt.pbquests.placeholder;

import com.google.common.reflect.TypeToken;
import me.rojo8399.placeholderapi.*;
import online.pixelbuilt.pbquests.PixelBuiltQuests;
import online.pixelbuilt.pbquests.config.ConfigManager;
import online.pixelbuilt.pbquests.config.serialization.ValueWrapper;
import online.pixelbuilt.pbquests.quest.Quest;
import online.pixelbuilt.pbquests.quest.QuestLine;
import online.pixelbuilt.pbquests.storage.sql.PlayerData;
import online.pixelbuilt.pbquests.storage.sql.QuestStatus;
import online.pixelbuilt.pbquests.task.AmountTask;
import online.pixelbuilt.pbquests.task.BaseTask;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.User;

public class PlaceholderHandler {

PixelBuiltQuests plugin;

public PlaceholderHandler(PixelBuiltQuests plugin) {
this.plugin = plugin;
}

public void init() {
PlaceholderService service = Sponge.getServiceManager().provideUnchecked(PlaceholderService.class);
service.loadAll(this, plugin).stream()
.map(builder -> builder.author("Eufranio").version("1.0"))
.forEach(builder -> {
try {
builder.buildAndRegister();
} catch (Exception ex) {
ex.printStackTrace();
}
});
service.registerTypeDeserializer(new TypeToken<QuestLine>(){}, ConfigManager::getLine);
service.registerTypeDeserializer(new TypeToken<Quest>(){}, string -> ConfigManager.getQuest(Integer.parseInt(string)));
service.registerTypeDeserializer(new TypeToken<QuestLineQuest>(){}, string -> {
String[] arr = string.split("_");
if (arr.length != 2)
return null;
return new QuestLineQuest() {{
questLine = ConfigManager.getLine(arr[0]);
quest = ConfigManager.getQuest(Integer.parseInt(arr[1]));
}};
});
service.registerTypeDeserializer(new TypeToken<QuestLineQuestTaskId>(){}, string -> {
String[] arr = string.split("_");
if (arr.length != 3)
return null;
return new QuestLineQuestTaskId() {{
questLine = ConfigManager.getLine(arr[0]);
quest = ConfigManager.getQuest(Integer.parseInt(arr[1]));
taskId = Integer.parseInt(arr[2]);
}};
});
service.registerTypeDeserializer(new TypeToken<QuestTaskId>(){}, string -> {
String[] arr = string.split("_");
if (arr.length != 2)
return null;
return new QuestTaskId() {{
quest = ConfigManager.getQuest(Integer.parseInt(arr[0]));
taskId = Integer.parseInt(arr[1]);
}};
});
}

@Placeholder(id = "pbqtaskneeded")
public Integer taskNeeded(
@Source User user,
@Token QuestLineQuestTaskId data
) {
QuestLine questLine = data.questLine;
Quest quest = data.quest;
int taskId = data.taskId;

BaseTask task = quest.getTask(taskId);
if (!(task instanceof AmountTask))
return null;

AmountTask amountTask = (AmountTask) task;
PlayerData playerData = PixelBuiltQuests.getStorage().getData(user.getUniqueId());
QuestStatus status = playerData.getStatus(amountTask, questLine, quest).orElse(null);

int current = status != null ? status.current : 0;
return amountTask.getTotal() - current;
}

@Placeholder(id = "pbqtaskcurrent")
public Integer taskCurrent(
@Source User user,
@Token QuestLineQuestTaskId data
) {
QuestLine questLine = data.questLine;
Quest quest = data.quest;
int taskId = data.taskId;

BaseTask task = quest.getTask(taskId);
if (!(task instanceof AmountTask))
return null;

AmountTask amountTask = (AmountTask) task;
PlayerData playerData = PixelBuiltQuests.getStorage().getData(user.getUniqueId());
QuestStatus status = playerData.getStatus(amountTask, questLine, quest).orElse(null);

return status != null ? status.current : 0;
}

@Placeholder(id = "pbqtaskpercentage")
public String taskPercentage(
@Source User user,
@Token QuestLineQuestTaskId data
) {
QuestLine questLine = data.questLine;
Quest quest = data.quest;
int taskId = data.taskId;

BaseTask task = quest.getTask(taskId);
if (!(task instanceof AmountTask))
return null;

AmountTask amountTask = (AmountTask) task;
PlayerData playerData = PixelBuiltQuests.getStorage().getData(user.getUniqueId());
QuestStatus status = playerData.getStatus(amountTask, questLine, quest).orElse(null);

if (status == null)
return null;

return amountTask.getPercentageCompleted(status) + "%";
}

@Placeholder(id = "pbqtasktotal")
public Integer taskTotal(
@Token QuestTaskId data
) {
Quest quest = data.quest;
int taskId = data.taskId;

BaseTask task = quest.getTask(taskId);
if (!(task instanceof AmountTask))
return null;

AmountTask amountTask = (AmountTask) task;
return amountTask.getTotal();
}

@Placeholder(id = "pbqquestpercentage")
public String questPercentage(
@Source User user,
@Token QuestLineQuest data
) {
QuestLine questLine = data.questLine;
Quest quest = data.quest;

PlayerData playerData = PixelBuiltQuests.getStorage().getData(user.getUniqueId());
int completed = (int) quest.tasks.stream()
.map(ValueWrapper::getValue)
.filter(task -> task.isCompleted(playerData, questLine, quest))
.count();

double result = (double) completed/ (double) quest.tasks.size();
double percent = result * 100;
return ((int) Math.round(percent)) + "%";
}

@Placeholder(id = "pbqquestlinetotal")
public Integer questLineTotal(@Token QuestLine questLine) {
return questLine.quests.size();
}

@Placeholder(id = "pbqquestlinecompleted")
public Integer questLineCompleted(
@Source User user,
@Token QuestLine questLine
) {
PlayerData playerData = PixelBuiltQuests.getStorage().getData(user.getUniqueId());
int completed = 0;

for (int questId : questLine.quests) {
Quest quest = ConfigManager.getQuest(questId);
if (quest == null)
continue;
if (playerData.hasRan(questLine, quest))
completed++;
}

return completed;
}

@Placeholder(id = "pbqquestlineprogress")
public Integer questLineProgress(
@Source User user,
@Token QuestLine questLine
) {
PlayerData playerData = PixelBuiltQuests.getStorage().getData(user.getUniqueId());
return playerData.getProgress(questLine);
}

public static class QuestLineQuest {
QuestLine questLine;
Quest quest;
}

public static class QuestTaskId {
Quest quest;
int taskId;
}

public static class QuestLineQuestTaskId {
QuestLine questLine;
Quest quest;
int taskId;
}

}
8 changes: 8 additions & 0 deletions src/main/java/online/pixelbuilt/pbquests/quest/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ public int getId() {
public void setId(int id) {
this.id = id;
}

public BaseTask getTask(int id) {
return this.tasks.stream()
.filter(value -> value.getValue().getId() == id)
.map(ValueWrapper::getValue)
.findFirst()
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ public void continueTask() {
reward.execute(playerData, questLine, quest);
}

playerData.startedQuests.remove(this.questLine.getName() + "," + this.quest.getId());
playerData.addQuest(this.questLine, this.quest);
PixelBuiltQuests.getStorage().save(playerData);
}
Expand Down
Loading

0 comments on commit ce51758

Please sign in to comment.