generify the drain spot system so that addon devs can more easily use it

This commit is contained in:
Ellpeck 2018-11-13 11:39:28 +01:00
parent b4508abb4a
commit 6af7634d6c
14 changed files with 71 additions and 25 deletions

View file

@ -6,6 +6,7 @@ import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge;
import de.ellpeck.naturesaura.blocks.ModBlocks;
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
import de.ellpeck.naturesaura.chunk.effect.DrainSpotEffects;
import de.ellpeck.naturesaura.commands.CommandAura;
import de.ellpeck.naturesaura.compat.Compat;
import de.ellpeck.naturesaura.events.CommonEvents;
@ -78,6 +79,7 @@ public final class NaturesAura {
ModRecipes.init();
ModRegistry.init(event);
OreDict.init();
DrainSpotEffects.init();
proxy.init(event);
}

View file

@ -1,6 +1,7 @@
package de.ellpeck.naturesaura.api;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge;
import de.ellpeck.naturesaura.api.aura.type.BasicAuraType;
@ -24,6 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
/**
* The main class of the Nature's Aura API. This is where you can find recipe
@ -68,6 +70,15 @@ public final class NaturesAuraAPI {
public static final IAuraType TYPE_NETHER = new BasicAuraType(new ResourceLocation(MOD_ID, "nether"), DimensionType.NETHER, 0x871c0c).register();
public static final IAuraType TYPE_END = new BasicAuraType(new ResourceLocation(MOD_ID, "end"), DimensionType.THE_END, 0x302624).register();
public static final IAuraType TYPE_OTHER = new BasicAuraType(new ResourceLocation(MOD_ID, "other"), null, 0x2fa8a0).register();
/**
* A map of all {@link IDrainSpotEffect} suppliers which are effects that
* happen passively at every spot that Aura has been drained from in the
* world. These effects include things like vegetational increase and
* natural decay. To register your own drain spot effects, just add a
* supplier for them to this map and they will automatically be executed
* once a second for every drain spot currently loaded.
*/
public static final Map<ResourceLocation, Supplier<IDrainSpotEffect>> DRAIN_SPOT_EFFECTS = new HashMap<>();
/**
* The capability for any item or block that stores Aura in the form of an

View file

@ -112,8 +112,6 @@ public interface IAuraChunk extends ICapabilityProvider, INBTSerializable<NBTTag
return NaturesAuraAPI.instance().getHighestAuraDrainSpot(world, pos, radius, defaultSpot);
}
void addEffect(IDrainSpotEffect effect);
/**
* @see #getSpotsInArea(World, BlockPos, int, BiConsumer)
*/

View file

@ -1,6 +1,7 @@
package de.ellpeck.naturesaura.api.aura.chunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
@ -11,4 +12,6 @@ public interface IDrainSpotEffect {
void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, MutableInt spot);
boolean appliesToType(IAuraType type);
ResourceLocation getName();
}

View file

@ -1,7 +1,6 @@
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
import net.minecraft.block.BlockHopper;

View file

@ -2,7 +2,6 @@ package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.blocks.ModBlocks;
import de.ellpeck.naturesaura.items.ModItems;
import de.ellpeck.naturesaura.packet.PacketHandler;

View file

@ -1,7 +1,6 @@
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;

View file

@ -1,13 +1,9 @@
package de.ellpeck.naturesaura.aura.chunk;
package de.ellpeck.naturesaura.chunk;
import de.ellpeck.naturesaura.NaturesAura;
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.type.IAuraType;
import de.ellpeck.naturesaura.aura.chunk.effect.GrassDieEffect;
import de.ellpeck.naturesaura.aura.chunk.effect.PlantBoostEffect;
import de.ellpeck.naturesaura.aura.chunk.effect.ReplenishingEffect;
import de.ellpeck.naturesaura.packet.PacketAuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import net.minecraft.nbt.NBTBase;
@ -28,6 +24,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
public class AuraChunk implements IAuraChunk {
@ -41,15 +38,11 @@ public class AuraChunk implements IAuraChunk {
this.chunk = chunk;
this.type = type;
this.addEffect(new ReplenishingEffect());
this.addEffect(new GrassDieEffect());
this.addEffect(new PlantBoostEffect());
}
@Override
public void addEffect(IDrainSpotEffect effect) {
if (effect.appliesToType(this.type))
this.effects.add(effect);
for (Supplier<IDrainSpotEffect> supplier : NaturesAuraAPI.DRAIN_SPOT_EFFECTS.values()) {
IDrainSpotEffect effect = supplier.get();
if (effect.appliesToType(this.type))
this.effects.add(effect);
}
}
@Override
@ -116,7 +109,7 @@ public class AuraChunk implements IAuraChunk {
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
for (IDrainSpotEffect effect : this.effects) {
world.profiler.func_194340_a(() -> NaturesAura.MOD_ID + ":" + effect.getClass().getSimpleName());
world.profiler.func_194340_a(() -> effect.getName().toString());
effect.update(world, this.chunk, this, entry.getKey(), entry.getValue());
world.profiler.endSection();
}

View file

@ -0,0 +1,12 @@
package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
public final class DrainSpotEffects {
public static void init() {
NaturesAuraAPI.DRAIN_SPOT_EFFECTS.put(GrassDieEffect.NAME, GrassDieEffect::new);
NaturesAuraAPI.DRAIN_SPOT_EFFECTS.put(PlantBoostEffect.NAME, PlantBoostEffect::new);
NaturesAuraAPI.DRAIN_SPOT_EFFECTS.put(ReplenishingEffect.NAME, ReplenishingEffect::new);
}
}

View file

@ -1,5 +1,6 @@
package de.ellpeck.naturesaura.aura.chunk.effect;
package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
@ -8,6 +9,7 @@ import de.ellpeck.naturesaura.blocks.ModBlocks;
import net.minecraft.block.*;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
@ -15,6 +17,9 @@ import net.minecraft.world.chunk.Chunk;
import org.apache.commons.lang3.mutable.MutableInt;
public class GrassDieEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "grass_die");
@Override
public void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, MutableInt spot) {
if (spot.intValue() < 0) {
@ -56,4 +61,9 @@ public class GrassDieEffect implements IDrainSpotEffect {
public boolean appliesToType(IAuraType type) {
return type == NaturesAuraAPI.TYPE_OVERWORLD;
}
@Override
public ResourceLocation getName() {
return NAME;
}
}

View file

@ -1,5 +1,6 @@
package de.ellpeck.naturesaura.aura.chunk.effect;
package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
@ -10,6 +11,7 @@ import net.minecraft.block.Block;
import net.minecraft.block.IGrowable;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
@ -17,6 +19,9 @@ import net.minecraft.world.chunk.Chunk;
import org.apache.commons.lang3.mutable.MutableInt;
public class PlantBoostEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "plant_boost");
@Override
public void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, MutableInt spot) {
if (spot.intValue() <= 0)
@ -58,4 +63,9 @@ public class PlantBoostEffect implements IDrainSpotEffect {
public boolean appliesToType(IAuraType type) {
return type == NaturesAuraAPI.TYPE_OVERWORLD;
}
@Override
public ResourceLocation getName() {
return NAME;
}
}

View file

@ -1,12 +1,14 @@
package de.ellpeck.naturesaura.aura.chunk.effect;
package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.NaturesAura;
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.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
@ -16,6 +18,9 @@ import java.util.ArrayList;
import java.util.List;
public class ReplenishingEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "replenishing");
@Override
public void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, MutableInt spot) {
int amount = spot.intValue();
@ -52,4 +57,9 @@ public class ReplenishingEffect implements IDrainSpotEffect {
public boolean appliesToType(IAuraType type) {
return true;
}
@Override
public ResourceLocation getName() {
return NAME;
}
}

View file

@ -4,7 +4,7 @@ import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.chunk.AuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.WorldServer;

View file

@ -2,7 +2,7 @@ package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
import de.ellpeck.naturesaura.chunk.AuraChunk;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos;