forked from FTBTeam/FTB-Chunks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request FTBTeam#247 from BasiqueEvangelist/common-prot-sup…
…port feat: add common protection api support (Fabric)
- Loading branch information
Showing
4 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...c/src/main/java/dev/ftb/mods/ftbchunks/compat/commonprot/FTBChunksProtectionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters