Skip to content

Commit

Permalink
Merge pull request FTBTeam#247 from BasiqueEvangelist/common-prot-sup…
Browse files Browse the repository at this point in the history
…port

feat: add common protection api support (Fabric)
  • Loading branch information
desht authored Apr 13, 2023
2 parents 9b33d29 + 0cc50e9 commit 29be853
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
3 changes: 3 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ repositories {
includeGroup "com.faux.ingredientextension"
}
}
maven { url "https://maven.nucleoid.xyz/" }
}

dependencies {
Expand All @@ -65,6 +66,8 @@ dependencies {
modCompileOnly("curse.maven:balm-fabric-500525:${balm_fabric_version}") { transitive = false }
modCompileOnly("curse.maven:waystones-fabric-500087:${waystones_fabric_version}") { transitive = false }

modCompileOnly("eu.pb4:common-protection-api:${common_prot_api_version}") { transitive = false }

common(project(path: ":common", configuration: "dev")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionFabric")) { transitive false }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package dev.ftb.mods.ftbchunks.compat.commonprot;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.mojang.authlib.GameProfile;
import dev.ftb.mods.ftbchunks.FTBChunks;
import dev.ftb.mods.ftbchunks.FTBChunksExpected;
import dev.ftb.mods.ftbchunks.FTBChunksWorldConfig;
import dev.ftb.mods.ftbchunks.data.ClaimedChunk;
import dev.ftb.mods.ftbchunks.data.FTBChunksAPI;
import dev.ftb.mods.ftbchunks.data.Protection;
import dev.ftb.mods.ftblibrary.math.ChunkDimPos;
import eu.pb4.common.protection.api.CommonProtection;
import eu.pb4.common.protection.api.ProtectionProvider;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.TimeUnit;

public class FTBChunksProtectionProvider implements ProtectionProvider {
public static ResourceLocation ID = new ResourceLocation(FTBChunks.MOD_ID, "provider");
private static LoadingCache<GameProfile, ServerPlayer> FAKE_PLAYERS = null;

public static void init() {
CommonProtection.register(ID, new FTBChunksProtectionProvider());

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
FAKE_PLAYERS = CacheBuilder.newBuilder()
.initialCapacity(64)
.expireAfterWrite(
30, TimeUnit.SECONDS)
.softValues()
.build(CacheLoader.from((profile) -> new OfflineServerPlayer(server.overworld(), profile)));
});

ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
FAKE_PLAYERS = null;
});
}

@Override
public boolean isProtected(Level world, BlockPos pos) {
return FTBChunksAPI.getManager().getChunk(new ChunkDimPos(world, pos)) != null;
}

@Override
public boolean isAreaProtected(Level world, AABB area) {
int minCX = (int) Math.floor(area.minX / 16);
int minCZ = (int) Math.floor(area.minX / 16);
int maxCX = (int) Math.ceil(area.maxX / 16);
int maxCZ = (int) Math.ceil(area.maxZ / 16);

for (int cx = minCX; cx < maxCX; cx++) {
for (int cz = minCZ; cz < maxCZ; cz++) {
if (FTBChunksAPI.getManager().getChunk(new ChunkDimPos(world.dimension(), cx, cz)) != null) {
return true;
}
}
}

return false;
}

@Override
public boolean canBreakBlock(Level world, BlockPos pos, GameProfile profile, Player player) {
player = tryResolvePlayer(world, profile);

if (player == null) return true;

return !FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, pos, FTBChunksExpected.getBlockBreakProtection(), null);
}

@Override
public boolean canExplodeBlock(Level world, BlockPos pos, Explosion explosion, GameProfile profile, Player player) {
if (explosion.source == null && !FTBChunksWorldConfig.PROTECT_UNKNOWN_EXPLOSIONS.get()) {
return true;
}

ChunkDimPos chunkPos = new ChunkDimPos(world, pos);
ClaimedChunk chunk = FTBChunksAPI.getManager().getChunk(chunkPos);

return chunk == null || chunk.allowExplosions();
}

@Override
public boolean canPlaceBlock(Level world, BlockPos pos, GameProfile profile, Player player) {
player = tryResolvePlayer(world, profile);

if (player == null) return true;

return !FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, pos, FTBChunksExpected.getBlockPlaceProtection(), null);
}

@Override
public boolean canInteractBlock(Level world, BlockPos pos, GameProfile profile, Player player) {
player = tryResolvePlayer(world, profile);

if (player == null) return true;

return !FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, pos, FTBChunksExpected.getBlockInteractProtection(), null);

}

@Override
public boolean canInteractEntity(Level world, Entity entity, GameProfile profile, Player player) {
player = tryResolvePlayer(world, profile);

if (player == null) return true;

return !FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, entity.blockPosition(), Protection.INTERACT_ENTITY, entity);
}

@Override
public boolean canDamageEntity(Level world, Entity entity, GameProfile profile, Player player) {
if (entity instanceof LivingEntity) return true;

player = tryResolvePlayer(world, profile);

if (player == null) return true;

return !FTBChunksAPI.getManager().protect(player, InteractionHand.MAIN_HAND, entity.blockPosition(), Protection.ATTACK_NONLIVING_ENTITY, entity);
}

public static @Nullable ServerPlayer tryResolvePlayer(Level l, GameProfile profile) {
if (!(l instanceof ServerLevel sl)) {
return null;
}

ServerPlayer online = sl.getServer().getPlayerList().getPlayer(profile.getId());

if (online != null) return online;

return FAKE_PLAYERS.getUnchecked(profile);
}

private static class OfflineServerPlayer extends ServerPlayer {
public OfflineServerPlayer(ServerLevel serverLevel, GameProfile gameProfile) {
super(serverLevel.getServer(), serverLevel, gameProfile, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.architectury.platform.Platform;
import dev.ftb.mods.ftbchunks.FTBChunks;
import dev.ftb.mods.ftbchunks.compat.commonprot.FTBChunksProtectionProvider;
import dev.ftb.mods.ftbchunks.compat.waystones.WaystonesCompat;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
Expand All @@ -23,5 +24,9 @@ public void onInitialize() {
if (Platform.isModLoaded("waystones")) {
WaystonesCompat.init();
}

if (Platform.isModLoaded("common-protection-api")) {
FTBChunksProtectionProvider.init();
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ waystones_fabric_version=3901920
balm_forge_version=3914527
balm_fabric_version=3913965
gamestages_version=11.0.2

common_prot_api_version=1.0.0

0 comments on commit 29be853

Please sign in to comment.