flan/src/main/java/com/flemmli97/flan/event/WorldEvents.java

75 lines
3.2 KiB
Java
Raw Normal View History

2020-08-23 12:52:36 +00:00
package com.flemmli97.flan.event;
import com.flemmli97.flan.claim.Claim;
import com.flemmli97.flan.claim.ClaimStorage;
import com.flemmli97.flan.claim.EnumPermission;
import net.minecraft.block.BlockState;
import net.minecraft.block.piston.PistonBehavior;
2020-08-26 18:15:46 +00:00
import net.minecraft.server.network.ServerPlayerEntity;
2020-08-23 12:52:36 +00:00
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
2020-08-23 12:52:36 +00:00
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;
2020-08-23 12:52:36 +00:00
public class WorldEvents {
public static void modifyExplosion(Explosion explosion, ServerWorld world) {
ClaimStorage storage = ClaimStorage.get(world);
explosion.getAffectedBlocks().removeIf(pos -> {
2020-08-23 12:52:36 +00:00
Claim claim = storage.getClaimAt(pos);
if (claim != null)
return !claim.canInteract(null, EnumPermission.EXPLOSIONS, pos);
return false;
});
}
public static boolean pistonCanPush(BlockState state, World world, BlockPos blockPos, Direction direction, Direction pistonDir) {
if (world.isClient || direction == Direction.UP || direction == Direction.DOWN)
return true;
boolean empty = state.isAir() || state.getPistonBehavior() == PistonBehavior.DESTROY;
BlockPos dirPos = blockPos.offset(direction);
ClaimStorage storage = ClaimStorage.get((ServerWorld) world);
Claim from = storage.getClaimAt(blockPos);
Claim to = storage.getClaimAt(dirPos);
boolean flag = true;
if (!empty) {
if ((from != null && !from.equals(to)) || (from == null && to != null))
flag = false;
}
if (from != null && from.equals(to)) {
Claim opp = storage.getClaimAt(blockPos.offset(direction.getOpposite()));
flag = from.equals(opp);
}
if (!flag) {
world.updateListeners(blockPos, state, state, 20);
BlockState toState = world.getBlockState(dirPos);
world.updateListeners(dirPos, toState, toState, 20);
}
return flag;
}
public static boolean canFlow(BlockState fluidBlockState, BlockView world, BlockPos blockPos, Direction direction) {
if (!(world instanceof ServerWorld) || direction == Direction.UP || direction == Direction.DOWN)
return true;
ClaimStorage storage = ClaimStorage.get((ServerWorld) world);
Claim from = storage.getClaimAt(blockPos);
Claim to = storage.getClaimAt(blockPos.offset(direction));
boolean fl = from == null && to == null;
if (from != null)
fl = from.equals(to);
return fl;
}
2020-08-26 18:15:46 +00:00
2020-09-02 13:36:58 +00:00
public static boolean canStartRaid(ServerPlayerEntity player) {
2020-08-26 18:15:46 +00:00
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos());
2020-09-03 16:00:37 +00:00
return claim == null || claim.canInteract(player, EnumPermission.RAID, player.getBlockPos());
2020-08-26 18:15:46 +00:00
}
2020-09-02 13:36:58 +00:00
public static boolean canFireSpread(ServerWorld world, BlockPos pos) {
Claim claim = ClaimStorage.get(world).getClaimAt(pos);
2020-09-03 16:00:37 +00:00
return claim == null || claim.canInteract(null, EnumPermission.FIRESPREAD, pos);
}
2020-08-23 12:52:36 +00:00
}