-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
base: main
Are you sure you want to change the base?
Feature/watertagged #704
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be calculated for us, look at how There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} |
There was a problem hiding this comment.
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 logicThere was a problem hiding this comment.
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