some refactoring

This commit is contained in:
Ell 2021-12-23 13:27:52 +01:00
parent 6a9f3675af
commit 8adf52eb93
16 changed files with 37 additions and 50 deletions

View file

@ -207,7 +207,7 @@ public final class Helper {
@Nullable
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
if (capability == NaturesAuraAPI.capAuraRecharge)
if (capability == NaturesAuraAPI.CAP_AURA_RECHARGE)
return this.recharge.cast();
return LazyOptional.empty();
}

View file

@ -2,7 +2,6 @@ package de.ellpeck.naturesaura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.api.multiblock.IMultiblock;
import de.ellpeck.naturesaura.blocks.multi.Multiblock;
@ -11,7 +10,6 @@ import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
@ -39,9 +37,9 @@ public class InternalHooks implements NaturesAuraAPI.IInternalHooks {
private boolean auraPlayerInteraction(Player player, int amount, boolean extract, boolean simulate) {
if (extract && player.isCreative())
return true;
var stack = Helper.getEquippedItem(s -> s.getCapability(NaturesAuraAPI.capAuraContainer).isPresent(), player);
var stack = Helper.getEquippedItem(s -> s.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).isPresent(), player);
if (!stack.isEmpty()) {
var container = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
var container = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).orElse(null);
if (extract) {
return container.drainAura(amount, simulate) > 0;
} else {

View file

@ -43,8 +43,7 @@ import java.util.function.Supplier;
public final class NaturesAuraAPI {
public static final String MOD_ID = "naturesaura";
public static final String API_ID = MOD_ID + "api";
public static final String VERSION = "9";
/**
* A map of all the block states that the Botanist's Pickaxe can convert
* into their mossy variations. Contains mossy brick and mossy cobblestone
@ -107,14 +106,14 @@ public final class NaturesAuraAPI {
* The capability for any item or block that stores Aura in the form of an
* {@link IAuraContainer}
*/
public static Capability<IAuraContainer> capAuraContainer = CapabilityManager.get(new CapabilityToken<>() {
public static final Capability<IAuraContainer> CAP_AURA_CONTAINER = CapabilityManager.get(new CapabilityToken<>() {
});
/**
* The capability for any item that can be recharged from an Aura storage
* container like the Aura Cache in the form of {@link IAuraRecharge} by a
* player holding it in their hand
*/
public static Capability<IAuraRecharge> capAuraRecharge = CapabilityManager.get(new CapabilityToken<>() {
public static final Capability<IAuraRecharge> CAP_AURA_RECHARGE = CapabilityManager.get(new CapabilityToken<>() {
});
/**
* The capability that any chunk in a level has to store Aura in it. As this
@ -124,7 +123,7 @@ public final class NaturesAuraAPI {
* helper method {@link IAuraChunk#getAuraChunk(net.minecraft.world.level.Level,
* BlockPos)}.
*/
public static Capability<IAuraChunk> capAuraChunk = CapabilityManager.get(new CapabilityToken<>() {
public static final Capability<IAuraChunk> CAP_AURA_CHUNK = CapabilityManager.get(new CapabilityToken<>() {
});
/**
* The capability that any level has to store Nature's Aura specific data in
@ -132,14 +131,14 @@ public final class NaturesAuraAPI {
* {@link ILevelData#getLevelData(net.minecraft.world.level.Level)} or
* {@link ILevelData#getOverworldData(net.minecraft.world.level.Level)}.
*/
public static Capability<ILevelData> capLevelData = CapabilityManager.get(new CapabilityToken<>() {
public static final Capability<ILevelData> CAP_LEVEL_DATA = CapabilityManager.get(new CapabilityToken<>() {
});
private static final IInternalHooks INSTANCE;
static {
try {
INSTANCE = (IInternalHooks) Class.forName("de.ellpeck.naturesaura.InternalHooks").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
INSTANCE = (IInternalHooks) Class.forName("de.ellpeck.naturesaura.InternalHooks").getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

View file

@ -36,7 +36,7 @@ public interface IAuraChunk extends INBTSerializable<CompoundTag> {
*/
static IAuraChunk getAuraChunk(Level level, BlockPos pos) {
var chunk = (LevelChunk) level.getChunk(pos);
return chunk.getCapability(NaturesAuraAPI.capAuraChunk, null).orElse(null);
return chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK, null).orElse(null);
}
/**

View file

@ -10,7 +10,7 @@ import net.minecraftforge.items.IItemHandlerModifiable;
public interface ILevelData extends ICapabilityProvider, INBTSerializable<CompoundTag> {
static ILevelData getLevelData(Level level) {
return level.getCapability(NaturesAuraAPI.capLevelData, null).orElse(null);
return level.getCapability(NaturesAuraAPI.CAP_LEVEL_DATA, null).orElse(null);
}
static ILevelData getOverworldData(Level level) {

View file

@ -114,7 +114,7 @@ public class BlockEntityImpl extends BlockEntity {
this.itemHandler = handler == null ? LazyOptional.empty() : LazyOptional.of(() -> handler);
}
return this.itemHandler.cast();
} else if (capability == NaturesAuraAPI.capAuraContainer) {
} else if (capability == NaturesAuraAPI.CAP_AURA_CONTAINER) {
if (this.auraContainer == null) {
var container = this.getAuraContainer();
this.auraContainer = container == null ? LazyOptional.empty() : LazyOptional.of(() -> container);

View file

@ -42,12 +42,12 @@ public class BlockEntityNatureAltar extends BlockEntityImpl implements ITickable
@Override
protected boolean canInsert(ItemStack stack, int slot) {
return BlockEntityNatureAltar.this.getRecipeForInput(stack) != null || stack.getCapability(NaturesAuraAPI.capAuraContainer, null).isPresent();
return BlockEntityNatureAltar.this.getRecipeForInput(stack) != null || stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).isPresent();
}
@Override
protected boolean canExtract(ItemStack stack, int slot, int amount) {
var cap = stack.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
var cap = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).orElse(null);
if (cap != null) {
return cap.storeAura(1, true) <= 0;
} else {
@ -119,7 +119,7 @@ public class BlockEntityNatureAltar extends BlockEntityImpl implements ITickable
}
var stack = this.items.getStackInSlot(0);
var container = stack.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
var container = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).orElse(null);
if (!stack.isEmpty() && container != null) {
var theoreticalDrain = this.container.drainAura(1000, true);
if (theoreticalDrain > 0) {

View file

@ -33,7 +33,7 @@ public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<
@Nullable
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
return capability == NaturesAuraAPI.capAuraChunk ? this.lazyChunk.cast() : LazyOptional.empty();
return capability == NaturesAuraAPI.CAP_AURA_CHUNK ? this.lazyChunk.cast() : LazyOptional.empty();
}
@Override

View file

@ -6,7 +6,6 @@ import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
import de.ellpeck.naturesaura.api.aura.chunk.ISpotDrainable;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
@ -25,7 +24,7 @@ public class ReplenishingEffect implements IDrainSpotEffect {
if (spot < 0) {
List<ISpotDrainable> tiles = new ArrayList<>();
Helper.getBlockEntitiesInArea(level, pos, 25, tile -> {
var container = tile.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
var container = tile.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).orElse(null);
if (container instanceof ISpotDrainable)
tiles.add((ISpotDrainable) container);
return false;

View file

@ -48,7 +48,7 @@ public class JEINaturesAuraPlugin implements IModPlugin {
registration.registerSubtypeInterpreter(ModItems.AURA_BOTTLE, (stack, context) -> ItemAuraBottle.getType(stack).getName().toString());
var auraInterpreter = (IIngredientSubtypeInterpreter<ItemStack>) (stack, context) -> {
var container = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
var container = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).orElse(null);
if (container != null)
return String.valueOf(container.getStoredAura());
return IIngredientSubtypeInterpreter.NONE;

View file

@ -20,12 +20,12 @@ public class AuraMendingEnchantment extends ModEnchantment {
@Override
public boolean canEnchant(ItemStack stack) {
return super.canEnchant(stack) && !stack.getCapability(NaturesAuraAPI.capAuraRecharge).isPresent();
return super.canEnchant(stack) && !stack.getCapability(NaturesAuraAPI.CAP_AURA_RECHARGE).isPresent();
}
@Override
public boolean canApplyAtEnchantingTable(ItemStack stack) {
return super.canApplyAtEnchantingTable(stack) && !stack.getCapability(NaturesAuraAPI.capAuraRecharge).isPresent();
return super.canApplyAtEnchantingTable(stack) && !stack.getCapability(NaturesAuraAPI.CAP_AURA_RECHARGE).isPresent();
}
@Override

View file

@ -3,7 +3,6 @@ package de.ellpeck.naturesaura.events;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Vector3d;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.NaturesAura;
@ -27,16 +26,12 @@ import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.util.Tuple;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
@ -250,7 +245,7 @@ public class ClientEvents {
var res = event.getWindow();
if (mc.player != null) {
if (!heldCache.isEmpty()) {
var container = heldCache.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
var container = heldCache.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).orElse(null);
var width = Mth.ceil(container.getStoredAura() / (float) container.getMaxAura() * 80);
int conf = ModConfig.instance.cacheBarLocation.get();
@ -360,7 +355,7 @@ public class ClientEvents {
IAuraContainer container;
var x = res.getGuiScaledWidth() / 2;
var y = res.getGuiScaledHeight() / 2;
if (tile != null && (container = tile.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null)) != null) {
if (tile != null && (container = tile.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).orElse(null)) != null) {
var state = mc.level.getBlockState(pos);
var blockStack = state.getBlock().getCloneItemStack(state, blockHitResult, mc.level, pos, mc.player);
this.drawContainerInfo(stack, container.getStoredAura(), container.getMaxAura(), container.getAuraColor(),
@ -369,7 +364,7 @@ public class ClientEvents {
if (tile instanceof BlockEntityNatureAltar) {
var tileStack = ((BlockEntityNatureAltar) tile).getItemHandler().getStackInSlot(0);
if (!tileStack.isEmpty()) {
var stackCont = tileStack.getCapability(NaturesAuraAPI.capAuraContainer, null).orElse(null);
var stackCont = tileStack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER, null).orElse(null);
if (stackCont != null) {
this.drawContainerInfo(stack, stackCont.getStoredAura(), stackCont.getMaxAura(), stackCont.getAuraColor(),
mc, res, 55, tileStack.getDisplayName().getString(), null);

View file

@ -73,7 +73,7 @@ public class CommonEvents {
public void onChunkUnload(ChunkEvent.Unload event) {
var iChunk = event.getChunk();
if (iChunk instanceof LevelChunk chunk) {
var auraChunk = chunk.getCapability(NaturesAuraAPI.capAuraChunk).orElse(null);
var auraChunk = chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK).orElse(null);
if (auraChunk instanceof AuraChunk) {
var data = (LevelData) ILevelData.getLevelData(chunk.getLevel());
data.auraChunksWithSpots.remove(chunk.getPos().toLong());
@ -109,7 +109,7 @@ public class CommonEvents {
var chunk = holder.getTickingChunk();
if (chunk == null)
continue;
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk, null).orElse(null);
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK, null).orElse(null);
if (auraChunk != null)
auraChunk.update();
}
@ -149,7 +149,7 @@ public class CommonEvents {
var chunk = Helper.getLoadedChunk(player.level, pos.x, pos.z);
if (chunk == null)
return false;
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk, null).orElse(null);
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK, null).orElse(null);
if (auraChunk == null)
return false;
PacketHandler.sendTo(player, auraChunk.makePacket());

View file

@ -4,9 +4,7 @@ import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Vector3f;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.container.ItemAuraContainer;
import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge;
import de.ellpeck.naturesaura.api.render.ITrinketItem;
import de.ellpeck.naturesaura.enchant.ModEnchantments;
import net.minecraft.client.Minecraft;
@ -44,14 +42,14 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
@Override
public void inventoryTick(ItemStack stackIn, Level levelIn, Entity entityIn, int itemSlot, boolean isSelected) {
if (!levelIn.isClientSide && entityIn instanceof Player player) {
if (player.isCrouching() && stackIn.getCapability(NaturesAuraAPI.capAuraContainer).isPresent()) {
var container = stackIn.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
if (player.isCrouching() && stackIn.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).isPresent()) {
var container = stackIn.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).orElse(null);
if (container.getStoredAura() <= 0) {
return;
}
for (var i = 0; i < player.getInventory().getContainerSize(); i++) {
var stack = player.getInventory().getItem(i);
var recharge = stack.getCapability(NaturesAuraAPI.capAuraRecharge).orElse(null);
var recharge = stack.getCapability(NaturesAuraAPI.CAP_AURA_RECHARGE).orElse(null);
if (recharge != null) {
if (recharge.rechargeFromContainer(container, itemSlot, i, player.getInventory().selected == i))
break;
@ -72,7 +70,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
items.add(new ItemStack(this));
var stack = new ItemStack(this);
stack.getCapability(NaturesAuraAPI.capAuraContainer).ifPresent(container -> {
stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).ifPresent(container -> {
container.storeAura(container.getMaxAura(), false);
items.add(stack);
});
@ -86,8 +84,8 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
@Override
public int getBarWidth(ItemStack stack) {
if (stack.getCapability(NaturesAuraAPI.capAuraContainer).isPresent()) {
var container = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
if (stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).isPresent()) {
var container = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).orElse(null);
return Math.round((container.getStoredAura() / (float) container.getMaxAura()) * 13);
}
return 0;
@ -95,7 +93,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
@Override
public int getBarColor(ItemStack stack) {
var cap = stack.getCapability(NaturesAuraAPI.capAuraContainer).orElse(null);
var cap = stack.getCapability(NaturesAuraAPI.CAP_AURA_CONTAINER).orElse(null);
return cap != null ? cap.getAuraColor() : super.getBarColor(stack);
}
@ -108,7 +106,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
if (capability == NaturesAuraAPI.capAuraContainer) {
if (capability == NaturesAuraAPI.CAP_AURA_CONTAINER) {
return this.container.cast();
} else {
return LazyOptional.empty();

View file

@ -18,7 +18,6 @@ import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
@ -39,7 +38,7 @@ public class LevelData implements ILevelData {
@Nullable
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
return capability == NaturesAuraAPI.capLevelData ? this.lazyThis.cast() : LazyOptional.empty();
return capability == NaturesAuraAPI.CAP_LEVEL_DATA ? this.lazyThis.cast() : LazyOptional.empty();
}
@Override

View file

@ -7,7 +7,6 @@ import de.ellpeck.naturesaura.events.ClientEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.network.NetworkEvent;
import org.apache.commons.lang3.mutable.MutableInt;
@ -68,7 +67,7 @@ public class PacketAuraChunk {
var chunk = level.getChunk(this.chunkX, this.chunkZ);
if (chunk.isEmpty())
return false;
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk).orElse(null);
var auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.CAP_AURA_CHUNK).orElse(null);
if (auraChunk == null)
return false;
auraChunk.setSpots(this.drainSpots);