Skip to content

Commit

Permalink
11
Browse files Browse the repository at this point in the history
  • Loading branch information
rotgruengelb committed Feb 9, 2024
1 parent 1ddbff9 commit be5e5e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 0 additions & 11 deletions src/main/java/net/rotgruengelb/landscape/feature/zones/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@
import java.util.Map;
import java.util.Optional;

import static net.rotgruengelb.landscape.Landscape.DEV_ENV;
import static net.rotgruengelb.landscape.util.Util.mapWins;

public class API {

public static Optional<Boolean> posAllowsAction(BlockPos pos, String rule, World world, boolean includeNull) {
long startTime = 0;
if (DEV_ENV) {
startTime = System.nanoTime();
}
Map<Integer, Boolean> values = new HashMap<>();
for (BlockPos managerPos : AvailableZoneManagers.getManagers(world)) {
if (world.getBlockEntity(managerPos) instanceof ZoneManager zoneManager && zoneManager.isBlockPosInZone(pos, false)) {
Expand All @@ -31,12 +26,6 @@ public static Optional<Boolean> posAllowsAction(BlockPos pos, String rule, World
if (values.isEmpty()) {
return includeNull ? Optional.empty() : Optional.of(true);
}

if (DEV_ENV) {
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1_000_000; // Convert to milliseconds
System.out.println("Check for rule: " + rule + " for pos: " + pos.toString() + " \ntook " + duration + "ms");
}
return Optional.of(mapWins(values));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.registry.Registries;
import net.minecraft.util.ActionResult;
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.feature.zones.API;
import org.spongepowered.asm.mixin.Debug;
import net.rotgruengelb.landscape.util.Debug;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Debug(export = true)
import static net.rotgruengelb.landscape.Landscape.LOGGER;

@Mixin(BlockItem.class)
public class BlockItemMixin {

Expand All @@ -22,13 +22,15 @@ public class BlockItemMixin {
cancellable = true
)
private void rule__core_block_place(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> cir) {
Landscape.LOGGER.info("BlockItemMixin.place");
System.out.println("BlockItemMixin.place");
// if (context.getStack().isOf(ZONE_BLOCK_ITEM)) { return; } (HOPEFULLY NOT NEEDED ANYMORE)
long timeStart = Debug.timeStart();
if (context.getPlayer() == null) { return; }
if (!API.posAllowsAction(context.getBlockPos(), "rule.core.player.block.place", context.getWorld(), Registries.ITEM.getId(context.getStack()
.getItem()).toString())) {
cir.setReturnValue(net.minecraft.util.ActionResult.FAIL);
}
if (!context.getWorld().isClient) {
LOGGER.info("Check for rule: at pos: " + context.getBlockPos()
.toString() + " took " + Debug.timeEnd(timeStart) + "ms");
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/net/rotgruengelb/landscape/util/Debug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.rotgruengelb.landscape.util;

public class Debug {
public static long timeStart() {
return System.nanoTime();
}

public static long timeEnd(long startTime) {
long endTime = System.nanoTime();
return (endTime - startTime) / 1_000_000; // Convert to milliseconds
}
}

0 comments on commit be5e5e5

Please sign in to comment.