Browse Source

rewrite lock capability blocking interface to be more powerful

pull/193/head
Adrian Siekierka 1 year ago
parent
commit
0d74c72b6a

+ 19
- 3
src/main/java/pl/asie/charset/module/storage/locks/CharsetStorageLocks.java View File

@@ -27,6 +27,7 @@ import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@@ -35,6 +36,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import pl.asie.charset.ModCharset;
import pl.asie.charset.lib.CharsetLib;
import pl.asie.charset.lib.capability.Capabilities;
@@ -44,6 +46,8 @@ import pl.asie.charset.lib.loader.CharsetModule;
import pl.asie.charset.lib.loader.ModuleProfile;
import pl.asie.charset.lib.ui.GuiHandlerCharset;
import pl.asie.charset.lib.utils.RegistryUtils;
import pl.asie.charset.module.storage.locks.wrapper.ReadOnlyFluidHandler;
import pl.asie.charset.module.storage.locks.wrapper.ReadOnlyItemHandler;
import pl.asie.charset.patchwork.LocksCapabilityHook;
import pl.asie.charset.patchwork.PatchworkHelper;

@@ -85,9 +89,21 @@ public class CharsetStorageLocks {
if (PatchworkHelper.getBoolean("LOCKS_BLOCK_CAPABILITIES")) {
LocksCapabilityHook.handler = new LocksCapabilityHandler();

LocksCapabilityHandler.addCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, true);
LocksCapabilityHandler.addCapability(Capabilities.ITEM_INSERTION_HANDLER, true);
LocksCapabilityHandler.addCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, true);
LocksCapabilityHandler.addCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, (o) -> {
if (o != null) {
return new ReadOnlyItemHandler((IItemHandler) o);
} else {
return null;
}
}, true);
LocksCapabilityHandler.addCapability(Capabilities.ITEM_INSERTION_HANDLER, null, true);
LocksCapabilityHandler.addCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, (o) -> {
if (o != null) {
return new ReadOnlyFluidHandler((IFluidHandler) o);
} else {
return null;
}
}, true);

CharsetStorageLocks.config.getCategory("blockedCapabilities").setComment("NOTE: This functionality requires a special configuration option in CharsetPatches to be turned on! Otherwise, it will not be effective!");


+ 11
- 8
src/main/java/pl/asie/charset/module/storage/locks/LocksCapabilityHandler.java View File

@@ -25,36 +25,39 @@ import net.minecraftforge.common.capabilities.Capability;
import pl.asie.charset.api.locks.Lockable;
import pl.asie.charset.lib.capability.Capabilities;
import pl.asie.charset.lib.config.ConfigUtils;
import pl.asie.charset.patchwork.LocksCapabilityHook;
import pl.asie.charset.patchwork.LocksCapabilityHook.Handler;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

public class LocksCapabilityHandler implements Handler {
private static final Set<Capability> FORBIDDEN = Collections.newSetFromMap(new IdentityHashMap<>());
private static final Map<Capability, Function<Object, Object>> FORBIDDEN = new IdentityHashMap<>();

public static void addCapability(Capability capability, boolean def) {
public static void addCapability(Capability capability, Function<Object, Object> transformer, boolean def) {
if (ConfigUtils.getBoolean(CharsetStorageLocks.config, "blockedCapabilities", capability.getName(), def, null, true)) {
FORBIDDEN.add(capability);
FORBIDDEN.put(capability, transformer);
}
}

@Override
public boolean blocksCapability(TileEntity tile, Capability capability, EnumFacing facing) {
public LocksCapabilityHook.Result wrapCapability(TileEntity tile, Capability capability, EnumFacing facing) {
if (capability == Capabilities.LOCKABLE) {
// Early return to prevent recursion
return false;
return LocksCapabilityHook.NO;
} else {
if (tile.hasCapability(Capabilities.LOCKABLE, facing)) {
Lockable lockable = tile.getCapability(Capabilities.LOCKABLE, facing);
if (lockable != null && lockable.hasLock()) {
return FORBIDDEN.contains(capability);
return new LocksCapabilityHook.Result(true, FORBIDDEN.get(capability));
} else {
return false;
return LocksCapabilityHook.NO;
}
} else {
return false;
return LocksCapabilityHook.NO;
}
}
}

+ 37
- 0
src/main/java/pl/asie/charset/module/storage/locks/wrapper/ReadOnlyFluidHandler.java View File

@@ -0,0 +1,37 @@
package pl.asie.charset.module.storage.locks.wrapper;

import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties;

import javax.annotation.Nullable;

public class ReadOnlyFluidHandler implements IFluidHandler {
private final IFluidHandler parent;

public ReadOnlyFluidHandler(IFluidHandler parent) {
this.parent = parent;
}

@Override
public IFluidTankProperties[] getTankProperties() {
return parent.getTankProperties();
}

@Override
public int fill(FluidStack resource, boolean doFill) {
return 0;
}

@Nullable
@Override
public FluidStack drain(FluidStack resource, boolean doDrain) {
return null;
}

@Nullable
@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
return null;
}
}

+ 42
- 0
src/main/java/pl/asie/charset/module/storage/locks/wrapper/ReadOnlyItemHandler.java View File

@@ -0,0 +1,42 @@
package pl.asie.charset.module.storage.locks.wrapper;

import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;

public class ReadOnlyItemHandler implements IItemHandler {
private final IItemHandler parent;

public ReadOnlyItemHandler(IItemHandler parent) {
this.parent = parent;
}

@Override
public int getSlots() {
return parent.getSlots();
}

@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
return parent.getStackInSlot(slot);
}

@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
return stack;
}

@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
return ItemStack.EMPTY;
}

@Override
public int getSlotLimit(int slot) {
return parent.getSlotLimit(slot);
}
}

+ 33
- 8
src/main/java/pl/asie/charset/patchwork/LocksCapabilityHook.java View File

@@ -19,20 +19,45 @@

package pl.asie.charset.patchwork;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import pl.asie.charset.lib.utils.ThreeState;

import java.util.function.Function;

public class LocksCapabilityHook {
public static final Result NO = new Result(false);

public static final class Result {
private final Function<Object, Object> transformer;
private final boolean result;

public Result(boolean b) {
this.transformer = null;
this.result = b;
}

public Result(boolean b, Function<Object, Object> transformer) {
this.transformer = transformer;
this.result = b;
}

public boolean captures() {
return result;
}

public boolean canApply() {
return transformer != null;
}

public Object apply(Object o) {
return transformer.apply(o);
}
}

public interface Handler {
boolean blocksCapability(TileEntity tile, Capability capability, EnumFacing facing);
Result wrapCapability(TileEntity tile, Capability capability, EnumFacing facing);
}

public static Handler handler = (tile, capability, facing) -> false;
public static Handler handler = (tile, capability, facing) -> NO;
}

Loading…
Cancel
Save