Skip to content

Commit

Permalink
perf: chunk based enclosure lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jul 9, 2024
1 parent d4e513b commit 5665d16
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
Expand All @@ -27,16 +26,16 @@

@Mixin(CampfireBlock.class)
public class MixinCampfireBlock {

@Shadow @Final public static BooleanProperty LIT;
@Shadow
@Final
public static BooleanProperty LIT;

@Inject(at = @At("HEAD"), method = "extinguish", cancellable = true)
private static void onExtinguish(Entity entity, WorldAccess world, BlockPos pos, BlockState state, CallbackInfo ci){
if(world instanceof ServerWorld){
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) world);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.USE_CAMPFIRE)) {
if(entity instanceof ServerPlayerEntity player){
private static void onExtinguish(Entity entity, WorldAccess world, BlockPos pos, BlockState state, CallbackInfo ci) {
if (world instanceof ServerWorld serverWorld) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
if (area != null && !area.hasPubPerm(Permission.USE_CAMPFIRE)) {
if (entity instanceof ServerPlayerEntity player) {
player.sendMessage(USE_CAMPFIRE.getNoPermissionMsg(player));
}

Expand All @@ -49,11 +48,10 @@ private static void onExtinguish(Entity entity, WorldAccess world, BlockPos pos,
}

@Inject(at = @At("HEAD"), method = "tryFillWithFluid", cancellable = true)
private void onFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState, CallbackInfoReturnable<Boolean> cir){
if(world instanceof ServerWorld){
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) world);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.USE_CAMPFIRE)) {
private void onFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState, CallbackInfoReturnable<Boolean> cir) {
if (world instanceof ServerWorld serverWorld) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
if (area != null && !area.hasPubPerm(Permission.USE_CAMPFIRE)) {
cir.setReturnValue(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.entity.EntityType;
Expand All @@ -22,14 +21,12 @@ public MixinEnderDragonEntity(EntityType<? extends EnderDragonEntity> entityType

@Redirect(method = "destroyBlocks", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;removeBlock(Lnet/minecraft/util/math/BlockPos;Z)Z"))
private boolean dragonProtection(World instance, BlockPos pos, boolean move) {
if (instance.isClient) {
return false;
if (instance instanceof ServerWorld serverWorld) {
EnclosureArea a = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
if (a != null && !a.hasPubPerm(Permission.DRAGON_DESTROY)) {
return true;
}
}
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) getWorld());
EnclosureArea a = list.getArea(pos);
if (a != null && !a.areaOf(pos).hasPubPerm(Permission.DRAGON_DESTROY)) {
return true;
}
return this.getWorld().removeBlock(pos, false);
return this.getWorld().removeBlock(pos, move);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
Expand Down Expand Up @@ -31,16 +30,15 @@ public abstract class MixinExplosion {
@ModifyVariable(at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/Vec3d;<init>(DDD)V", ordinal = 1), method = "collectBlocksAndDamageEntities")
private List<Entity> protectEntities(List<Entity> list) {
if (!world.isClient) {
EnclosureList enclosureList = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) world);
list.removeIf(e -> {
assert e != null;
BlockPos pos = e.getBlockPos();
EnclosureArea a = enclosureList.getArea(pos);
return a != null && !a.areaOf(pos).hasPubPerm(Permission.EXPLOSION);
list.removeIf(entity -> {
assert entity != null;
BlockPos pos = entity.getBlockPos();
EnclosureArea a = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) world, pos);
return a != null && !a.hasPubPerm(Permission.EXPLOSION);
});
this.affectedBlocks.removeIf(pos -> {
EnclosureArea a = enclosureList.getArea(pos);
return a != null && !a.areaOf(pos).hasPubPerm(Permission.EXPLOSION);
EnclosureArea a = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) world, pos);
return a != null && !a.hasPubPerm(Permission.EXPLOSION);
});
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.fabricmc.api.Environment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.FallingBlockEntity;
Expand Down Expand Up @@ -37,8 +36,8 @@ private void protectFallingBlocks(CallbackInfo ci) {
if (getWorld().isClient) {
return;
}
EnclosureArea currentArea = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) getWorld()).getArea(getBlockPos());
EnclosureArea sourceArea = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) getWorld()).getArea(getFallingBlockPos());
EnclosureArea currentArea = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) getWorld(), getBlockPos());
EnclosureArea sourceArea = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) getWorld(), getFallingBlockPos());
if (currentArea == null || sourceArea == currentArea) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
Expand All @@ -21,19 +20,18 @@ public class MixinFireBlock {
@Unique
private void set(World instance, BlockPos pos, BlockState blockState, int i) {
if (instance.isClient) return;
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) instance);
EnclosureArea area = list.getArea(pos);
if (area == null || area.areaOf(pos).hasPubPerm(Permission.FIRE_SPREADING)) {
instance.setBlockState(pos, blockState, i);
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) instance, pos);
if (area != null && !area.hasPubPerm(Permission.FIRE_SPREADING)) {
return;
}
instance.setBlockState(pos, blockState, i);
}

@Unique
private void remove(World instance, BlockPos pos, boolean move) {
if (instance.isClient) return;
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) instance);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.FIRE_SPREADING)) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) instance, pos);
if (area != null && !area.hasPubPerm(Permission.FIRE_SPREADING)) {
if (instance.getBlockState(pos).isOf(Blocks.FIRE)) {
// 但是允许火熄灭
instance.removeBlock(pos, move);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public MixinItemEntity(EntityType<?> type, World world) {
@Inject(method = "onPlayerCollision", at = @At("HEAD"), cancellable = true)
private void onPlayerCollision(PlayerEntity player, CallbackInfo ci) {
if (getWorld() instanceof ServerWorld serverWorld) {
EnclosureArea area = ServerMain.INSTANCE.getAllEnclosures(serverWorld).getArea(getBlockPos());
if (area != null && !area.areaOf(getBlockPos()).hasPerm((ServerPlayerEntity) player, Permission.PICKUP_ITEM)) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, getBlockPos());
if (area != null && !area.hasPerm((ServerPlayerEntity) player, Permission.PICKUP_ITEM)) {
((PlayerAccess) player).sendMessageWithCD(Permission.PICKUP_ITEM::getNoPermissionMsg);
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected MixinItemFrameEntity(EntityType<? extends AbstractDecorationEntity> en
private void onUse(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
if (player instanceof ServerPlayerEntity serverPlayer) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) serverPlayer.getWorld(), getBlockPos());
if (area != null && !area.areaOf(getBlockPos()).hasPerm(serverPlayer, Permission.ITEM_FRAME)) {
if (area != null && !area.hasPerm(serverPlayer, Permission.ITEM_FRAME)) {
player.sendMessage(Permission.ITEM_FRAME.getNoPermissionMsg(serverPlayer));
cir.setReturnValue(ActionResult.FAIL);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
Expand All @@ -18,10 +17,9 @@
public class MixinLeveledCauldronBlock {
@Inject(method = "onFireCollision", at = @At("HEAD"), cancellable = true)
private void onFireCollision(BlockState state, World world, BlockPos pos, CallbackInfo ci){
if (!world.isClient) {
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) world);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.CONSUMPTIVELY_EXTINGUISH)) {
if (world instanceof ServerWorld serverWorld) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
if (area != null && !area.hasPubPerm(Permission.CONSUMPTIVELY_EXTINGUISH)) {
ci.cancel();
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.Block;
Expand Down Expand Up @@ -35,9 +34,8 @@ private void onExtinguishFire(BlockPos pos, CallbackInfo ci) {
}
Block block = getWorld().getBlockState(pos).getBlock();
if (block instanceof CampfireBlock) {
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) getWorld());
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.USE_CAMPFIRE)) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) getWorld(), pos);
if (area != null && !area.hasPubPerm(Permission.USE_CAMPFIRE)) {
if (getOwner() instanceof ServerPlayerEntity player) {
player.sendMessage(USE_CAMPFIRE.getNoPermissionMsg(player));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
Expand All @@ -19,13 +18,11 @@
public class MixinPowderSnowBlock {
@Inject(method = "onEntityCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;breakBlock(Lnet/minecraft/util/math/BlockPos;Z)Z"), cancellable = true)
private void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity, CallbackInfo ci) {
if (world.isClient) {
return;
}
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures((ServerWorld) world);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.CONSUMPTIVELY_EXTINGUISH)) {
ci.cancel();
if (world instanceof ServerWorld serverWorld) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
if (area != null && !area.hasPubPerm(Permission.CONSUMPTIVELY_EXTINGUISH)) {
ci.cancel();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
Expand All @@ -18,9 +17,8 @@
public class MixinSculkCatalystBlock {
@Inject(method = "scheduledTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;I)Z"), cancellable = true)
private void setBlock(BlockState state, ServerWorld world, BlockPos pos, Random random, CallbackInfo ci) {
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures(world);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.SCULK_SPREAD)) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(world, pos);
if (area != null && !area.hasPubPerm(Permission.SCULK_SPREAD)) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void spread(ServerWorld world, RegistryEntry<GameEvent> event, GameEvent
}
BlockPos blockPos = BlockPos.ofFloored(emitterPos.offset(Direction.UP, 0.5));
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure((ServerWorld) livingEntity.getWorld(), blockPos);
if (area != null && !area.areaOf(blockPos).hasPubPerm(Permission.SCULK_SPREAD)) {
if (area != null && !area.hasPubPerm(Permission.SCULK_SPREAD)) {
cir.setReturnValue(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.block.SculkSpreadable;
import net.minecraft.block.entity.SculkSpreadManager;
Expand All @@ -28,9 +26,8 @@ private static void getSpreadPos(WorldAccess world, BlockPos sourcePos, BlockPos
if (targetPos == null) {
return;
}
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures(serverWorld);
EnclosureArea area = list.getArea(targetPos);
if (area != null && !area.areaOf(targetPos).hasPubPerm(Permission.SCULK_SPREAD)) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, targetPos);
if (area != null && !area.hasPubPerm(Permission.SCULK_SPREAD)) {
cir.setReturnValue(false);
}
}
Expand All @@ -39,9 +36,8 @@ private static void getSpreadPos(WorldAccess world, BlockPos sourcePos, BlockPos
@Redirect(method = "spread", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/SculkSpreadable;spread(Lnet/minecraft/world/WorldAccess;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Ljava/util/Collection;Z)Z"))
private boolean spread(SculkSpreadable instance, WorldAccess world, BlockPos pos, BlockState state, Collection<Direction> directions, boolean markForPostProcessing) {
if (world instanceof ServerWorld serverWorld) {
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures(serverWorld);
EnclosureArea area = list.getArea(pos);
if (area != null && !area.areaOf(pos).hasPubPerm(Permission.SCULK_SPREAD)) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
if (area != null && !area.hasPubPerm(Permission.SCULK_SPREAD)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.enclosure.mixin;

import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.EnclosureList;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.utils.Permission;
import net.minecraft.block.BlockState;
Expand All @@ -26,9 +25,8 @@ public class MixinSculkVeinBlock {
@Unique
private static boolean disallow(WorldAccess world, BlockPos pos) {
if (world instanceof ServerWorld serverWorld) {
EnclosureList list = ServerMain.INSTANCE.getAllEnclosures(serverWorld);
EnclosureArea area = list.getArea(pos);
return area != null && !area.areaOf(pos).hasPubPerm(Permission.SCULK_SPREAD);
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(serverWorld, pos);
return area != null && !area.hasPubPerm(Permission.SCULK_SPREAD);
}
return false;
}
Expand Down
Loading

0 comments on commit 5665d16

Please sign in to comment.