Skip to content
Permalink
Browse files

Update capabilities

  • Loading branch information...
Choonster committed Mar 1, 2019
1 parent ba601df commit b191f331519093a5d6986f01508812fb95cd59bb
@@ -5,10 +5,10 @@
import net.minecraft.init.SoundEvents;
import net.minecraft.network.play.server.SPacketChat;
import net.minecraft.network.play.server.SPacketSoundEffect;
import net.minecraft.util.INameable;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.text.ChatType;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.LockCode;

/**
@@ -18,7 +18,7 @@
*
* @author Choonster
*/
public interface ILock extends IWorldNameable {
public interface ILock extends INameable {

/**
* @return Is this locked?
@@ -7,8 +7,8 @@
import net.minecraft.inventory.IContainerListener;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;

import java.util.HashSet;
@@ -58,8 +58,8 @@ private static void addListeners(final EntityPlayerMP player, final Container co
*/
@SubscribeEvent
public static void playerLoggedIn(final PlayerLoggedInEvent event) {
if (event.player instanceof EntityPlayerMP) {
final EntityPlayerMP player = (EntityPlayerMP) event.player;
if (event.getPlayer() instanceof EntityPlayerMP) {
final EntityPlayerMP player = (EntityPlayerMP) event.getPlayer();
addListeners(player, player.inventoryContainer);
}
}
@@ -1,6 +1,6 @@
package choonster.testmod3.capability;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.INBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
@@ -16,7 +16,7 @@
*
* @author Choonster
*/
public class CapabilityProviderSerializable<HANDLER> extends CapabilityProviderSimple<HANDLER> implements INBTSerializable<NBTBase> {
public class CapabilityProviderSerializable<HANDLER> extends CapabilityProviderSimple<HANDLER> implements INBTSerializable<INBTBase> {

/**
* Create a provider for the default handler instance.
@@ -41,13 +41,25 @@ public CapabilityProviderSerializable(final Capability<HANDLER> capability, @Nul

@Nullable
@Override
public NBTBase serializeNBT() {
return getCapability().writeNBT(getInstance(), getFacing());
public INBTBase serializeNBT() {
final HANDLER instance = getInstance();

if (instance == null) {
return null;
}

return getCapability().writeNBT(instance, getFacing());
}

@Override
public void deserializeNBT(final NBTBase nbt) {
getCapability().readNBT(getInstance(), getFacing(), nbt);
public void deserializeNBT(final INBTBase nbt) {
final HANDLER instance = getInstance();

if (instance == null) {
return;
}

getCapability().readNBT(instance, getFacing(), nbt);
}

}
@@ -3,6 +3,7 @@
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nullable;

@@ -28,35 +29,22 @@
*/
protected final HANDLER instance;

/**
* A lazy optional containing handler instance to provide.
*/
protected final LazyOptional<HANDLER> lazyOptional;

public CapabilityProviderSimple(final Capability<HANDLER> capability, @Nullable final EnumFacing facing, @Nullable final HANDLER instance) {
this.capability = capability;
this.facing = facing;
this.instance = instance;
}

@Deprecated
public CapabilityProviderSimple(@Nullable final HANDLER instance, final Capability<HANDLER> capability, @Nullable final EnumFacing facing) {
this(capability, facing, instance);
}
this.instance = instance;

/**
* Determines if this object has support for the capability in question on the specific side.
* The return value of this MIGHT change during runtime if this object gains or looses support
* for a capability.
* <p>
* Example:
* A Pipe getting a cover placed on one side causing it loose the Inventory attachment function for that side.
* <p>
* This is a light weight version of getCapability, intended for metadata uses.
*
* @param capability The capability to check
* @param facing The Side to check from:
* CAN BE NULL. Null is defined to represent 'internal' or 'self'
* @return True if this object supports the capability.
*/
@Override
public boolean hasCapability(final Capability<?> capability, @Nullable final EnumFacing facing) {
return capability == getCapability();
if (this.instance != null) {
lazyOptional = LazyOptional.of(() -> this.instance);
} else {
lazyOptional = LazyOptional.empty();
}
}

/**
@@ -67,16 +55,11 @@ public boolean hasCapability(final Capability<?> capability, @Nullable final Enu
* @param capability The capability to check
* @param facing The Side to check from:
* CAN BE NULL. Null is defined to represent 'internal' or 'self'
* @return The handler if this object supports the capability.
* @return A lazy optional containing the handler, if this object supports the capability.
*/
@Override
@Nullable
public <T> T getCapability(final Capability<T> capability, @Nullable final EnumFacing facing) {
if (capability == getCapability()) {
return getCapability().cast(getInstance());
}

return null;
public <T> LazyOptional<T> getCapability(final Capability<T> capability, @Nullable final EnumFacing facing) {
return getCapability().orEmpty(capability, lazyOptional);
}

/**
@@ -101,7 +84,7 @@ public EnumFacing getFacing() {
/**
* Get the handler instance.
*
* @return The handler instance
* @return A lazy optional containing the handler instance
*/
@Nullable
public final HANDLER getInstance() {
@@ -6,22 +6,22 @@
import choonster.testmod3.network.MessageUpdateChunkEnergyValue;
import choonster.testmod3.util.CapabilityUtils;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.INBTBase;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.IChunk;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.world.ChunkWatchEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import javax.annotation.Nullable;

import static choonster.testmod3.util.InjectionUtil.Null;

@@ -60,12 +60,12 @@
public static void register() {
CapabilityManager.INSTANCE.register(IChunkEnergy.class, new Capability.IStorage<IChunkEnergy>() {
@Override
public NBTBase writeNBT(final Capability<IChunkEnergy> capability, final IChunkEnergy instance, final EnumFacing side) {
public INBTBase writeNBT(final Capability<IChunkEnergy> capability, final IChunkEnergy instance, final EnumFacing side) {
return new NBTTagInt(instance.getEnergyStored());
}

@Override
public void readNBT(final Capability<IChunkEnergy> capability, final IChunkEnergy instance, final EnumFacing side, final NBTBase nbt) {
public void readNBT(final Capability<IChunkEnergy> capability, final IChunkEnergy instance, final EnumFacing side, final INBTBase nbt) {
if (!(instance instanceof ChunkEnergy))
throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");

@@ -79,21 +79,19 @@ public void readNBT(final Capability<IChunkEnergy> capability, final IChunkEnerg
*
* @param world The World
* @param chunkPos The chunk position
* @return The IChunkEnergy, if any
* @return A lazy optional containing the IChunkEnergy, if any
*/
@Nullable
public static IChunkEnergy getChunkEnergy(final World world, final ChunkPos chunkPos) {
public static LazyOptional<IChunkEnergy> getChunkEnergy(final World world, final ChunkPos chunkPos) {
return getChunkEnergy(world.getChunk(chunkPos.x, chunkPos.z));
}

/**
* Get the {@link IChunkEnergy} for the chunk.
*
* @param chunk The chunk
* @return The IChunkEnergy, if any
* @return A lazy optional containing the IChunkEnergy, if any
*/
@Nullable
public static IChunkEnergy getChunkEnergy(final Chunk chunk) {
public static LazyOptional<IChunkEnergy> getChunkEnergy(final Chunk chunk) {
return CapabilityUtils.getCapability(chunk, CHUNK_ENERGY_CHUNK_CAPABILITY, DEFAULT_FACING);
}

@@ -115,13 +113,11 @@ public static void attachChunkCapabilities(final AttachCapabilitiesEvent<Chunk>
@SubscribeEvent
public static void chunkWatch(final ChunkWatchEvent.Watch event) {
final EntityPlayerMP player = event.getPlayer();
final Chunk chunk = event.getChunkInstance();
if (chunk == null) return;

final IChunkEnergy chunkEnergy = getChunkEnergy(chunk);
if (chunkEnergy == null) return;
final IChunk chunk = event.getChunk();
if (!(chunk instanceof Chunk)) return;

TestMod3.network.sendTo(new MessageUpdateChunkEnergyValue(chunkEnergy), player);
getChunkEnergy((Chunk) chunk)
.ifPresent((chunkEnergy) -> TestMod3.network.sendTo(new MessageUpdateChunkEnergyValue(chunkEnergy), player));
}
}
}
@@ -108,6 +108,6 @@ protected void onEnergyChanged() {
if (playerChunkMapEntry == null) return;

final IMessage message = new MessageUpdateChunkEnergyValue(this);
TestMod3.network.sendToAllTracking(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), chunkOrigin.getX(), chunkOrigin.getY(), chunkOrigin.getZ(), 0));
TestMod3.network.sendToAllTracking(message, new NetworkRegistry.TargetPoint(world.dimension.getDimension(), chunkOrigin.getX(), chunkOrigin.getY(), chunkOrigin.getZ(), 0));
}
}
@@ -7,16 +7,14 @@
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.INBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;

import javax.annotation.Nullable;
import java.util.Optional;
import net.minecraftforge.common.util.LazyOptional;

import static choonster.testmod3.util.InjectionUtil.Null;

@@ -45,14 +43,14 @@
public static void register() {
CapabilityManager.INSTANCE.register(IHiddenBlockRevealer.class, new Capability.IStorage<IHiddenBlockRevealer>() {
@Override
public NBTBase writeNBT(final Capability<IHiddenBlockRevealer> capability, final IHiddenBlockRevealer instance, final EnumFacing side) {
public INBTBase writeNBT(final Capability<IHiddenBlockRevealer> capability, final IHiddenBlockRevealer instance, final EnumFacing side) {
final NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setBoolean("RevealHiddenBlocks", instance.revealHiddenBlocks());
tagCompound.putBoolean("RevealHiddenBlocks", instance.revealHiddenBlocks());
return tagCompound;
}

@Override
public void readNBT(final Capability<IHiddenBlockRevealer> capability, final IHiddenBlockRevealer instance, final EnumFacing side, final NBTBase nbt) {
public void readNBT(final Capability<IHiddenBlockRevealer> capability, final IHiddenBlockRevealer instance, final EnumFacing side, final INBTBase nbt) {
final NBTTagCompound tagCompound = (NBTTagCompound) nbt;
instance.setRevealHiddenBlocks(tagCompound.getBoolean("RevealHiddenBlocks"));
}
@@ -66,10 +64,9 @@ public void readNBT(final Capability<IHiddenBlockRevealer> capability, final IHi
* Get the {@link IHiddenBlockRevealer} for the {@link ItemStack}, if any.
*
* @param stack The ItemStack
* @return The IHiddenBlockRevealer for the {@link ItemStack}, if any
* @return A lazy optional containing the IHiddenBlockRevealer for the {@link ItemStack}, if any
*/
@Nullable
public static IHiddenBlockRevealer getHiddenBlockRevealer(final ItemStack stack) {
public static LazyOptional<IHiddenBlockRevealer> getHiddenBlockRevealer(final ItemStack stack) {
return CapabilityUtils.getCapability(stack, HIDDEN_BLOCK_REVEALER_CAPABILITY, DEFAULT_FACING);
}

@@ -79,16 +76,14 @@ public static IHiddenBlockRevealer getHiddenBlockRevealer(final ItemStack stack)
* @param stack The ItemStack
* @return The new reveal state, or empty if there is no IHiddenBlockRevealer
*/
public static Optional<Boolean> toggleRevealHiddenBlocks(final ItemStack stack) {
final IHiddenBlockRevealer hiddenBlockRevealer = getHiddenBlockRevealer(stack);
if (hiddenBlockRevealer != null) {
final boolean revealHiddenBlocks = !hiddenBlockRevealer.revealHiddenBlocks();
hiddenBlockRevealer.setRevealHiddenBlocks(revealHiddenBlocks);

return Optional.of(revealHiddenBlocks);
}

return Optional.empty();
public static LazyOptional<Boolean> toggleRevealHiddenBlocks(final ItemStack stack) {
return getHiddenBlockRevealer(stack)
.map(hiddenBlockRevealer -> {
final boolean revealHiddenBlocks = !hiddenBlockRevealer.revealHiddenBlocks();
hiddenBlockRevealer.setRevealHiddenBlocks(revealHiddenBlocks);

return revealHiddenBlocks;
});
}

/**
@@ -103,10 +98,10 @@ public static IHiddenBlockRevealer getHiddenBlockRevealer(final ItemStack stack)
/**
* The getter.
*/
private static final IItemPropertyGetter GETTER = (stack, worldIn, entityIn) -> {
final IHiddenBlockRevealer hiddenBlockRevealer = CapabilityHiddenBlockRevealer.getHiddenBlockRevealer(stack);
return hiddenBlockRevealer != null && hiddenBlockRevealer.revealHiddenBlocks() ? 1 : 0;
};
private static final IItemPropertyGetter GETTER = (stack, worldIn, entityIn) ->
CapabilityHiddenBlockRevealer.getHiddenBlockRevealer(stack)
.map(hiddenBlockRevealer -> hiddenBlockRevealer.revealHiddenBlocks() ? 1 : 0)
.orElse(0);

/**
* Add this getter to an {@link Item}.
@@ -7,10 +7,10 @@
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;

/**
* Manages the hidden block state for the client.
@@ -19,7 +19,7 @@
*
* @author Choonster
*/
@Mod.EventBusSubscriber(value = Side.CLIENT, modid = TestMod3.MODID)
@Mod.EventBusSubscriber(value = Dist.CLIENT, modid = TestMod3.MODID)
public class HiddenBlockManager {
private static volatile boolean lastCheckResult = false;
private static boolean toggled = false;
@@ -32,8 +32,11 @@
*/
private static boolean shouldHeldItemRevealHiddenBlocks(final EntityPlayer player) {
for (final EnumHand hand : EnumHand.values()) {
final IHiddenBlockRevealer hiddenBlockRevealer = CapabilityHiddenBlockRevealer.getHiddenBlockRevealer(player.getHeldItem(hand));
if (hiddenBlockRevealer != null && hiddenBlockRevealer.revealHiddenBlocks()) {
final boolean revealHiddenBlocks = CapabilityHiddenBlockRevealer.getHiddenBlockRevealer(player.getHeldItem(hand))
.map(IHiddenBlockRevealer::revealHiddenBlocks)
.orElse(false);

if (revealHiddenBlocks) {
return true;
}
}

0 comments on commit b191f33

Please sign in to comment.
You can’t perform that action at this time.