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

fix(wool): use platform-specific ColorUtils for wool matching #1364

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions core/src/main/java/tc/oc/pgm/wool/MonumentWool.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected boolean canPlayerUpdateProximity(ParticipantState player) {
@Override
protected boolean canBlockUpdateProximity(BlockState oldState, BlockState newState) {
// If monument proximity metric is closest block, make it only the wool
return !hasTouched(getOwner()) || this.getDefinition().isObjectiveWool(newState.getData());
return !hasTouched(getOwner()) || this.getDefinition().isObjectiveWool(newState);
}

public void handleWoolAcquisition(Player player, ItemStack item) {
Expand All @@ -129,7 +129,8 @@ public void onItemKitApplication(ApplyItemKitEvent event) {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onArmorKitApplication(ApplyKitEvent event) {
if (event.getKit() instanceof ArmorKit) {
for (ArmorKit.ArmorItem armorPiece : ((ArmorKit) event.getKit()).getArmor().values()) {
for (ArmorKit.ArmorItem armorPiece :
((ArmorKit) event.getKit()).getArmor().values()) {
handleWoolAcquisition(event.getPlayer().getBukkit(), armorPiece.stack);
}
}
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/tc/oc/pgm/wool/MonumentWoolFactory.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package tc.oc.pgm.wool;

import static net.kyori.adventure.text.Component.text;
import static tc.oc.pgm.wool.WoolModule.WOOL;

import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils;
import org.bukkit.DyeColor;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Wool;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.feature.FeatureInfo;
Expand All @@ -18,6 +19,7 @@
import tc.oc.pgm.goals.ShowOptions;
import tc.oc.pgm.teams.TeamFactory;
import tc.oc.pgm.util.bukkit.BukkitUtils;
import tc.oc.pgm.util.material.ColorUtils;
import tc.oc.pgm.util.text.TextFormatter;

@FeatureInfo(name = "wool")
Expand Down Expand Up @@ -119,11 +121,11 @@ public boolean isCraftable() {
}

public boolean isObjectiveWool(ItemStack stack) {
return stack != null && this.isObjectiveWool(stack.getData());
return stack != null && WOOL.matches(stack) && ColorUtils.COLOR_UTILS.isColor(stack, color);
}

public boolean isObjectiveWool(org.bukkit.material.MaterialData material) {
return material instanceof Wool && ((Wool) material).getColor() == this.color;
public boolean isObjectiveWool(BlockState state) {
return WOOL.matches(state) && ColorUtils.COLOR_UTILS.isColor(state, color);
}

public boolean isHolding(InventoryHolder holder) {
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/tc/oc/pgm/wool/WoolMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static net.kyori.adventure.text.Component.translatable;
import static tc.oc.pgm.util.material.ColorUtils.COLOR_UTILS;
import static tc.oc.pgm.wool.WoolModule.WOOL;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -40,7 +41,6 @@
import tc.oc.pgm.goals.events.GoalStatusChangeEvent;
import tc.oc.pgm.teams.Team;
import tc.oc.pgm.util.block.BlockVectors;
import tc.oc.pgm.util.material.MaterialMatcher;

@ListenerScope(MatchScope.RUNNING)
public class WoolMatchModule implements MatchModule, Listener {
Expand All @@ -61,8 +61,6 @@ public class WoolMatchModule implements MatchModule, Listener {
// layout of the wools in the inventory. This is used to refill the container with wools.
private final Map<Inventory, Map<Integer, ItemStack>> woolChests = new HashMap<>();

private static final MaterialMatcher WOOL = MaterialMatcher.of(m -> m.name().endsWith("WOOL"));

private static final int REFILL_INTERVAL = 30; // seconds

public WoolMatchModule(Match match, Multimap<Team, MonumentWool> wools) {
Expand Down
40 changes: 19 additions & 21 deletions core/src/main/java/tc/oc/pgm/wool/WoolModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import tc.oc.pgm.teams.TeamMatchModule;
import tc.oc.pgm.teams.TeamModule;
import tc.oc.pgm.teams.Teams;
import tc.oc.pgm.util.material.MaterialMatcher;
import tc.oc.pgm.util.xml.InvalidXMLException;
import tc.oc.pgm.util.xml.Node;
import tc.oc.pgm.util.xml.XMLUtils;

public class WoolModule implements MapModule<WoolMatchModule> {
static final MaterialMatcher WOOL = MaterialMatcher.of(m -> m.name().endsWith("WOOL"));
TTtie marked this conversation as resolved.
Show resolved Hide resolved
private static final Collection<MapTag> TAGS =
ImmutableList.of(new MapTag("wool", Gamemode.CAPTURE_THE_WOOL, false));

Expand Down Expand Up @@ -98,36 +100,32 @@ public WoolModule parse(MapFactory factory, Logger logger, Document doc)
ShowOptions options = ShowOptions.parse(factory.getFilters(), woolEl);
Boolean required = XMLUtils.parseBoolean(woolEl.getAttribute("required"), null);

ProximityMetric woolProximityMetric =
ProximityMetric.parse(
woolEl, "wool", new ProximityMetric(ProximityMetric.Type.CLOSEST_KILL, false));
ProximityMetric monumentProximityMetric =
ProximityMetric.parse(
woolEl, "monument", new ProximityMetric(ProximityMetric.Type.CLOSEST_BLOCK, false));
ProximityMetric woolProximityMetric = ProximityMetric.parse(
woolEl, "wool", new ProximityMetric(ProximityMetric.Type.CLOSEST_KILL, false));
ProximityMetric monumentProximityMetric = ProximityMetric.parse(
woolEl, "monument", new ProximityMetric(ProximityMetric.Type.CLOSEST_BLOCK, false));

Vector location;
if (factory.getProto().isOlderThan(MapProtos.WOOL_LOCATIONS)) {
// The default location is at infinity, so players/blocks are always an infinite distance
// from it
location =
new Vector(
Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
location = new Vector(
Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
} else {
location = XMLUtils.parseVector(XMLUtils.getRequiredAttribute(woolEl, "location"));
}

MonumentWoolFactory wool =
new MonumentWoolFactory(
id,
required,
options,
team,
woolProximityMetric,
monumentProximityMetric,
color,
location,
placement,
craftable);
MonumentWoolFactory wool = new MonumentWoolFactory(
id,
required,
options,
team,
woolProximityMetric,
monumentProximityMetric,
color,
location,
placement,
craftable);
factory.getFeatures().addFeature(woolEl, wool);
woolFactories.put(team, wool);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,18 @@ public void setColor(Block block, DyeColor color) {
block.setType(setColor(block.getType(), color));
}

private boolean setAndCompare(Material mat, DyeColor color) {
return setColor(mat, color) == mat;
}

TTtie marked this conversation as resolved.
Show resolved Hide resolved
@Override
public boolean isColor(BlockState block, DyeColor color) {
return setColor(block.getType(), color) == block.getType();
return setAndCompare(block.getType(), color);
}

@Override
public boolean isColor(ItemStack stack, DyeColor color) {
return setAndCompare(stack.getType(), color);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public boolean isColor(BlockState block, DyeColor color) {
return color.getWoolData() == block.getRawData();
}

@Override
public boolean isColor(ItemStack stack, DyeColor color) {
return color.getWoolData() == stack.getDurability();
TTtie marked this conversation as resolved.
Show resolved Hide resolved
}

public void setColor(World world, Iterable<BlockVector> positions, DyeColor color) {
byte blockData = color.getWoolData();
for (BlockVector pos : positions) {
Expand Down
2 changes: 2 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/material/ColorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface ColorUtils {

boolean isColor(BlockState block, DyeColor color);

boolean isColor(ItemStack stack, DyeColor color);
TTtie marked this conversation as resolved.
Show resolved Hide resolved

default void setColor(World world, Iterable<BlockVector> positions, DyeColor color) {
for (BlockVector pos : positions) {
setColor(world.getBlockAt(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()), color);
Expand Down