Skip to content

Commit

Permalink
Fix blocks being converted into items when computing the island level
Browse files Browse the repository at this point in the history
This is no longer supported in Minecraft 1.21 for materials that cannot be items such as lava or potted plants.
Fixes #62
  • Loading branch information
minoneer committed Dec 19, 2024
1 parent 8644963 commit 64cb062
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package us.talabrek.ultimateskyblock.api.model;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.inventory.ItemStack;

/**
Expand All @@ -14,8 +16,25 @@ public interface BlockScore {
*
* @return The type of block.
* @since v2.1.2
* @deprecated Converting a BlockData to an ItemStack is not supported in Minecraft. Use #getBlockData() instead.
*/
ItemStack getBlock();
@Deprecated(since = "v3.1.0")
default ItemStack getBlock() {
Material material = getBlockData().getMaterial();
if (material.isItem()) {
return new ItemStack(material);
} else {
throw new UnsupportedOperationException("BlockData is not an item. Use getBlockData() instead.");
}
}

/**
* The type of block.
*
* @return The type of block.
* @since v3.1.0
*/
BlockData getBlockData();

/**
* The number of blocks of this type found on the island.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import us.talabrek.ultimateskyblock.Settings;
import us.talabrek.ultimateskyblock.api.async.Callback;
import us.talabrek.ultimateskyblock.api.model.BlockScore;
import us.talabrek.ultimateskyblock.api.model.IslandScore;
import us.talabrek.ultimateskyblock.island.IslandInfo;
import us.talabrek.ultimateskyblock.player.PatienceTester;
import us.talabrek.ultimateskyblock.player.PlayerInfo;
Expand Down Expand Up @@ -65,7 +66,7 @@ public boolean getIslandInfo(final Player player, final String islandPlayer, fin
return false;
}
final PlayerInfo playerInfo = islandPlayer.equals(player.getName()) ? plugin.getPlayerInfo(player) : plugin.getPlayerInfo(islandPlayer);
final Callback<us.talabrek.ultimateskyblock.api.model.IslandScore> showInfo = new Callback<us.talabrek.ultimateskyblock.api.model.IslandScore>() {
final Callback<IslandScore> showInfo = new Callback<>() {
@Override
public void run() {
if (player.isOnline()) {
Expand All @@ -82,8 +83,8 @@ public void run() {
player.sendMessage(tr("Score Count Block"));
for (BlockScore score : getState().getTop((currentPage - 1) * 10, 10)) {
player.sendMessage(score.getState().getColor() + tr("{0,number,00.00} {1,number,#} {2}",
score.getScore(), score.getCount(),
ItemStackUtil.getItemName(score.getBlock())));
score.getScore(), score.getCount(),
ItemStackUtil.getBlockName(score.getBlockData())));
}
player.sendMessage(tr("\u00a7aIsland level is {0,number,###.##}", getState().getScore()));
}
Expand All @@ -101,6 +102,4 @@ public void run() {
}, 1L);
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void updateBlockCount(Location islandLocation, IslandScore score) {
private Map<Material, Integer> asBlockCount(IslandScore score) {
Map<Material, Integer> countMap = new ConcurrentHashMap<>();
for (BlockScore blockScore : score.getTop()) {
Material type = blockScore.getBlock().getType();
Material type = blockScore.getBlockData().getMaterial();
if (blockLimits.containsKey(type)) {
int initalValue = countMap.getOrDefault(type, 0);
initalValue += blockScore.getCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public BlockScore calculateScore(int count, double pointsPerLevel) {
adjustedCount = dReturns(adjustedCount, diminishingReturns);
}
double blockScore = adjustedCount * scorePerBlock;
return new BlockScoreImpl(baseBlock.asItemStack(), count, blockScore/pointsPerLevel, state);
return new BlockScoreImpl(baseBlock.getType().createBlockData(), count, blockScore/pointsPerLevel, state);
}

private double dReturns(final double val, final double scale) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package us.talabrek.ultimateskyblock.island.level;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

/**
* Holds the identification of a unit to be matched against a block
Expand All @@ -17,10 +16,6 @@ public Material getType() {
return type;
}

public ItemStack asItemStack() {
return new ItemStack(type, 1);
}

public boolean matches(Material material) {
return this.type == material;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int compare(BlockScore o1, BlockScore o2) {
cmp = o2.getCount() - o1.getCount();
}
if (cmp == 0) {
cmp = ItemStackUtil.getItemName(o2.getBlock()).compareTo(ItemStackUtil.getItemName(o1.getBlock()));
cmp = ItemStackUtil.getBlockName(o2.getBlockData()).compareTo(ItemStackUtil.getBlockName(o1.getBlockData()));
}
return cmp;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package us.talabrek.ultimateskyblock.island.level;

import dk.lockfuglsang.minecraft.util.ItemStackUtil;
import org.bukkit.inventory.ItemStack;
import org.bukkit.block.data.BlockData;


public class BlockScoreImpl implements us.talabrek.ultimateskyblock.api.model.BlockScore {
private final ItemStack block;
private final BlockData block;
private final int count;
private final double score;
private final State state;
private final String name;

public BlockScoreImpl(ItemStack block, int count, double score, State state) {
public BlockScoreImpl(BlockData block, int count, double score, State state) {
this(block, count, score, state, null);
}

public BlockScoreImpl(ItemStack block, int count, double score, State state, String name) {
public BlockScoreImpl(BlockData block, int count, double score, State state, String name) {
this.block = block;
this.count = count;
this.score = score;
this.state = state;
this.name = name != null ? name : ItemStackUtil.getItemName(getBlock());
this.name = name != null ? name : ItemStackUtil.getBlockName(getBlockData());
}

@Override
Expand All @@ -34,9 +34,8 @@ public String toString() {
'}';
}


@Override
public ItemStack getBlock() {
public BlockData getBlockData() {
return block;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import us.talabrek.ultimateskyblock.api.model.BlockScore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -42,7 +41,7 @@ private BlockScoreImpl add(BlockScore score, BlockScore existing) {
if (score.getState().ordinal() > existing.getState().ordinal()) {
state = existing.getState();
}
return new BlockScoreImpl(existing.getBlock(),
return new BlockScoreImpl(existing.getBlockData(),
score.getCount() + existing.getCount(),
score.getScore() + existing.getScore(), state, score.getName());
}
Expand Down Expand Up @@ -70,7 +69,7 @@ public List<BlockScore> getTop(int offset, int num) {
throw new IllegalArgumentException("Offset must be a non-negative integer.");
}
if (!isSorted) {
Collections.sort(top, new BlockScoreComparator());
top.sort(new BlockScoreComparator());
isSorted = true;
}
return top.subList(offset, Math.min(offset+num, top.size()));
Expand Down

0 comments on commit 64cb062

Please sign in to comment.