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

Feature/watertagged #704

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.countercraft.movecraft.processing.tasks.detection.validators.PilotSignValidator;
import net.countercraft.movecraft.processing.tasks.detection.validators.SizeValidator;
import net.countercraft.movecraft.processing.tasks.detection.validators.WaterContactValidator;
import net.countercraft.movecraft.processing.tasks.detection.validators.LiquidBlockValidator;
import net.countercraft.movecraft.util.AtomicLocationSet;
import net.countercraft.movecraft.util.CollectionUtils;
import net.countercraft.movecraft.util.Tags;
Expand Down Expand Up @@ -95,7 +96,8 @@ public class DetectionTask implements Supplier<Effect> {
private static final List<DetectionPredicate<Map<Material, Deque<MovecraftLocation>>>> COMPLETION_VALIDATORS = List.of(
new SizeValidator(),
new FlyBlockValidator(),
new DetectionBlockValidator()
new DetectionBlockValidator(),
new LiquidBlockValidator()
);
private static final List<DetectionPredicate<Map<Material, Deque<MovecraftLocation>>>> VISITED_VALIDATORS = List.of(
new WaterContactValidator()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package net.countercraft.movecraft.processing.tasks.detection.validators;

import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.processing.functions.DetectionPredicate;
import net.countercraft.movecraft.processing.functions.Result;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Deque;
import java.util.Map;

public class LiquidBlockValidator implements DetectionPredicate<Map<Material, Deque<MovecraftLocation>>> {
@Override
public @NotNull Result validate(@NotNull Map<Material, Deque<MovecraftLocation>> materialDequeMap, @NotNull CraftType type, @NotNull MovecraftWorld world, @Nullable Player player) {
String allowedAmountString = type.getStringProperty(CraftType.LIQUIDS_MAX_AMOUNT);
boolean blockNumber = !allowedAmountString.startsWith("N"); //if false use block percentage
final String errorMessage = "Too many waterlogged blocks on craft";

int maxAmount;
try {
if (blockNumber) {
maxAmount = Integer.parseInt(allowedAmountString.substring(1));
} else {
maxAmount = Integer.parseInt(allowedAmountString);
}
} catch (NumberFormatException e) {
return Result.failWithMessage("liquidsMaxAmount wasn't configurated properly");
}

final int liquidBlocks = getLiquidAmount(materialDequeMap, world);
if (liquidBlocks == 0)
return Result.succeed();

if (maxAmount == 0)
return Result.failWithMessage(errorMessage);

if(blockNumber) {
if (liquidBlocks <= maxAmount) {
return Result.succeed();
} else {
return Result.failWithMessage(errorMessage);
}
} else {
int allBlocks = getTotalBlocks(materialDequeMap);
double percentage = ((double) liquidBlocks / allBlocks) * 100;

if (percentage <= maxAmount) {
return Result.succeed();
} else {
return Result.failWithMessage(errorMessage);
}
}
}
Comment on lines +22 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should utilize RequiredBlockEntry's logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so, there is only a max amount no min amount in this case


public int getLiquidAmount(Map<Material, Deque<MovecraftLocation>> materialDequeMap, MovecraftWorld world) {
int amount = 0;
for (var locationList : materialDequeMap.entrySet()) {
final Deque<MovecraftLocation> locations = locationList.getValue();

if (locationList.getKey() == Material.WATER || locationList.getKey() == Material.BUBBLE_COLUMN) {
amount += locations.size();
continue;
}

for (var location : locations) {
BlockData blockData = world.getData(location);

if (blockData instanceof Waterlogged waterlogged && waterlogged.isWaterlogged()) {
amount++;
}
}
}

return amount;
}

public int getTotalBlocks(Map<Material, Deque<MovecraftLocation>> materialDequeMap) {
int amount = 0;
for (var locationList : materialDequeMap.values()) {
amount += locationList.size();
}

return amount;
}
Comment on lines +83 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be calculated for us, look at how FlyBlockValidator does this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does the same exact thing, just using a functional one liner approach:

int total = materialDequeMap.values().parallelStream().mapToInt(Deque::size).sum();

Prefer this?

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ final public class CraftType {
private static final NamespacedKey CAN_FLY = buildKey("can_fly");
// Private key used to calculate BLOCKED_BY_WATER
public static final NamespacedKey REQUIRE_WATER_CONTACT = buildKey("require_water_contact");
public static final NamespacedKey LIQUIDS_MAX_AMOUNT = buildKey("liquids_max_amount");
public static final NamespacedKey TRY_NUDGE = buildKey("try_nudge");
public static final NamespacedKey MOVE_BLOCKS = buildKey("move_blocks");
public static final NamespacedKey CAN_CRUISE = buildKey("can_cruise");
Expand Down Expand Up @@ -412,6 +413,7 @@ public static void registerTypeValidator(Predicate<CraftType> validator, String
registerProperty(new BooleanProperty("blockedByWater", BLOCKED_BY_WATER, type -> true));
registerProperty(new BooleanProperty("canFly", CAN_FLY, type -> type.getBoolProperty(BLOCKED_BY_WATER)));
registerProperty(new BooleanProperty("requireWaterContact", REQUIRE_WATER_CONTACT, type -> false));
registerProperty(new StringProperty("liquidsMaxAmount", LIQUIDS_MAX_AMOUNT, type -> "0"));
registerProperty(new BooleanProperty("tryNudge", TRY_NUDGE, type -> false));
registerProperty(new RequiredBlockProperty("moveblocks", MOVE_BLOCKS, type -> new HashSet<>()));
registerProperty(new BooleanProperty("canCruise", CAN_CRUISE, type -> false));
Expand Down