Skip to content

Commit

Permalink
Replace RegisterCapabilitiesEvent with @AutoRegisterCapability
Browse files Browse the repository at this point in the history
- Convert ModCapabilities to ModCapabilityContainerListeners, only registering container listeners
- Move ItemTooltipEvent handler for IPigSpawner to common code
- Clean up code
  • Loading branch information
Choonster committed Jun 21, 2024
1 parent 0202837 commit 18905c2
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.AutoRegisterCapability;
import net.minecraftforge.energy.IEnergyStorage;

/**
* Stores an energy value for a single chunk.
*
* @author Choonster
*/
@AutoRegisterCapability
public interface IChunkEnergy extends IEnergyStorage {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.AutoRegisterCapability;

/**
* A capability allowing things to be locked with a {@link LockCode} and only opened by players holding an item with a matching display name.
Expand All @@ -14,6 +15,7 @@
*
* @author Choonster
*/
@AutoRegisterCapability
public interface ILock {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package choonster.testmod3.api.capability.maxhealth;

import net.minecraftforge.common.capabilities.AutoRegisterCapability;

/**
* A capability to provide a max health bonus to an entity.
*
* @author Choonster
*/
@AutoRegisterCapability
public interface IMaxHealth {
/**
* Get the bonus max health.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.level.Level;

import net.minecraftforge.common.capabilities.AutoRegisterCapability;
import org.jetbrains.annotations.Nullable;
import java.util.List;

Expand All @@ -11,6 +12,7 @@
*
* @author Choonster
*/
@AutoRegisterCapability
public interface IPigSpawner {
/**
* Can a pig be spawned at the specified position?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.level.ChunkWatchEvent;
Expand Down Expand Up @@ -48,10 +47,6 @@ public class ChunkEnergyCapability {
*/
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath(TestMod3.MODID, "chunk_energy");

public static void register(final RegisterCapabilitiesEvent event) {
event.register(IChunkEnergy.class);
}

/**
* Get the {@link IChunkEnergy} for the {@link Level} and chunk position.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author Choonster
*/
public final class FluidHandlerCapability {
public static void register() {
public static void registerContainerListener() {
CapabilityContainerListenerManager.registerListenerFactory(FluidTankContainerListener::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.common.util.LazyOptional;

import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -36,10 +32,6 @@ public final class LockCapability {
*/
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath(TestMod3.MODID, "lock");

public static void register(final RegisterCapabilitiesEvent event) {
event.register(ILock.class);
}

/**
* Get the {@link ILock} from a block.
*
Expand All @@ -49,16 +41,16 @@ public static void register(final RegisterCapabilitiesEvent event) {
* @return A lazy optional containing the ILock, or an empty lazy optional if there isn't one
*/
public static LazyOptional<ILock> getLock(final LevelReader world, final BlockPos pos, @Nullable final Direction side) {
final BlockState state = world.getBlockState(pos);
final var state = world.getBlockState(pos);

if (state.getBlock() instanceof EntityBlock) {
final BlockEntity blockEntity = world.getBlockEntity(pos);
final var blockEntity = world.getBlockEntity(pos);
if (blockEntity != null) {
final LazyOptional<ILock> optionalLock = blockEntity.getCapability(LOCK_CAPABILITY, side);
final var optionalLock = blockEntity.getCapability(LOCK_CAPABILITY, side);
if (optionalLock.isPresent()) {
return optionalLock;
} else if (blockEntity instanceof BaseContainerBlockEntity) {
return LazyOptional.of(() -> new BaseContainerBlockEntityWrapper((BaseContainerBlockEntity) blockEntity));
} else if (blockEntity instanceof final BaseContainerBlockEntity baseContainerBlockEntity) {
return LazyOptional.of(() -> new BaseContainerBlockEntityWrapper(baseContainerBlockEntity));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.component.ItemAttributeModifiers;
import net.minecraftforge.common.capabilities.*;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
Expand All @@ -24,7 +27,6 @@
* @author Choonster
*/
public final class MaxHealthCapability {

/**
* The {@link Capability} instance.
*/
Expand All @@ -43,10 +45,6 @@ public final class MaxHealthCapability {

public static final Marker LOG_MARKER = ModLogUtils.getMarker("MaxHealth");

public static void register(final RegisterCapabilitiesEvent event) {
event.register(IMaxHealth.class);
}

/**
* Get the {@link IMaxHealth} from the specified entity.
*
Expand Down Expand Up @@ -92,7 +90,7 @@ private static class EventHandler {
@SubscribeEvent
public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) {
if (event.getObject() instanceof LivingEntity) {
final MaxHealth maxHealth = new MaxHealth((LivingEntity) event.getObject());
final var maxHealth = new MaxHealth((LivingEntity) event.getObject());
event.addCapability(ID, createProvider(maxHealth));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.capabilities.*;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
Expand Down Expand Up @@ -52,11 +54,9 @@ public final class PigSpawnerCapability {
public static final Marker LOG_MARKER = ModLogUtils.getMarker("PIG_SPAWNER");

/**
* Register the capability.
* Register the capability container listener.
*/
public static void register(final RegisterCapabilitiesEvent event) {
event.register(IPigSpawner.class);

public static void registerContainerListener() {
CapabilityContainerListenerManager.registerListenerFactory(FinitePigSpawnerContainerListener::new);
}

Expand Down Expand Up @@ -174,10 +174,6 @@ public static void entityInteract(final PlayerInteractEvent.EntityInteract event
.ifPresent(pigSpawner -> trySpawnPig(pigSpawner, level, x, y, z, interactable, target.blockPosition(), event.getEntity()));
}

}

@Mod.EventBusSubscriber(value = Dist.CLIENT, modid = TestMod3.MODID)
private static class ClientEventHandler {
/**
* Add the {@link IPigSpawner}'s tooltip lines to the tooltip if the item has the {@link IPigSpawner} capability
*
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/choonster/testmod3/init/ModCapabilities.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package choonster.testmod3.init;

import choonster.testmod3.TestMod3;
import choonster.testmod3.capability.fluidhandler.FluidHandlerCapability;
import choonster.testmod3.capability.pigspawner.PigSpawnerCapability;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;

/**
* Registers this mod's capability container listeners.
*
* @author Choonster
*/
@Mod.EventBusSubscriber(modid = TestMod3.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModCapabilityContainerListeners {
/**
* Register the capability container listeners.
*
* @param event The common setup event
*/
@SubscribeEvent
public static void registerContainerListeners(FMLCommonSetupEvent event) {
event.enqueueWork(() -> {
PigSpawnerCapability.registerContainerListener();
FluidHandlerCapability.registerContainerListener();
});
}
}

0 comments on commit 18905c2

Please sign in to comment.