Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Icon" Section to Phase Config. #178

Merged
merged 2 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository>
<repository>
<id>minecraft-repo</id>
<url>https://libraries.minecraft.net/</url>
</repository>
<repository>
<id>codemc</id>
<url>https://repo.codemc.org/repository/maven-snapshots/</url>
Expand All @@ -163,6 +167,13 @@
<version>${spigot.version}</version>
<scope>provided</scope>
</dependency>
<!-- Mojang Auth-->
<dependency>
<groupId>com.mojang</groupId>
<artifactId>authlib</artifactId>
<version>1.5.21</version>
<scope>provided</scope>
</dependency>
<!-- Mockito (Unit testing) -->
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.bukkit.Material;

import org.bukkit.inventory.ItemStack;
import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
Expand Down Expand Up @@ -45,8 +46,8 @@ public boolean execute(User user, String label, List<String> args) {
item.name(user.getTranslation("aoneblock.commands.phases.name-syntax",
TextVariables.NAME, en.getValue().getPhaseName(),
TextVariables.NUMBER, String.valueOf(en.getKey())));
Material material = en.getValue().getFirstBlock() == null ? Material.STONE : en.getValue().getFirstBlock().getMaterial();
item.icon(material);
ItemStack icon = en.getValue().getIconBlock() == null ? en.getValue().getFirstBlock() == null ? new ItemStack(Material.STONE, 1) : new ItemStack(en.getValue().getFirstBlock().getMaterial(), 1) : en.getValue().getIconBlock();
item.icon(icon);
item.description(user.getTranslation("aoneblock.commands.phases.description-syntax",
TextVariables.NAME, en.getValue().getPhaseName(),
TextVariables.NUMBER, String.valueOf(en.getKey())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/**
* Represents something that can be generated when a block is broken
* Can be a block or entity
* @author tastybento
*
* @author tastybento
*/
public class OneBlockObject {

Expand Down Expand Up @@ -39,14 +39,15 @@ public enum Rarity {

private EntityType entityType;
private Material material;
private Map<Integer,ItemStack> chest;
private Map<Integer, ItemStack> chest;
private Rarity rarity;
private int prob;

/**
* An entity
*
* @param entityType - type
* @param prob - relative probability
* @param prob - relative probability
*/
public OneBlockObject(EntityType entityType, int prob) {
this.entityType = entityType;
Expand All @@ -55,20 +56,23 @@ public OneBlockObject(EntityType entityType, int prob) {

/**
* A block
*
* @param material - block type
* @param prob - relative probability
* @param prob - relative probability
*/
public OneBlockObject(Material material, int prob) {
this.material = material;
this.prob = prob;

}


/**
* A chest
*
* @param chest - list of itemstacks in the chest
*/
public OneBlockObject(Map<Integer,ItemStack> chest, Rarity rarity) {
public OneBlockObject(Map<Integer, ItemStack> chest, Rarity rarity) {
this.material = Material.CHEST;
this.chest = chest;
this.setRarity(rarity);
Expand All @@ -77,6 +81,7 @@ public OneBlockObject(Map<Integer,ItemStack> chest, Rarity rarity) {

/**
* Copy constructor
*
* @param ob - OneBlockObject
*/
public OneBlockObject(OneBlockObject ob) {
Expand Down
73 changes: 52 additions & 21 deletions src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockPhase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@
import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.aoneblock.oneblocks.OneBlockObject.Rarity;


public class OneBlockPhase {
protected static final SortedMap<Double, Rarity> CHEST_CHANCES = new TreeMap<>();

static {
CHEST_CHANCES.put(0.62D, Rarity.COMMON);
CHEST_CHANCES.put(0.87D, Rarity.UNCOMMON);
CHEST_CHANCES.put(0.96D, Rarity.RARE);
CHEST_CHANCES.put(1D, Rarity.EPIC);
}

/**
* Tree map of all materials and their probabilities as a ratio to the sum of all probabilities
* Tree map of all materials and their probabilities as a ratio to the sum of
* all probabilities
*/
private final TreeMap<Integer, OneBlockObject> probMap = new TreeMap<>();
/**
Expand All @@ -42,6 +44,7 @@ public class OneBlockPhase {
private Biome phaseBiome;
private Environment environment;
private OneBlockObject firstBlock;
private ItemStack iconBlock;
private final Map<Rarity, List<OneBlockObject>> chests = new EnumMap<>(Rarity.class);
private final Random random = new Random();
private final String blockNumber;
Expand All @@ -53,10 +56,10 @@ public class OneBlockPhase {
private List<Requirement> requirements;
private Map<Integer, OneBlockObject> fixedBlocks;


/**
* Construct a phase that starts at blockNumber. Phase continues forever until
* another phase starts.
*
* @param blockNumber - starting block number
*/
public OneBlockPhase(String blockNumber) {
Expand All @@ -76,6 +79,7 @@ public String getBlockNumber() {

/**
* Get the block number as an integer
*
* @return the integer value of the blockNumber
*/
public int getBlockNumberValue() {
Expand Down Expand Up @@ -114,8 +118,9 @@ public void setPhaseBiome(Biome phaseBiome) {

/**
* Adds a material and associated probability
*
* @param material - Material
* @param prob - probability
* @param prob - probability
*/
public void addBlock(Material material, int prob) {
total += prob;
Expand All @@ -125,8 +130,9 @@ public void addBlock(Material material, int prob) {

/**
* Adds an entity type and associated probability
*
* @param entityType - entityType
* @param prob - probability
* @param prob - probability
*/
public void addMob(EntityType entityType, int prob) {
total += prob;
Expand All @@ -140,15 +146,17 @@ public void addChest(Map<Integer, ItemStack> items, Rarity rarity) {

/**
* This picks a random object
* @param addon AOneBlock
*
* @param addon AOneBlock
* @param blockNumber the block number in the phase requested
* @return OneBlockObject selected
*/

public OneBlockObject getNextBlock(AOneBlock addon, int blockNumber) {
if (total <1) {
addon.logError("Phase " + this.getPhaseName() + " has zero probability of generating blocks. Check config file. Is the block section missing?");
return this.getFirstBlock() != null ? getFirstBlock() : new OneBlockObject(Material.GRASS_BLOCK,1);
if (total < 1) {
addon.logError("Phase " + this.getPhaseName()
+ " has zero probability of generating blocks. Check config file. Is the block section missing?");
return this.getFirstBlock() != null ? getFirstBlock() : new OneBlockObject(Material.GRASS_BLOCK, 1);
}
if (blockNumber == 0 && this.getFirstBlock() != null) {
return getResult(this.getFirstBlock());
Expand All @@ -158,7 +166,9 @@ public OneBlockObject getNextBlock(AOneBlock addon, int blockNumber) {
return getResult(this.getFixedBlocks().get(blockNumber));
}
OneBlockObject block = getRandomBlock(probMap, total);
if (block.isEntity()) return block;
if (block.isEntity()) {
return block;
Fredthedoggy marked this conversation as resolved.
Show resolved Hide resolved
}
return getResult(block);
}

Expand All @@ -168,20 +178,24 @@ private OneBlockObject getResult(OneBlockObject block) {

private OneBlockObject getRandomChest() {
// Get the right type of chest
Rarity r = CHEST_CHANCES.getOrDefault(((TreeMap<Double, Rarity>) CHEST_CHANCES).ceilingKey(random.nextDouble()), Rarity.COMMON);
Rarity r = CHEST_CHANCES.getOrDefault(((TreeMap<Double, Rarity>) CHEST_CHANCES).ceilingKey(random.nextDouble()),
Rarity.COMMON);
// If the chest lists have no common fallback, then return empty chest
if (!chests.containsKey(r) && !chests.containsKey(Rarity.COMMON)) return new OneBlockObject(Material.CHEST, 0);
if (!chests.containsKey(r) && !chests.containsKey(Rarity.COMMON)) {
return new OneBlockObject(Material.CHEST, 0);
Fredthedoggy marked this conversation as resolved.
Show resolved Hide resolved
}
// Get the rare chest or worse case the common one
List<OneBlockObject> list = chests.containsKey(r) ? chests.get(r) : chests.get(Rarity.COMMON);
// Pick one from the list or return an empty chest. Note list.get() can return nothing
// Pick one from the list or return an empty chest. Note list.get() can return
// nothing
return list.isEmpty() ? new OneBlockObject(Material.CHEST, 0) : list.get(random.nextInt(list.size()));
}

private OneBlockObject getRandomBlock(TreeMap<Integer, OneBlockObject> probMap2, int total2) {
// Use +1 on the bound because the random choice is exclusive
OneBlockObject temp = probMap2.get(random.nextInt(total2+1));
OneBlockObject temp = probMap2.get(random.nextInt(total2 + 1));
if (temp == null) {
temp = probMap2.ceilingEntry(random.nextInt(total2+1)).getValue();
temp = probMap2.ceilingEntry(random.nextInt(total2 + 1)).getValue();
}
if (temp == null) {
temp = probMap2.firstEntry().getValue();
Expand Down Expand Up @@ -211,21 +225,35 @@ public OneBlockObject getFirstBlock() {
return firstBlock;
}

/**
* @return the iconBlock
*/
@Nullable
public ItemStack getIconBlock() {
return iconBlock;
}

/**
* @param firstBlock the firstBlock to set
*/
public void setFirstBlock(OneBlockObject firstBlock) {
this.firstBlock = firstBlock;
}

/**
* @param iconBlock the iconBlock to set
*/
public void setIconBlock(ItemStack iconBlock) {
this.iconBlock = iconBlock;
}

/**
* Get all the chests in this phase
*
* @return collection of all the chests
*/
public Collection<OneBlockObject> getChests() {
return chests.values().stream()
.flatMap(List::stream)
.collect(Collectors.toList());
return chests.values().stream().flatMap(List::stream).collect(Collectors.toList());
}

/**
Expand All @@ -237,18 +265,22 @@ public Map<Rarity, List<OneBlockObject>> getChestsMap() {

/**
* Get the mobs that are in this phase
*
* @return map of mob type and its relative probability
*/
public Map<EntityType, Integer> getMobs() {
return probMap.values().stream().filter(OneBlockObject::isEntity).collect(Collectors.toMap(OneBlockObject::getEntityType, OneBlockObject::getProb));
return probMap.values().stream().filter(OneBlockObject::isEntity)
.collect(Collectors.toMap(OneBlockObject::getEntityType, OneBlockObject::getProb));
}

/**
* Get the block materials in this phase
*
* @return map of materials and relative probabilities
*/
public Map<Material, Integer> getBlocks() {
return probMap.values().stream().filter(OneBlockObject::isMaterial).collect(Collectors.toMap(OneBlockObject::getMaterial, OneBlockObject::getProb));
return probMap.values().stream().filter(OneBlockObject::isMaterial)
.collect(Collectors.toMap(OneBlockObject::getMaterial, OneBlockObject::getProb));
}

/**
Expand Down Expand Up @@ -349,5 +381,4 @@ public void setFixedBlocks(Map<Integer, OneBlockObject> fixedBlocks) {
this.fixedBlocks = fixedBlocks;
}


}
Loading