mirror of
https://github.com/Ellpeck/NaturesAura.git
synced 2024-12-22 14:59:22 +01:00
some API improvements
This commit is contained in:
parent
b1f4d3ebf9
commit
d62cef58ef
15 changed files with 83 additions and 107 deletions
|
@ -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
|
||||
|
|
|
@ -34,14 +34,14 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
|
|||
public static final int DEFAULT_AURA = 10000;
|
||||
|
||||
private final Chunk chunk;
|
||||
private final Map<BlockPos, DrainSpot> drainSpots = new HashMap<>();
|
||||
private final Map<BlockPos, MutableInt> drainSpots = new HashMap<>();
|
||||
private boolean needsSync;
|
||||
|
||||
public AuraChunk(Chunk chunk) {
|
||||
this.chunk = chunk;
|
||||
}
|
||||
|
||||
public static void getSpotsInArea(World world, BlockPos pos, int radius, BiConsumer<BlockPos, DrainSpot> consumer) {
|
||||
public static void getSpotsInArea(World world, BlockPos pos, int radius, BiConsumer<BlockPos, MutableInt> 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<NBTTagCo
|
|||
|
||||
public static int getAuraInArea(World world, BlockPos pos, int radius) {
|
||||
MutableInt result = new MutableInt(DEFAULT_AURA);
|
||||
getSpotsInArea(world, pos, radius, (blockPos, drainSpot) -> 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<NBTTagCo
|
|||
return closest;
|
||||
}
|
||||
|
||||
public void getSpotsInArea(BlockPos pos, int radius, BiConsumer<BlockPos, DrainSpot> consumer) {
|
||||
for (Map.Entry<BlockPos, DrainSpot> entry : this.drainSpots.entrySet()) {
|
||||
public void getSpotsInArea(BlockPos pos, int radius, BiConsumer<BlockPos, MutableInt> consumer) {
|
||||
for (Map.Entry<BlockPos, MutableInt> 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<NBTTagCo
|
|||
}
|
||||
|
||||
public void drainAura(BlockPos pos, int amount) {
|
||||
DrainSpot spot = this.getDrainSpot(pos);
|
||||
spot.drain(amount);
|
||||
if (spot.isEmpty())
|
||||
MutableInt spot = this.getDrainSpot(pos);
|
||||
spot.subtract(amount);
|
||||
if (spot.intValue() == 0)
|
||||
this.drainSpots.remove(pos);
|
||||
this.markDirty();
|
||||
}
|
||||
|
||||
public void storeAura(BlockPos pos, int amount) {
|
||||
DrainSpot spot = this.getDrainSpot(pos);
|
||||
spot.store(amount);
|
||||
if (spot.isEmpty())
|
||||
MutableInt spot = this.getDrainSpot(pos);
|
||||
spot.add(amount);
|
||||
if (spot.intValue() == 0)
|
||||
this.drainSpots.remove(pos);
|
||||
this.markDirty();
|
||||
}
|
||||
|
||||
private DrainSpot getDrainSpot(BlockPos pos) {
|
||||
DrainSpot spot = this.drainSpots.get(pos);
|
||||
private MutableInt getDrainSpot(BlockPos pos) {
|
||||
MutableInt spot = this.drainSpots.get(pos);
|
||||
if (spot == null) {
|
||||
spot = new DrainSpot(0);
|
||||
spot = new MutableInt();
|
||||
this.drainSpots.put(pos, spot);
|
||||
}
|
||||
return spot;
|
||||
}
|
||||
|
||||
public void setSpots(Map<BlockPos, DrainSpot> spots) {
|
||||
public void setSpots(Map<BlockPos, MutableInt> spots) {
|
||||
this.drainSpots.clear();
|
||||
this.drainSpots.putAll(spots);
|
||||
}
|
||||
|
@ -138,27 +138,28 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
|
|||
}
|
||||
|
||||
if (world.getTotalWorldTime() % 40 == 0) {
|
||||
for (Map.Entry<BlockPos, DrainSpot> entry : this.drainSpots.entrySet()) {
|
||||
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
|
||||
BlockPos pos = entry.getKey();
|
||||
int amount = entry.getValue().getAmount();
|
||||
int amount = entry.getValue().intValue();
|
||||
if (amount < 0) {
|
||||
List<TileEntity> 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<NBTTagCo
|
|||
@Override
|
||||
public NBTTagCompound serializeNBT() {
|
||||
NBTTagList list = new NBTTagList();
|
||||
for (Map.Entry<BlockPos, DrainSpot> entry : this.drainSpots.entrySet()) {
|
||||
for (Map.Entry<BlockPos, MutableInt> 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<NBTTagCo
|
|||
NBTTagCompound tag = (NBTTagCompound) base;
|
||||
this.drainSpots.put(
|
||||
BlockPos.fromLong(tag.getLong("pos")),
|
||||
new DrainSpot(tag.getInteger("amount")));
|
||||
new MutableInt(tag.getInteger("amount")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
package de.ellpeck.naturesaura.aura.chunk;
|
||||
|
||||
public class DrainSpot {
|
||||
|
||||
private int amount;
|
||||
|
||||
public DrainSpot(int amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public void drain(int amount) {
|
||||
this.amount -= amount;
|
||||
}
|
||||
|
||||
public void store(int amount) {
|
||||
this.amount += amount;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return this.amount;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.getAmount() == 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package de.ellpeck.naturesaura.aura.chunk;
|
||||
|
||||
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
||||
|
||||
public interface ISpotDrainable extends IAuraContainer {
|
||||
|
||||
int drainAuraPassively(int amountToDrain, boolean simulate);
|
||||
|
||||
}
|
|
@ -1,17 +1,14 @@
|
|||
package de.ellpeck.naturesaura.aura.container;
|
||||
|
||||
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class BasicAuraContainer implements IAuraContainer {
|
||||
|
||||
protected final int maxAura;
|
||||
protected final boolean artificial;
|
||||
protected int aura;
|
||||
|
||||
public BasicAuraContainer(int maxAura, boolean artificial) {
|
||||
public BasicAuraContainer(int maxAura) {
|
||||
this.maxAura = maxAura;
|
||||
this.artificial = artificial;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -47,11 +44,6 @@ public class BasicAuraContainer implements IAuraContainer {
|
|||
return 0x1E891E;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isArtificial() {
|
||||
return this.artificial;
|
||||
}
|
||||
|
||||
public void writeNBT(NBTTagCompound compound) {
|
||||
compound.setInteger("aura", this.aura);
|
||||
}
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package de.ellpeck.naturesaura.aura.container;
|
||||
|
||||
public class FiniteAuraContainer extends BasicAuraContainer {
|
||||
|
||||
public FiniteAuraContainer(int aura, boolean artificial) {
|
||||
super(aura, artificial);
|
||||
this.aura = aura;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int storeAura(int amountToStore, boolean simulate) {
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,4 @@ public interface IAuraContainer {
|
|||
int getMaxAura();
|
||||
|
||||
int getAuraColor();
|
||||
|
||||
boolean isArtificial();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package de.ellpeck.naturesaura.aura.container;
|
||||
|
||||
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
|
@ -8,12 +7,10 @@ public class ItemAuraContainer implements IAuraContainer {
|
|||
|
||||
protected final ItemStack stack;
|
||||
protected final int maxAura;
|
||||
protected final boolean artificial;
|
||||
|
||||
public ItemAuraContainer(ItemStack stack, int maxAura, boolean artificial) {
|
||||
public ItemAuraContainer(ItemStack stack, int maxAura) {
|
||||
this.stack = stack;
|
||||
this.maxAura = maxAura;
|
||||
this.artificial = artificial;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -61,9 +58,4 @@ public class ItemAuraContainer implements IAuraContainer {
|
|||
public int getAuraColor() {
|
||||
return 0x42a6bc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isArtificial() {
|
||||
return this.artificial;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package de.ellpeck.naturesaura.aura.container;
|
||||
|
||||
import de.ellpeck.naturesaura.aura.chunk.ISpotDrainable;
|
||||
|
||||
public class NaturalAuraContainer extends BasicAuraContainer implements ISpotDrainable {
|
||||
|
||||
private final int drainAmount;
|
||||
|
||||
public NaturalAuraContainer(int aura, int drainAmount) {
|
||||
super(aura);
|
||||
this.aura = aura;
|
||||
this.drainAmount = drainAmount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int storeAura(int amountToStore, boolean simulate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int drainAuraPassively(int amountToDrain, boolean simulate) {
|
||||
return this.drainAura(Math.min(this.drainAmount, amountToDrain), simulate);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package de.ellpeck.naturesaura.aura.item;
|
||||
|
||||
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
||||
|
||||
public interface IAuraRecharge {
|
||||
|
||||
boolean recharge();
|
||||
void rechargeFromContainer(IAuraContainer container);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package de.ellpeck.naturesaura.blocks.tiles;
|
||||
|
||||
import de.ellpeck.naturesaura.aura.container.FiniteAuraContainer;
|
||||
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
||||
import de.ellpeck.naturesaura.aura.container.NaturalAuraContainer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
public class TileEntityAncientLeaves extends TileEntityImpl {
|
||||
|
||||
private final FiniteAuraContainer container = new FiniteAuraContainer(20, false) {
|
||||
private final NaturalAuraContainer container = new NaturalAuraContainer(20, 5) {
|
||||
@Override
|
||||
public int getAuraColor() {
|
||||
return 0xc46df9;
|
||||
|
|
|
@ -111,7 +111,7 @@ public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
|
|||
}
|
||||
};
|
||||
|
||||
private final BasicAuraContainer container = new BasicAuraContainer(5000, true);
|
||||
private final BasicAuraContainer container = new BasicAuraContainer(5000);
|
||||
public boolean structureFine;
|
||||
|
||||
private AltarRecipe currentRecipe;
|
||||
|
|
|
@ -53,7 +53,7 @@ public class ClientEvents {
|
|||
MutableInt spots = new MutableInt();
|
||||
AuraChunk.getSpotsInArea(mc.world, mc.player.getPosition(), 15, ((blockPos, drainSpot) -> {
|
||||
spots.increment();
|
||||
amount.add(drainSpot.getAmount());
|
||||
amount.add(drainSpot.intValue());
|
||||
}));
|
||||
left.add(prefix + "Aura: " + amount.intValue());
|
||||
left.add(prefix + "DrainSpots: " + spots.intValue());
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<BlockPos, DrainSpot> drainSpots;
|
||||
private Map<BlockPos, MutableInt> drainSpots;
|
||||
|
||||
public PacketAuraChunk(int chunkX, int chunkZ, Map<BlockPos, DrainSpot> drainSpots) {
|
||||
public PacketAuraChunk(int chunkX, int chunkZ, Map<BlockPos, MutableInt> 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<BlockPos, DrainSpot> entry : this.drainSpots.entrySet()) {
|
||||
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
|
||||
buf.writeLong(entry.getKey().toLong());
|
||||
buf.writeInt(entry.getValue().getAmount());
|
||||
buf.writeInt(entry.getValue().intValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue