some API improvements

This commit is contained in:
Ellpeck 2018-10-24 13:06:24 +02:00
parent b1f4d3ebf9
commit d62cef58ef
15 changed files with 83 additions and 107 deletions

View file

@ -140,12 +140,12 @@ public final class Helper {
public static ICapabilityProvider makeRechargeProvider(ItemStack stack) { public static ICapabilityProvider makeRechargeProvider(ItemStack stack) {
return new ICapabilityProvider() { return new ICapabilityProvider() {
private final IAuraRecharge recharge = () -> { private final IAuraRecharge recharge = container -> {
if (stack.getItemDamage() > 0) { int toDrain = 3;
if (stack.getItemDamage() > 0 && container.drainAura(toDrain, true) >= toDrain) {
stack.setItemDamage(stack.getItemDamage() - 1); stack.setItemDamage(stack.getItemDamage() - 1);
return true; container.drainAura(toDrain, false);
} }
return false;
}; };
@Override @Override

View file

@ -34,14 +34,14 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
public static final int DEFAULT_AURA = 10000; public static final int DEFAULT_AURA = 10000;
private final Chunk chunk; private final Chunk chunk;
private final Map<BlockPos, DrainSpot> drainSpots = new HashMap<>(); private final Map<BlockPos, MutableInt> drainSpots = new HashMap<>();
private boolean needsSync; private boolean needsSync;
public AuraChunk(Chunk chunk) { public AuraChunk(Chunk chunk) {
this.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 x = (pos.getX() - radius) >> 4; x <= (pos.getX() + radius) >> 4; x++) {
for (int z = (pos.getZ() - radius) >> 4; z <= (pos.getZ() + radius) >> 4; z++) { for (int z = (pos.getZ() - radius) >> 4; z <= (pos.getZ() + radius) >> 4; z++) {
Chunk chunk = world.getChunk(x, 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) { public static int getAuraInArea(World world, BlockPos pos, int radius) {
MutableInt result = new MutableInt(DEFAULT_AURA); 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(); return result.intValue();
} }
@ -85,8 +85,8 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
return closest; return closest;
} }
public void getSpotsInArea(BlockPos pos, int radius, BiConsumer<BlockPos, DrainSpot> consumer) { public void getSpotsInArea(BlockPos pos, int radius, BiConsumer<BlockPos, MutableInt> consumer) {
for (Map.Entry<BlockPos, DrainSpot> entry : this.drainSpots.entrySet()) { for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
BlockPos drainPos = entry.getKey(); BlockPos drainPos = entry.getKey();
if (drainPos.distanceSq(pos) <= radius * radius) { if (drainPos.distanceSq(pos) <= radius * radius) {
consumer.accept(drainPos, entry.getValue()); consumer.accept(drainPos, entry.getValue());
@ -95,31 +95,31 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
} }
public void drainAura(BlockPos pos, int amount) { public void drainAura(BlockPos pos, int amount) {
DrainSpot spot = this.getDrainSpot(pos); MutableInt spot = this.getDrainSpot(pos);
spot.drain(amount); spot.subtract(amount);
if (spot.isEmpty()) if (spot.intValue() == 0)
this.drainSpots.remove(pos); this.drainSpots.remove(pos);
this.markDirty(); this.markDirty();
} }
public void storeAura(BlockPos pos, int amount) { public void storeAura(BlockPos pos, int amount) {
DrainSpot spot = this.getDrainSpot(pos); MutableInt spot = this.getDrainSpot(pos);
spot.store(amount); spot.add(amount);
if (spot.isEmpty()) if (spot.intValue() == 0)
this.drainSpots.remove(pos); this.drainSpots.remove(pos);
this.markDirty(); this.markDirty();
} }
private DrainSpot getDrainSpot(BlockPos pos) { private MutableInt getDrainSpot(BlockPos pos) {
DrainSpot spot = this.drainSpots.get(pos); MutableInt spot = this.drainSpots.get(pos);
if (spot == null) { if (spot == null) {
spot = new DrainSpot(0); spot = new MutableInt();
this.drainSpots.put(pos, spot); this.drainSpots.put(pos, spot);
} }
return spot; return spot;
} }
public void setSpots(Map<BlockPos, DrainSpot> spots) { public void setSpots(Map<BlockPos, MutableInt> spots) {
this.drainSpots.clear(); this.drainSpots.clear();
this.drainSpots.putAll(spots); this.drainSpots.putAll(spots);
} }
@ -138,22 +138,24 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
} }
if (world.getTotalWorldTime() % 40 == 0) { 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(); BlockPos pos = entry.getKey();
int amount = entry.getValue().getAmount(); int amount = entry.getValue().intValue();
if (amount < 0) { if (amount < 0) {
List<TileEntity> tiles = new ArrayList<>(); List<TileEntity> tiles = new ArrayList<>();
Helper.getTileEntitiesInArea(world, pos, 25, tile -> { Helper.getTileEntitiesInArea(world, pos, 25, tile -> {
if (tile.hasCapability(Capabilities.auraContainer, null)) { if (tile.hasCapability(Capabilities.auraContainer, null)) {
IAuraContainer container = tile.getCapability(Capabilities.auraContainer, null);
if (container instanceof ISpotDrainable) {
tiles.add(tile); tiles.add(tile);
} }
}
}); });
if (!tiles.isEmpty()) { if (!tiles.isEmpty()) {
for (int i = world.rand.nextInt(10) + 5; i >= 0; i--) { for (int i = world.rand.nextInt(10) + 5; i >= 0; i--) {
TileEntity tile = tiles.get(world.rand.nextInt(tiles.size())); TileEntity tile = tiles.get(world.rand.nextInt(tiles.size()));
IAuraContainer container = tile.getCapability(Capabilities.auraContainer, null); IAuraContainer container = tile.getCapability(Capabilities.auraContainer, null);
if (!container.isArtificial()) { int drained = ((ISpotDrainable) container).drainAuraPassively(-amount, false);
int drained = container.drainAura(Math.min(-amount, 5), false);
this.storeAura(pos, drained); this.storeAura(pos, drained);
amount += drained; amount += drained;
if (amount >= drained) { if (amount >= drained) {
@ -165,7 +167,6 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
} }
} }
} }
}
public IMessage makePacket() { public IMessage makePacket() {
return new PacketAuraChunk(this.chunk.x, this.chunk.z, this.drainSpots); return new PacketAuraChunk(this.chunk.x, this.chunk.z, this.drainSpots);
@ -185,10 +186,10 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
@Override @Override
public NBTTagCompound serializeNBT() { public NBTTagCompound serializeNBT() {
NBTTagList list = new NBTTagList(); 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(); NBTTagCompound tag = new NBTTagCompound();
tag.setLong("pos", entry.getKey().toLong()); tag.setLong("pos", entry.getKey().toLong());
tag.setInteger("amount", entry.getValue().getAmount()); tag.setInteger("amount", entry.getValue().intValue());
list.appendTag(tag); list.appendTag(tag);
} }
@ -205,7 +206,7 @@ public class AuraChunk implements ICapabilityProvider, INBTSerializable<NBTTagCo
NBTTagCompound tag = (NBTTagCompound) base; NBTTagCompound tag = (NBTTagCompound) base;
this.drainSpots.put( this.drainSpots.put(
BlockPos.fromLong(tag.getLong("pos")), BlockPos.fromLong(tag.getLong("pos")),
new DrainSpot(tag.getInteger("amount"))); new MutableInt(tag.getInteger("amount")));
} }
} }
} }

View file

@ -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;
}
}

View file

@ -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);
}

View file

@ -1,17 +1,14 @@
package de.ellpeck.naturesaura.aura.container; package de.ellpeck.naturesaura.aura.container;
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
public class BasicAuraContainer implements IAuraContainer { public class BasicAuraContainer implements IAuraContainer {
protected final int maxAura; protected final int maxAura;
protected final boolean artificial;
protected int aura; protected int aura;
public BasicAuraContainer(int maxAura, boolean artificial) { public BasicAuraContainer(int maxAura) {
this.maxAura = maxAura; this.maxAura = maxAura;
this.artificial = artificial;
} }
@Override @Override
@ -47,11 +44,6 @@ public class BasicAuraContainer implements IAuraContainer {
return 0x1E891E; return 0x1E891E;
} }
@Override
public boolean isArtificial() {
return this.artificial;
}
public void writeNBT(NBTTagCompound compound) { public void writeNBT(NBTTagCompound compound) {
compound.setInteger("aura", this.aura); compound.setInteger("aura", this.aura);
} }

View file

@ -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;
}
}

View file

@ -10,6 +10,4 @@ public interface IAuraContainer {
int getMaxAura(); int getMaxAura();
int getAuraColor(); int getAuraColor();
boolean isArtificial();
} }

View file

@ -1,6 +1,5 @@
package de.ellpeck.naturesaura.aura.container; package de.ellpeck.naturesaura.aura.container;
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -8,12 +7,10 @@ public class ItemAuraContainer implements IAuraContainer {
protected final ItemStack stack; protected final ItemStack stack;
protected final int maxAura; 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.stack = stack;
this.maxAura = maxAura; this.maxAura = maxAura;
this.artificial = artificial;
} }
@Override @Override
@ -61,9 +58,4 @@ public class ItemAuraContainer implements IAuraContainer {
public int getAuraColor() { public int getAuraColor() {
return 0x42a6bc; return 0x42a6bc;
} }
@Override
public boolean isArtificial() {
return this.artificial;
}
} }

View file

@ -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);
}
}

View file

@ -1,7 +1,9 @@
package de.ellpeck.naturesaura.aura.item; package de.ellpeck.naturesaura.aura.item;
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
public interface IAuraRecharge { public interface IAuraRecharge {
boolean recharge(); void rechargeFromContainer(IAuraContainer container);
} }

View file

@ -1,13 +1,13 @@
package de.ellpeck.naturesaura.blocks.tiles; 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.IAuraContainer;
import de.ellpeck.naturesaura.aura.container.NaturalAuraContainer;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
public class TileEntityAncientLeaves extends TileEntityImpl { public class TileEntityAncientLeaves extends TileEntityImpl {
private final FiniteAuraContainer container = new FiniteAuraContainer(20, false) { private final NaturalAuraContainer container = new NaturalAuraContainer(20, 5) {
@Override @Override
public int getAuraColor() { public int getAuraColor() {
return 0xc46df9; return 0xc46df9;

View file

@ -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; public boolean structureFine;
private AltarRecipe currentRecipe; private AltarRecipe currentRecipe;

View file

@ -53,7 +53,7 @@ public class ClientEvents {
MutableInt spots = new MutableInt(); MutableInt spots = new MutableInt();
AuraChunk.getSpotsInArea(mc.world, mc.player.getPosition(), 15, ((blockPos, drainSpot) -> { AuraChunk.getSpotsInArea(mc.world, mc.player.getPosition(), 15, ((blockPos, drainSpot) -> {
spots.increment(); spots.increment();
amount.add(drainSpot.getAmount()); amount.add(drainSpot.intValue());
})); }));
left.add(prefix + "Aura: " + amount.intValue()); left.add(prefix + "Aura: " + amount.intValue());
left.add(prefix + "DrainSpots: " + spots.intValue()); left.add(prefix + "DrainSpots: " + spots.intValue());

View file

@ -39,9 +39,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
ItemStack stack = player.getHeldItemMainhand(); ItemStack stack = player.getHeldItemMainhand();
if (stack.hasCapability(Capabilities.auraRecharge, null)) { if (stack.hasCapability(Capabilities.auraRecharge, null)) {
IAuraContainer container = stackIn.getCapability(Capabilities.auraContainer, null); IAuraContainer container = stackIn.getCapability(Capabilities.auraContainer, null);
if (container.getStoredAura() >= 3 && stack.getCapability(Capabilities.auraRecharge, null).recharge()) { stack.getCapability(Capabilities.auraRecharge, null).rechargeFromContainer(container);
container.drainAura(4, false);
}
} }
} }
} }
@ -78,7 +76,7 @@ public class ItemAuraCache extends ItemImpl implements ITrinketItem {
@Override @Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) { public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
return new ICapabilityProvider() { return new ICapabilityProvider() {
private final ItemAuraContainer container = new ItemAuraContainer(stack, 4000, true); private final ItemAuraContainer container = new ItemAuraContainer(stack, 4000);
@Override @Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) { public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {

View file

@ -3,7 +3,6 @@ package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.aura.Capabilities; import de.ellpeck.naturesaura.aura.Capabilities;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk; import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.aura.chunk.DrainSpot;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos; 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.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -22,9 +22,9 @@ public class PacketAuraChunk implements IMessage {
private int chunkX; private int chunkX;
private int chunkZ; 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.chunkX = chunkX;
this.chunkZ = chunkZ; this.chunkZ = chunkZ;
this.drainSpots = drainSpots; this.drainSpots = drainSpots;
@ -44,7 +44,7 @@ public class PacketAuraChunk implements IMessage {
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
this.drainSpots.put( this.drainSpots.put(
BlockPos.fromLong(buf.readLong()), 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.chunkZ);
buf.writeInt(this.drainSpots.size()); 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.writeLong(entry.getKey().toLong());
buf.writeInt(entry.getValue().getAmount()); buf.writeInt(entry.getValue().intValue());
} }
} }