Skip to content

Commit

Permalink
Rework TryToComplete. (#109)
Browse files Browse the repository at this point in the history
* Rework TryToComplete.

Implement ability to complete challenge multiple times at once.
To do it, I split everything in checking/removing/rewarding steps.

In checking step, it calculates if it is possible to complete with minimal requirements and then calculates maximal repeating factor.
In removing step, it removes everything that is necessary.
In rewarding step, it give rewards by necessary factor (multilayer).

I rework item/block/entity removing as factors may be influenced at the last element, so I improve everything by memory usage. Create necessary sets/lists/queues for faster access to already collected items.

* Add method that returns removed items, if somehow algorithm did not manage to remove all items.
Fix issue when removeItems method did not merge ItemStacks as they had different item amount.

Return and fix TryToCompleteTest.

* Implement Multiple Challenge Completion command and GUI. (#73)

/[gamemode] challenges complete [number] allows to complete challenges [number] amount (or less if not enough items)
Via GUI users can right click on challenge and if it is repeatable, then it will open AnvilGUI that accepts only numbers as input.
  • Loading branch information
BONNe committed May 6, 2019
1 parent d9486bb commit c8088f2
Show file tree
Hide file tree
Showing 7 changed files with 804 additions and 329 deletions.
25 changes: 20 additions & 5 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,23 @@ private boolean isChallengeComplete(String storageDataID, String challengeID)
* @param challengeID - challengeID
*/
private void setChallengeComplete(@NonNull String storageDataID, @NonNull String challengeID)
{
this.setChallengeComplete(storageDataID, challengeID, 1);
}


/**
* Sets the challenge with given ID as complete and increments the number of times it has been
* completed
*
* @param storageDataID - playerData ID
* @param challengeID - challengeID
* @param count - how many times challenge is completed
*/
private void setChallengeComplete(@NonNull String storageDataID, @NonNull String challengeID, int count)
{
this.addPlayerData(storageDataID);
this.playerCacheData.get(storageDataID).setChallengeDone(challengeID);
this.playerCacheData.get(storageDataID).addChallengeDone(challengeID, count);
// Save
this.savePlayerData(storageDataID);
}
Expand Down Expand Up @@ -842,9 +856,9 @@ public boolean isChallengeComplete(UUID user, World world, String challengeID)
* @param world - World where completion must be called.
* @param challenge - That must be completed.
*/
public void setChallengeComplete(User user, World world, Challenge challenge)
public void setChallengeComplete(User user, World world, Challenge challenge, int completionCount)
{
this.setChallengeComplete(user.getUniqueId(), world, challenge);
this.setChallengeComplete(user.getUniqueId(), world, challenge, completionCount);
}


Expand All @@ -854,21 +868,22 @@ public void setChallengeComplete(User user, World world, Challenge challenge)
* @param world - World where completion must be called.
* @param challenge - That must be completed.
*/
public void setChallengeComplete(UUID userID, World world, Challenge challenge)
public void setChallengeComplete(UUID userID, World world, Challenge challenge, int completionCount)
{
String storageID = this.getDataUniqueID(userID, Util.getWorld(world));
this.setChallengeComplete(storageID, challenge.getUniqueId());
this.addLogEntry(storageID, new LogEntry.Builder("COMPLETE").
data("user-id", userID.toString()).
data("challenge-id", challenge.getUniqueId()).
data("completion-count", Integer.toString(completionCount)).
build());

// Fire event that user completes challenge
Bukkit.getServer().getPluginManager().callEvent(
new ChallengeCompletedEvent(challenge.getUniqueId(),
userID,
false,
1));
completionCount));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
});

break;
// TODO: not implemented YET
// case 4:
// // Suggest a number of completions.
// if (lastString.isEmpty() || lastString.matches("[0-9]*"))
// {
// returnList.addAll(Util.tabLimit(Collections.singletonList("<number>"), lastString));
// }
//
// break;
case 4:
// Suggest a number of completions.
if (lastString.isEmpty() || lastString.matches("[0-9]*"))
{
returnList.addAll(Util.tabLimit(Collections.singletonList("<number>"), lastString));
}

break;
default:
{
returnList.addAll(Util.tabLimit(Collections.singletonList("help"), lastString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,21 @@ public void reset(@NonNull String worldName)
*/
public void setChallengeDone(@NonNull String challengeName)
{
int times = challengeStatus.getOrDefault(challengeName, 0) + 1;
challengeStatus.put(challengeName, times);
this.addChallengeDone(challengeName, 1);
}


/**
* Mark a challenge as having been completed. Will increment the number of times and
* timestamp
*
* @param challengeName - unique challenge name
* @param times - how many new times should be added
*/
public void addChallengeDone(@NonNull String challengeName, int times)
{
int newTimes = challengeStatus.getOrDefault(challengeName, 0) + times;
challengeStatus.put(challengeName, newTimes);
challengesTimestamp.put(challengeName, System.currentTimeMillis());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.inventory.ItemStack;
import java.util.List;

import net.wesjd.anvilgui.AnvilGUI;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
Expand Down Expand Up @@ -356,14 +357,47 @@ private PanelItem getChallengeButton(Challenge challenge)
description(GuiUtils.stringSplit(this.generateChallengeDescription(challenge, this.user.getPlayer()),
this.addon.getChallengesSettings().getLoreLineLength())).
clickHandler((panel, user1, clickType, slot) -> {
if (TryToComplete.complete(this.addon,
this.user,
challenge,
this.world,
this.topLabel,
this.permissionPrefix))

// Add ability to input how many repeats player should do.
// Do not open if challenge is not repeatable.
if (clickType.isRightClick() && challenge.isRepeatable())
{
new AnvilGUI(this.addon.getPlugin(),
this.user.getPlayer(),
"1",
(player, reply) -> {
try
{
if (TryToComplete.complete(this.addon,
this.user,
challenge,
this.world,
this.topLabel,
this.permissionPrefix,
Integer.parseInt(reply)))
{
panel.getInventory().setItem(slot, this.getChallengeButton(challenge).getItem());
}
}
catch (Exception e)
{
this.user.sendMessage("challenges.errors.not-a-integer", "[value]", reply);
}

return reply;
});
}
else
{
panel.getInventory().setItem(slot, this.getChallengeButton(challenge).getItem());
if (TryToComplete.complete(this.addon,
this.user,
challenge,
this.world,
this.topLabel,
this.permissionPrefix))
{
panel.getInventory().setItem(slot, this.getChallengeButton(challenge).getItem());
}
}

return true;
Expand Down

0 comments on commit c8088f2

Please sign in to comment.