Skip to content

Commit

Permalink
Added Common Protection API support
Browse files Browse the repository at this point in the history
Updated to 1.19.2
  • Loading branch information
PotatoPresident committed Nov 13, 2022
1 parent d1c56df commit 939127c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 13 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
repositories {
maven { url = "https://maven.nucleoid.xyz/" }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url "https://maven.nucleoid.xyz/" }
}

sourceCompatibility = JavaVersion.VERSION_17
Expand All @@ -27,11 +28,11 @@ dependencies {
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.

modImplementation "fr.catcore:server-translations-api:${project.translation_version}"
include "fr.catcore:server-translations-api:${project.translation_version}"
modImplementation include("fr.catcore:server-translations-api:${project.translation_version}")

modImplementation 'me.lucko:fabric-permissions-api:0.1-SNAPSHOT'
include 'me.lucko:fabric-permissions-api:0.1-SNAPSHOT'
modImplementation include("me.lucko:fabric-permissions-api:0.1-SNAPSHOT")

modImplementation include("eu.pb4:common-protection-api:1.0.0")
}

processResources {
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.19
yarn_mappings=1.19+build.1
loader_version=0.14.7
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.10
# Mod Properties
mod_version=1.1.5
maven_group=us.potatoboy
archives_base_name=htm
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.55.3+1.19
translation_version=1.4.14+1.19-rc2
fabric_version=0.66.0+1.19.2
translation_version=1.4.18+1.19.2
4 changes: 4 additions & 0 deletions src/main/java/com/github/fabricservertools/htm/HTM.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.github.fabricservertools.htm.command.HTMCommand;
import com.github.fabricservertools.htm.command.subcommands.*;
import com.github.fabricservertools.htm.config.HTMConfig;
import com.github.fabricservertools.htm.interactions.InteractionManager;
import com.github.fabricservertools.htm.listeners.PlayerEventListener;
import com.github.fabricservertools.htm.listeners.WorldEventListener;
import com.mojang.brigadier.CommandDispatcher;
import eu.pb4.common.protection.api.CommonProtection;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -28,6 +31,7 @@ public void onInitialize() {
config = HTMConfig.loadConfig(new File(FabricLoader.getInstance().getConfigDir() + "/htm_config.json"));

CommandRegistrationCallback.EVENT.register(((dispatcher, environment, registryAccess) -> registerCommands(dispatcher)));
CommonProtection.register(Identifier.of("htm", "containers"), new InteractionManager());

PlayerEventListener.init();
WorldEventListener.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public boolean removeTrust(UUID id) {
return trusted.remove(id);
}

public boolean isTrusted(UUID id) {
return trusted.contains(id);
}

public void transfer(UUID id) {
owner = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
import com.github.fabricservertools.htm.api.LockInteraction;
import com.github.fabricservertools.htm.api.LockableChestBlock;
import com.github.fabricservertools.htm.api.LockableObject;
import com.mojang.authlib.GameProfile;
import eu.pb4.common.protection.api.ProtectionProvider;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public class InteractionManager {
public class InteractionManager implements ProtectionProvider {
public static final Object2ObjectMap<ServerPlayerEntity, LockInteraction> pendingActions = new Object2ObjectOpenHashMap<>();
public static final ObjectSet<UUID> persisting = new ObjectOpenHashSet<>();
public static final ObjectSet<UUID> noMessage = new ObjectOpenHashSet<>();
Expand Down Expand Up @@ -80,4 +85,25 @@ public static void toggleNoMessage(ServerPlayerEntity player) {
noMessage.add(player.getUuid());
}
}

@Override
public boolean isProtected(World world, BlockPos pos) {
var lock = InteractionManager.getLock((ServerWorld) world, pos);
return lock != null && lock.isLocked();
}

@Override
public boolean canBreakBlock(World world, BlockPos pos, GameProfile profile, @Nullable PlayerEntity player) {
var lock = InteractionManager.getLock((ServerWorld) world, pos);
if (lock != null && lock.isLocked()) {
return lock.getOwner().equals(profile.getId());
}

return true;
}

@Override
public boolean isAreaProtected(World world, Box area) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static boolean onBeforeBreak(World world, PlayerEntity player, BlockPos

if (!lock.isLocked()) return true;

if (lock.isOwner((ServerPlayerEntity) player) || (HTM.config.canTrustedPlayersBreakChests && lock.getTrusted().contains(player.getUuid()))) {
if (lock.isOwner((ServerPlayerEntity) player) || (HTM.config.canTrustedPlayersBreakChests && lock.isTrusted(player.getUuid()))) {
if (state.getBlock() instanceof LockableChestBlock) {
Optional<BlockEntity> unlocked = ((LockableChestBlock) state.getBlock()).getUnlockedPart(state, world, pos);
if (unlocked.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class KeyLock implements Lock {

@Override
public boolean canOpen(ServerPlayerEntity player, HTMContainerLock lock) {
if (lock.getTrusted().contains(player.getUuid())) return true;
if (lock.isTrusted(player.getUuid())) return true;
if (Utility.getGlobalTrustState(player.server).isTrusted(lock.getOwner(), player.getUuid()))
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class PrivateLock implements Lock {
@Override
public boolean canOpen(ServerPlayerEntity player, HTMContainerLock lock) {
if (lock.getTrusted().contains(player.getUuid())) return true;
if (lock.isTrusted(player.getUuid())) return true;
return Utility.getGlobalTrustState(player.server).isTrusted(lock.getOwner(), player.getUuid());
}

Expand Down

0 comments on commit 939127c

Please sign in to comment.