Skip to content

Commit

Permalink
Improve debugging in Creative GameMode.
Browse files Browse the repository at this point in the history
Add ability to Creative GameMode complete inventory and exp challenge without necessary items and exp.
  • Loading branch information
BONNe committed Feb 14, 2019
1 parent 40d33f4 commit 68b0fa3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>world.bentobox</groupId>
<artifactId>challenges</artifactId>
<version>0.5.0</version>
<version>0.6.0-SNAPSHOT</version>

<name>Challenges</name>
<description>Challenges is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like SkyBlock, AcidIsland or CaveBlock.</description>
Expand Down
91 changes: 52 additions & 39 deletions src/main/java/world/bentobox/challenges/panel/TryToComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package world.bentobox.challenges.panel;


import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -486,50 +487,57 @@ private ChallengeResult checkInventory()
{
// Run through inventory
List<ItemStack> required = new ArrayList<>(this.challenge.getRequiredItems());
for (ItemStack req : required)

// Players in creative game mode has got all items. No point to search for them.
if (this.user.getPlayer().getGameMode() != GameMode.CREATIVE)
{
// Check for FIREWORK_ROCKET, ENCHANTED_BOOK, WRITTEN_BOOK, POTION and FILLED_MAP because these have unique meta when created
switch (req.getType())
for (ItemStack req : required)
{
case FIREWORK_ROCKET:
case ENCHANTED_BOOK:
case WRITTEN_BOOK:
case FILLED_MAP:
// Get how many items are in the inventory. Item stacks amounts need to be summed
int numInInventory =
Arrays.stream(this.user.getInventory().getContents()).filter(Objects::nonNull).
filter(i -> i.getType().equals(req.getType())).
mapToInt(ItemStack::getAmount).
sum();

if (numInInventory < req.getAmount())
{
this.user.sendMessage("challenges.errors.not-enough-items",
"[items]",
Util.prettifyText(req.getType().toString()));
return EMPTY_RESULT;
}
break;
default:
// General checking
if (!this.user.getInventory().containsAtLeast(req, req.getAmount()))
{
this.user.sendMessage("challenges.errors.not-enough-items",
"[items]",
Util.prettifyText(req.getType().toString()));
return EMPTY_RESULT;
}
// Check for FIREWORK_ROCKET, ENCHANTED_BOOK, WRITTEN_BOOK, POTION and
// FILLED_MAP because these have unique meta when created
switch (req.getType())
{
case FIREWORK_ROCKET:
case ENCHANTED_BOOK:
case WRITTEN_BOOK:
case FILLED_MAP:
// Get how many items are in the inventory. Item stacks amounts need to be summed
int numInInventory =
Arrays.stream(this.user.getInventory().getContents()).
filter(Objects::nonNull).
filter(i -> i.getType().equals(req.getType())).
mapToInt(ItemStack::getAmount).
sum();

if (numInInventory < req.getAmount())
{
this.user.sendMessage("challenges.errors.not-enough-items",
"[items]",
Util.prettifyText(req.getType().toString()));
return EMPTY_RESULT;
}
break;
default:
// General checking
if (!this.user.getInventory().containsAtLeast(req, req.getAmount()))
{
this.user.sendMessage("challenges.errors.not-enough-items",
"[items]",
Util.prettifyText(req.getType().toString()));
return EMPTY_RESULT;
}
break;
}
}
}

// If remove items, then remove them
// If remove items, then remove them

if (this.challenge.isTakeItems())
{
this.removeItems(required);
if (this.challenge.isTakeItems())
{
this.removeItems(required);
}
}


// Return the result
return new ChallengeResult().setMeetsRequirements().setRepeat(
this.manager.isChallengeComplete(this.user, this.challenge));
Expand Down Expand Up @@ -790,8 +798,11 @@ else if (this.challenge.getRequiredExperience() < 0)
{
this.user.sendMessage("challenges.errors.incorrect");
}
else if (this.user.getPlayer().getTotalExperience() < this.challenge.getRequiredExperience())
else if (this.user.getPlayer().getTotalExperience() < this.challenge.getRequiredExperience() &&
this.user.getPlayer().getGameMode() != GameMode.CREATIVE)
{
// Players in creative gamemode has infinite amount of EXP.

this.user.sendMessage("challenges.errors.not-enough-experience",
"[value]",
Integer.toString(this.challenge.getRequiredExperience()));
Expand All @@ -810,8 +821,10 @@ else if (this.addon.isLevelProvided() &&
this.addon.getEconomyProvider().withdraw(this.user, this.challenge.getRequiredMoney());
}

if (this.challenge.isTakeExperience())
if (this.challenge.isTakeExperience() &&
this.user.getPlayer().getGameMode() != GameMode.CREATIVE)
{
// Cannot take anything from creative game mode.
this.user.getPlayer().setTotalExperience(
this.user.getPlayer().getTotalExperience() - this.challenge.getRequiredExperience());
}
Expand Down

0 comments on commit 68b0fa3

Please sign in to comment.