Skip to content

Commit

Permalink
Create Statistic Requirement for Challenges addon.
Browse files Browse the repository at this point in the history
Statistic requirement is a new type of challenge that is based on Statistic page for clients.
  • Loading branch information
BONNe committed Aug 14, 2021
1 parent f75cfa9 commit c63087c
Show file tree
Hide file tree
Showing 10 changed files with 1,091 additions and 117 deletions.
98 changes: 98 additions & 0 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -1287,6 +1290,101 @@ private void addLogEntry(@NonNull String storageDataID, @NonNull LogEntry entry)
// ---------------------------------------------------------------------


/**
* Gets statistic data.
*
* @param user the user
* @param world the world
* @param statistic the statistic
* @return the statistic data
*/
public int getStatisticData(User user, World world, Statistic statistic)
{
if (this.settings.isStoreAsIslandData())
{
Island island = this.addon.getIslands().getIsland(world, user);

if (island == null)
{
return 0;
}

return island.getMemberSet().stream().map(Bukkit::getPlayer).
filter(Objects::nonNull).
mapToInt(player -> player.getStatistic(statistic)).
sum();
}
else
{
return user.getPlayer().getStatistic(statistic);
}
}


/**
* Gets statistic data.
*
* @param user the user
* @param world the world
* @param statistic the statistic
* @param material the material
* @return the statistic data
*/
public int getStatisticData(User user, World world, Statistic statistic, Material material)
{
if (this.settings.isStoreAsIslandData())
{
Island island = this.addon.getIslands().getIsland(world, user);

if (island == null)
{
return 0;
}

return island.getMemberSet().stream().map(Bukkit::getPlayer).
filter(Objects::nonNull).
mapToInt(player -> player.getStatistic(statistic, material)).
sum();
}
else
{
return user.getPlayer().getStatistic(statistic, material);
}
}


/**
* Gets statistic data.
*
* @param user the user
* @param world the world
* @param statistic the statistic
* @param entity the entity
* @return the statistic data
*/
public int getStatisticData(User user, World world, Statistic statistic, EntityType entity)
{
if (this.settings.isStoreAsIslandData())
{
Island island = this.addon.getIslands().getIsland(world, user);

if (island == null)
{
return 0;
}

return island.getMemberSet().stream().map(Bukkit::getPlayer).
filter(Objects::nonNull).
mapToInt(player -> player.getStatistic(statistic, entity)).
sum();
}
else
{
return user.getPlayer().getStatistic(statistic, entity);
}
}


/**
* This method returns if given user has completed given challenge in world.
* @param user - User that must be checked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public enum ChallengeType
* other plugins to be setup before it could work.
*/
OTHER,

/**
* Challenge based on player statistic data.
*/
STATISTIC
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//
// Created by BONNe
// Copyright - 2021
//


package world.bentobox.challenges.database.object.requirements;


import com.google.gson.annotations.Expose;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;


public class StatisticRequirements extends Requirements
{
/**
* Constructor Requirements creates a new Requirements instance.
*/
public StatisticRequirements()
{
// Empty constructor
}


/**
* This method clones given statistic object.
* @return Clone of this object.
*/
@Override
public Requirements clone()
{
StatisticRequirements requirements = new StatisticRequirements();
requirements.setStatistic(this.statistic);
requirements.setEntity(this.entity);
requirements.setMaterial(this.material);
requirements.setAmount(this.amount);
requirements.setReduceStatistic(this.reduceStatistic);

return requirements;
}


@Override
public boolean isValid()
{
if (!super.isValid())
{
return false;
}

if (this.statistic == null)
{
return false;
}

switch (this.statistic.getType())
{
case ITEM -> {
return this.material != null && this.material.isItem();
}
case BLOCK -> {
return this.material != null && this.material.isBlock();
}
case ENTITY -> {
return this.entity != null;
}
}

return true;
}


// ---------------------------------------------------------------------
// Section: Getters and setters
// ---------------------------------------------------------------------


/**
* Gets statistic.
*
* @return the statistic
*/
public Statistic getStatistic()
{
return statistic;
}


/**
* Sets statistic.
*
* @param statistic the statistic
*/
public void setStatistic(Statistic statistic)
{
this.statistic = statistic;
}


/**
* Gets entity.
*
* @return the entity
*/
public EntityType getEntity()
{
return entity;
}


/**
* Sets entity.
*
* @param entity the entity
*/
public void setEntity(EntityType entity)
{
this.entity = entity;
}


/**
* Gets material.
*
* @return the material
*/
public Material getMaterial()
{
return material;
}


/**
* Sets material.
*
* @param material the material
*/
public void setMaterial(Material material)
{
this.material = material;
}


/**
* Gets amount.
*
* @return the amount
*/
public int getAmount()
{
return amount;
}


/**
* Sets amount.
*
* @param amount the amount
*/
public void setAmount(int amount)
{
this.amount = amount;
}


/**
* Is reduce statistic boolean.
*
* @return the boolean
*/
public boolean isReduceStatistic()
{
return reduceStatistic;
}


/**
* Sets reduce statistic.
*
* @param reduceStatistic the reduce statistic
*/
public void setReduceStatistic(boolean reduceStatistic)
{
this.reduceStatistic = reduceStatistic;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------

/**
* Type of the statistic field.
*/
@Expose
private Statistic statistic;

/**
* Type of entity for entity related statistics.
*/
@Expose
private EntityType entity;

/**
* Type of material for block and item related statistics.
*/
@Expose
private Material material;

/**
* Amount of the stats.
*/
@Expose
private int amount;

/**
* Indicate that player statistic fields must be adjusted after completing challenges.
*/
@Expose
private boolean reduceStatistic;
}

0 comments on commit c63087c

Please sign in to comment.