Skip to content

Commit

Permalink
Add ability to opt out of aura handling (closes #1), made aura info c…
Browse files Browse the repository at this point in the history
…apability actually registered (closes #2)
  • Loading branch information
TheCodex6824 committed Jul 25, 2020
1 parent dfd5454 commit a13b929
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 10 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.jvmargs=-Xmx3G
version=1.0.1
apiversion=1.0.1
version=1.0.2
apiversion=1.0.2
mcversion=1.12.2
forgeminversion=14.23.5.2768
forgeversion=14.23.5.2847
11 changes: 9 additions & 2 deletions src/main/java/thecodex6824/auracontrol/AuraConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,23 @@ private AuraConfig() {}
@RequiresMcRestart
public static String[] biomeList = {};

@Name("ControlAura")
@Comment({
"Whether the biome list should affect Aura generation."
})
@RequiresMcRestart
public static boolean aura = true;

@Name("ControlCrystals")
@Comment({
"Whether the biome list should affect Vis Crystal generation, in addition to the aura."
"Whether the biome list should affect Vis Crystal generation."
})
@RequiresMcRestart
public static boolean crystals = false;

@Name("ControlTrees")
@Comment({
"Whether the biome list should affect Thaumcraft tree generation, in addition to the aura."
"Whether the biome list should affect Thaumcraft tree generation."
})
@RequiresMcRestart
public static boolean trees = false;
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/thecodex6824/auracontrol/AuraControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,35 @@

import java.util.Set;

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableSet;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import thecodex6824.auracontrol.api.AuraControlAPI;
import thecodex6824.auracontrol.api.CapabilityOriginalAuraInfo;
import thecodex6824.auracontrol.api.IOriginalAuraInfo;
import thecodex6824.auracontrol.api.OriginalAuraInfo;

@Mod(modid = AuraControlAPI.MODID, name = AuraControlAPI.NAME, version = AuraControl.VERSION, useMetadata = true)
@EventBusSubscriber(modid = AuraControlAPI.MODID)
public class AuraControl {
public static final String VERSION = "@VERSION@";

Expand All @@ -43,6 +60,24 @@ public class AuraControl {
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
AuraControlAPI.setMethodHandler(new InternalMethodHandler());
CapabilityManager.INSTANCE.register(IOriginalAuraInfo.class, new IStorage<IOriginalAuraInfo>() {
@Override
public void readNBT(Capability<IOriginalAuraInfo> capability, IOriginalAuraInfo instance, EnumFacing side, NBTBase nbt) {
if (!(instance instanceof OriginalAuraInfo) || !(nbt instanceof NBTTagCompound))
throw new UnsupportedOperationException("Can't deserialize non-API implementation");

((OriginalAuraInfo) instance).deserializeNBT((NBTTagCompound) nbt);
}

@Override
@Nullable
public NBTBase writeNBT(Capability<IOriginalAuraInfo> capability, IOriginalAuraInfo instance, EnumFacing side) {
if (!(instance instanceof OriginalAuraInfo))
throw new UnsupportedOperationException("Can't serialize non-API implementation");

return ((OriginalAuraInfo) instance).serializeNBT();
}
}, OriginalAuraInfo::new);
}

@EventHandler
Expand All @@ -55,11 +90,18 @@ public void init(FMLInitializationEvent event) {
else
allowedBiomes.removeIf(b -> list.contains(b.getRegistryName().toString()));

AuraControlAPI.setHandleAuraGen(AuraConfig.aura);
AuraControlAPI.setHandleCrystalGen(AuraConfig.crystals);
AuraControlAPI.setHandleTreeGen(AuraConfig.trees);

GameRegistry.registerWorldGenerator(new AuraWorldSetupGenerator(), -1);
GameRegistry.registerWorldGenerator(new AuraWorldGenerator(), 100000);
}

@SubscribeEvent
public static void onAttachCapabilityChunk(AttachCapabilitiesEvent<Chunk> event) {
event.addCapability(new ResourceLocation(AuraControlAPI.NAME, "original_aura_info"),
new SimpleCapabilityProvider<>(new OriginalAuraInfo(), CapabilityOriginalAuraInfo.AURA_INFO));
}

}
25 changes: 19 additions & 6 deletions src/main/java/thecodex6824/auracontrol/InternalMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class InternalMethodHandler implements IInternalMethodHandler {
protected HashSet<Biome> allowedBiomes = new HashSet<>();
protected boolean crystalGen;
protected boolean treeGen;
protected boolean auraGen;

@Override
public Set<Biome> getAllowedBiomes() {
Expand All @@ -45,12 +46,14 @@ public Set<Biome> getAllowedBiomes() {

@Override
public void handleAura(World world, int chunkX, int chunkZ) {
Biome biome = world.getBiome(new BlockPos(chunkX * 16 + 8, 64, chunkZ * 16 + 8));
if (!AuraControlAPI.getAllowedBiomes().contains(biome)) {
AuraChunk chunk = AuraHandler.getAuraChunk(world.provider.getDimension(), chunkX, chunkZ);
chunk.setBase((short) 0);
chunk.setVis(0.0F);
chunk.setFlux(0.0F);
if (auraGen) {
Biome biome = world.getBiome(new BlockPos(chunkX * 16 + 8, 64, chunkZ * 16 + 8));
if (!AuraControlAPI.getAllowedBiomes().contains(biome)) {
AuraChunk chunk = AuraHandler.getAuraChunk(world.provider.getDimension(), chunkX, chunkZ);
chunk.setBase((short) 0);
chunk.setVis(0.0F);
chunk.setFlux(0.0F);
}
}
}

Expand All @@ -69,6 +72,16 @@ public void setupTCWorldgenFlags(World world, int chunkX, int chunkZ) {
}
}

@Override
public boolean shouldHandleAuraGen() {
return auraGen;
}

@Override
public void setHandleAuraGen(boolean allow) {
auraGen = allow;
}

@Override
public boolean shouldHandleCrystalGen() {
return crystalGen;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* AuraControl
* Copyright (c) 2020 TheCodex6824.
*
* This file is part of AuraControl.
*
* AuraControl is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AuraControl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with AuraControl. If not, see <https://www.gnu.org/licenses/>.
*/

package thecodex6824.auracontrol;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;

public class SimpleCapabilityProvider<C> implements ICapabilitySerializable<NBTTagCompound> {

protected C instance;
protected Capability<C> cap;

public SimpleCapabilityProvider(C inst, Capability<C> c) {
cap = c;
instance = inst;
}

@Override
public void deserializeNBT(NBTTagCompound nbt) {
cap.readNBT(instance, null, nbt);
}

@Override
public NBTTagCompound serializeNBT() {
return (NBTTagCompound) cap.writeNBT(instance, null);
}

@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == cap;
}

@Override
@Nullable
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
// in case some mod decides to query caps before they were set up
if (cap != null && capability == cap)
return cap.cast(instance);

return null;
}

}
59 changes: 59 additions & 0 deletions src/main/java/thecodex6824/auracontrol/api/AuraControlAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,93 @@ private AuraControlAPI() {}

private static IInternalMethodHandler handler;

/**
* For internal use only - sets the handler for API methods
* @param newHandler The handler to use
*/
public static void setMethodHandler(IInternalMethodHandler newHandler) {
handler = newHandler;
}

/**
* Returns the set of all biomes that should have an aura and aura features.
* It will always be the allowed biomes regardless of how the user configured
* the list (i.e. regardless of whether a blocklist or allowlist was used).
* @return The set of all biomes that should have an aura generated
*/
public static Set<Biome> getAllowedBiomes() {
return handler.getAllowedBiomes();
}

/**
* Modifies the aura, taking the allowed biome list into account, for the given chunk.
* It may set the chunk vis cap, vis amount, and flux amount, but will not change
* the information in the chunk's OriginalAuraInfo capability.
* @param world The world the chunk is located in
* @param chunkX The chunk X coordinate
* @param chunkZ The chunk Z coordinate
* @see IOriginalAuraInfo
*/
public static void handleAura(World world, int chunkX, int chunkZ) {
handler.handleAura(world, chunkX, chunkZ);
}

/**
* Prepares the static final fields in TC's config class to support disabling
* crystals and Thaumcraft trees in biomes with no aura. This needs to be called
* before Thaumcraft's world generator is called.
* @param world The world the chunk is located in
* @param chunkX The chunk X coordinate
* @param chunkZ The chunk Z coordinate
*/
public static void setupTCWorldgenFlags(World world, int chunkX, int chunkZ) {
handler.setupTCWorldgenFlags(world, chunkX, chunkZ);
}

/**
* Returns if aura gen should be controlled.
* @return If crystal gen should be controlled
*/
public static boolean shouldHandleAuraGen() {
return handler.shouldHandleAuraGen();
}

/**
* Sets if crystal gen should be controlled.
* @param handle If crystal gen should be handled
*/
public static void setHandleAuraGen(boolean handle) {
handler.setHandleAuraGen(handle);
}

/**
* Returns if crystal gen should be controlled.
* @return If crystal gen should be controlled
*/
public static boolean shouldHandleCrystalGen() {
return handler.shouldHandleCrystalGen();
}

/**
* Sets if crystal gen should be controlled.
* @param handle If crystal gen should be handled
*/
public static void setHandleCrystalGen(boolean handle) {
handler.setHandleCrystalGen(handle);
}

/**
* Returns if Thaumcraft tree gen should be controlled.
* @return If tree gen should be controlled
*/
public static boolean shouldHandleTreeGen() {
return handler.shouldHandleTreeGen();
}

/**
* Sets if Thaumcraft tree gen should be controlled.
* @param handle If tree gen should be handled
*/
public static void setHandleTreeGen(boolean handle) {
handler.setHandleTreeGen(handle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;

/**
* Holds the Capability instance for the OriginalAuraInfo
* capability.
* @see IOriginalAuraInfo
*/
public final class CapabilityOriginalAuraInfo {

private CapabilityOriginalAuraInfo() {}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/thecodex6824/auracontrol/api/IOriginalAuraInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,53 @@

package thecodex6824.auracontrol.api;

/**
* Capability that holds the statistics for an aura chunk,
* at the time it was generated. Useful for detecting modifications
* to the aura, or restoring it to its original values. Mods (including AuraControl)
* can, and are expected to, modify this at worldgen time to reflect the final values
* of the generated aura.
*/
public interface IOriginalAuraInfo {

/**
* Returns the base aura level, also known as the vis cap.
* @return The base aura level
*/
public short getBase();

/**
* Sets the base aura level.
* @param newBase The new base aura level
*/
public void setBase(short newBase);

/**
* Returns the amount of vis in the aura, which can be
* different from the vis cap.
* @return The vis level
*/
public float getVis();

/**
* Sets the amount of vis in the aura.
* @param newVis The new vis level
*/
public void setVis(float newVis);

/**
* Returns the amount of flux in the aura. Vanilla Thaumcraft
* barely adds any flux, if any, at worldgen time, but mods
* like Thaumic Augmentation may add some as part of their
* worldgen.
* @return The amount of flux in the aura
*/
public float getFlux();

/**
* Sets the amount of flux in the aura.
* @param newFlux The new flux level
*/
public void setFlux(float newFlux);

}
Loading

0 comments on commit a13b929

Please sign in to comment.