Skip to content

Commit

Permalink
Merge pull request #372 from BentoBoxWorld/multi_statistics
Browse files Browse the repository at this point in the history
Enable challenges to have multiple statistic requirements
  • Loading branch information
tastybento authored Feb 10, 2025
2 parents cf4dcaf + 183368b commit 0bd1c79
Show file tree
Hide file tree
Showing 9 changed files with 1,044 additions and 561 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// Created by BONNe
// Copyright - 2021
//
// Enhanced by tastybento


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


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
Expand All @@ -15,215 +20,121 @@
import com.google.gson.annotations.Expose;


/**
* Requirements for statistics based challenges
*/
public class StatisticRequirements extends Requirements
{
/**
* Constructor Requirements creates a new Requirements instance.
* Record for this requirement
* @param Statistic statistic
* @param EntityType entity
* @param Material material
* @param Integer amount
* @param Boolean reduceStatistic
*/
public StatisticRequirements()
{
// Empty constructor
public record StatisticRec(@Expose Statistic statistic, @Expose EntityType entity, @Expose Material material,
@Expose Integer amount, @Expose Boolean reduceStatistic) {
}


/**
* This method copies given statistic object.
* @return Copy of this object.
* Type of the statistic field.
* @deprecated Shifting to a list
*/
@Override
public Requirements copy()
{
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;
}

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

case BLOCK -> this.material != null && this.material.isBlock();

case ENTITY -> this.entity != null;

case UNTYPED -> true;

};

}


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

@Expose
@Nullable
private Statistic statistic;

/**
* Gets statistic.
*
* @return the statistic
* Type of entity for entity related statistics.
* @deprecated Shifting to a list
*/
@Expose
@Nullable
public Statistic getStatistic()
{
return statistic;
}

private EntityType entity;

/**
* Sets statistic.
*
* @param statistic the statistic
* Type of material for block and item related statistics.
* @deprecated Shifting to a list
*/
public void setStatistic(@Nullable Statistic statistic)
{
this.statistic = statistic;
}

@Expose
@Nullable
private Material material;

/**
* Gets entity.
*
* @return the entity
* Amount of the stats.
* @deprecated Shifting to a list
*/
@Nullable
public EntityType getEntity()
{
return entity;
}

@Expose
private Integer amount;

/**
* Sets entity.
*
* @param entity the entity
* Indicate that player statistic fields must be adjusted after completing challenges.
* @deprecated Shifting to a list
*/
public void setEntity(@Nullable EntityType entity)
{
this.entity = entity;
}

@Expose
private Boolean reduceStatistic;

/**
* Gets material.
*
* @return the material
* List of statistics that must be done for this challenge
*/
@Expose
@Nullable
public Material getMaterial()
{
return material;
}
private List<StatisticRec> statisticList;


/**
* Sets material.
*
* @param material the material
* Constructor Requirements creates a new Requirements instance.
*/
public void setMaterial(@Nullable Material material)
public StatisticRequirements()
{
this.material = material;
// Empty constructor
}


/**
* Gets amount.
*
* @return the amount
* This method copies given statistic object.
* @return Copy of this object.
*/
public int getAmount()
@Override
public Requirements copy()
{
return amount;
StatisticRequirements requirements = new StatisticRequirements();
requirements.setStatisticList(this.getRequiredStatistics());
return requirements;
}


/**
* Sets amount.
*
* @param amount the amount
*/
public void setAmount(int amount)
@Override
public boolean isValid()
{
this.amount = amount;
// TODO - do something here?
return super.isValid();
}


/**
* Is reduce statistic boolean.
*
* @return the boolean
* @return the statisticList
*/
public boolean isReduceStatistic()
{
return reduceStatistic;
public List<StatisticRec> getRequiredStatistics() {
if (statisticList == null) {
statisticList = new ArrayList<>();
// Convert old single statistic entries to new list of records
if (statistic != null) {
StatisticRec rec = new StatisticRec(this.statistic, this.entity, this.material, this.amount,
this.reduceStatistic);
statisticList.add(rec);
}
}
return statisticList;
}


/**
* Sets reduce statistic.
*
* @param reduceStatistic the reduce statistic
* @param value the statisticList to set
*/
public void setReduceStatistic(boolean reduceStatistic)
{
this.reduceStatistic = reduceStatistic;
public void setStatisticList(Collection<StatisticRec> value) {
// If value is null, assign null; otherwise, create a new ArrayList from value.
this.statisticList = (value == null) ? null : new ArrayList<>(value);
}


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

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

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

/**
* Type of material for block and item related statistics.
*/
@Expose
@Nullable
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import world.bentobox.challenges.database.object.requirements.IslandRequirements;
import world.bentobox.challenges.database.object.requirements.OtherRequirements;
import world.bentobox.challenges.database.object.requirements.StatisticRequirements;
import world.bentobox.challenges.database.object.requirements.StatisticRequirements.StatisticRec;
import world.bentobox.challenges.utils.Constants;
import world.bentobox.challenges.utils.Utils;

Expand Down Expand Up @@ -356,13 +357,12 @@ private void populateRequirements(Challenge challenge, ConfigurationSection sect
case STATISTIC_TYPE -> {
StatisticRequirements requirements = new StatisticRequirements();
challenge.setRequirements(requirements);

requirements.setAmount(section.getInt("amount", 0));
requirements.setReduceStatistic(section.getBoolean("reduce", false));

requirements.setStatistic(matchStatistic(section.getString("statistic")));
requirements.setEntity(matchEntity(section.getString("entity")));
requirements.setMaterial(matchMaterial(section.getString("material")));
List<StatisticRec> list = new ArrayList<>();
list.add(new StatisticRec(matchStatistic(section.getString("statistic")),
matchEntity(section.getString("entity")), matchMaterial(section.getString("material")),
section.getInt("amount", 0), section.getBoolean("reduce", false)));
// TODO: Add support for multiple stat challenge
requirements.setStatisticList(list);
}
}

Expand Down
Loading

0 comments on commit 0bd1c79

Please sign in to comment.