From d62cef58efd97aefbe4ed295fa42ef57d779699d Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 24 Oct 2018 13:06:24 +0200 Subject: [PATCH] some API improvements --- .../java/de/ellpeck/naturesaura/Helper.java | 8 +-- .../naturesaura/aura/chunk/AuraChunk.java | 57 ++++++++++--------- .../naturesaura/aura/chunk/DrainSpot.java | 26 --------- .../aura/chunk/ISpotDrainable.java | 9 +++ .../aura/container/BasicAuraContainer.java | 10 +--- .../aura/container/FiniteAuraContainer.java | 14 ----- .../aura/container/IAuraContainer.java | 2 - .../aura/container/ItemAuraContainer.java | 10 +--- .../aura/container/NaturalAuraContainer.java | 24 ++++++++ .../naturesaura/aura/item/IAuraRecharge.java | 4 +- .../blocks/tiles/TileEntityAncientLeaves.java | 4 +- .../blocks/tiles/TileEntityNatureAltar.java | 2 +- .../naturesaura/events/ClientEvents.java | 2 +- .../naturesaura/items/ItemAuraCache.java | 6 +- .../naturesaura/packet/PacketAuraChunk.java | 12 ++-- 15 files changed, 83 insertions(+), 107 deletions(-) delete mode 100644 src/main/java/de/ellpeck/naturesaura/aura/chunk/DrainSpot.java create mode 100644 src/main/java/de/ellpeck/naturesaura/aura/chunk/ISpotDrainable.java delete mode 100644 src/main/java/de/ellpeck/naturesaura/aura/container/FiniteAuraContainer.java create mode 100644 src/main/java/de/ellpeck/naturesaura/aura/container/NaturalAuraContainer.java diff --git a/src/main/java/de/ellpeck/naturesaura/Helper.java b/src/main/java/de/ellpeck/naturesaura/Helper.java index bd811ae5..98ffe255 100644 --- a/src/main/java/de/ellpeck/naturesaura/Helper.java +++ b/src/main/java/de/ellpeck/naturesaura/Helper.java @@ -140,12 +140,12 @@ public final class Helper { public static ICapabilityProvider makeRechargeProvider(ItemStack stack) { return new ICapabilityProvider() { - private final IAuraRecharge recharge = () -> { - if (stack.getItemDamage() > 0) { + private final IAuraRecharge recharge = container -> { + int toDrain = 3; + if (stack.getItemDamage() > 0 && container.drainAura(toDrain, true) >= toDrain) { stack.setItemDamage(stack.getItemDamage() - 1); - return true; + container.drainAura(toDrain, false); } - return false; }; @Override diff --git a/src/main/java/de/ellpeck/naturesaura/aura/chunk/AuraChunk.java b/src/main/java/de/ellpeck/naturesaura/aura/chunk/AuraChunk.java index a74f2f74..ccd67622 100644 --- a/src/main/java/de/ellpeck/naturesaura/aura/chunk/AuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/aura/chunk/AuraChunk.java @@ -34,14 +34,14 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable drainSpots = new HashMap<>(); + private final Map drainSpots = new HashMap<>(); private boolean needsSync; public AuraChunk(Chunk chunk) { this.chunk = chunk; } - public static void getSpotsInArea(World world, BlockPos pos, int radius, BiConsumer consumer) { + public static void getSpotsInArea(World world, BlockPos pos, int radius, BiConsumer consumer) { for (int x = (pos.getX() - radius) >> 4; x <= (pos.getX() + radius) >> 4; x++) { for (int z = (pos.getZ() - radius) >> 4; z <= (pos.getZ() + radius) >> 4; z++) { Chunk chunk = world.getChunk(x, z); @@ -55,7 +55,7 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable result.add(drainSpot.getAmount())); + getSpotsInArea(world, pos, radius, (blockPos, drainSpot) -> result.add(drainSpot.intValue())); return result.intValue(); } @@ -85,8 +85,8 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable consumer) { - for (Map.Entry entry : this.drainSpots.entrySet()) { + public void getSpotsInArea(BlockPos pos, int radius, BiConsumer consumer) { + for (Map.Entry entry : this.drainSpots.entrySet()) { BlockPos drainPos = entry.getKey(); if (drainPos.distanceSq(pos) <= radius * radius) { consumer.accept(drainPos, entry.getValue()); @@ -95,31 +95,31 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable spots) { + public void setSpots(Map spots) { this.drainSpots.clear(); this.drainSpots.putAll(spots); } @@ -138,27 +138,28 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable entry : this.drainSpots.entrySet()) { + for (Map.Entry entry : this.drainSpots.entrySet()) { BlockPos pos = entry.getKey(); - int amount = entry.getValue().getAmount(); + int amount = entry.getValue().intValue(); if (amount < 0) { List tiles = new ArrayList<>(); Helper.getTileEntitiesInArea(world, pos, 25, tile -> { if (tile.hasCapability(Capabilities.auraContainer, null)) { - tiles.add(tile); + IAuraContainer container = tile.getCapability(Capabilities.auraContainer, null); + if (container instanceof ISpotDrainable) { + tiles.add(tile); + } } }); if (!tiles.isEmpty()) { for (int i = world.rand.nextInt(10) + 5; i >= 0; i--) { TileEntity tile = tiles.get(world.rand.nextInt(tiles.size())); IAuraContainer container = tile.getCapability(Capabilities.auraContainer, null); - if (!container.isArtificial()) { - int drained = container.drainAura(Math.min(-amount, 5), false); - this.storeAura(pos, drained); - amount += drained; - if (amount >= drained) { - break; - } + int drained = ((ISpotDrainable) container).drainAuraPassively(-amount, false); + this.storeAura(pos, drained); + amount += drained; + if (amount >= drained) { + break; } } } @@ -185,10 +186,10 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable entry : this.drainSpots.entrySet()) { + for (Map.Entry entry : this.drainSpots.entrySet()) { NBTTagCompound tag = new NBTTagCompound(); tag.setLong("pos", entry.getKey().toLong()); - tag.setInteger("amount", entry.getValue().getAmount()); + tag.setInteger("amount", entry.getValue().intValue()); list.appendTag(tag); } @@ -205,7 +206,7 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable { spots.increment(); - amount.add(drainSpot.getAmount()); + amount.add(drainSpot.intValue()); })); left.add(prefix + "Aura: " + amount.intValue()); left.add(prefix + "DrainSpots: " + spots.intValue()); diff --git a/src/main/java/de/ellpeck/naturesaura/items/ItemAuraCache.java b/src/main/java/de/ellpeck/naturesaura/items/ItemAuraCache.java index aaab202c..0e9efd6b 100644 --- a/src/main/java/de/ellpeck/naturesaura/items/ItemAuraCache.java +++ b/src/main/java/de/ellpeck/naturesaura/items/ItemAuraCache.java @@ -39,9 +39,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem { ItemStack stack = player.getHeldItemMainhand(); if (stack.hasCapability(Capabilities.auraRecharge, null)) { IAuraContainer container = stackIn.getCapability(Capabilities.auraContainer, null); - if (container.getStoredAura() >= 3 && stack.getCapability(Capabilities.auraRecharge, null).recharge()) { - container.drainAura(4, false); - } + stack.getCapability(Capabilities.auraRecharge, null).rechargeFromContainer(container); } } } @@ -78,7 +76,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem { @Override public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) { return new ICapabilityProvider() { - private final ItemAuraContainer container = new ItemAuraContainer(stack, 4000, true); + private final ItemAuraContainer container = new ItemAuraContainer(stack, 4000); @Override public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { diff --git a/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java b/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java index 313eda6d..d001efb3 100644 --- a/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/packet/PacketAuraChunk.java @@ -3,7 +3,6 @@ package de.ellpeck.naturesaura.packet; import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.aura.Capabilities; import de.ellpeck.naturesaura.aura.chunk.AuraChunk; -import de.ellpeck.naturesaura.aura.chunk.DrainSpot; import io.netty.buffer.ByteBuf; import net.minecraft.client.Minecraft; import net.minecraft.util.math.BlockPos; @@ -14,6 +13,7 @@ import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import org.apache.commons.lang3.mutable.MutableInt; import java.util.HashMap; import java.util.Map; @@ -22,9 +22,9 @@ public class PacketAuraChunk implements IMessage { private int chunkX; private int chunkZ; - private Map drainSpots; + private Map drainSpots; - public PacketAuraChunk(int chunkX, int chunkZ, Map drainSpots) { + public PacketAuraChunk(int chunkX, int chunkZ, Map drainSpots) { this.chunkX = chunkX; this.chunkZ = chunkZ; this.drainSpots = drainSpots; @@ -44,7 +44,7 @@ public class PacketAuraChunk implements IMessage { for (int i = 0; i < amount; i++) { this.drainSpots.put( BlockPos.fromLong(buf.readLong()), - new DrainSpot(buf.readInt()) + new MutableInt(buf.readInt()) ); } } @@ -55,9 +55,9 @@ public class PacketAuraChunk implements IMessage { buf.writeInt(this.chunkZ); buf.writeInt(this.drainSpots.size()); - for (Map.Entry entry : this.drainSpots.entrySet()) { + for (Map.Entry entry : this.drainSpots.entrySet()) { buf.writeLong(entry.getKey().toLong()); - buf.writeInt(entry.getValue().getAmount()); + buf.writeInt(entry.getValue().intValue()); } }