diff --git a/examples/postInit/draconicevolution.groovy b/examples/postInit/draconicevolution.groovy index 96b64dfe7..b6d5e69c2 100644 --- a/examples/postInit/draconicevolution.groovy +++ b/examples/postInit/draconicevolution.groovy @@ -26,3 +26,5 @@ mods.draconicevolution.fusion.recipeBuilder() mods.draconicevolution.fusion.removeByCatalyst(item('draconicevolution:chaos_shard')) //mods.draconicevolution.fusion.removeAll() + +mods.draconicevolution.energyCore.setOuterBlock(2, 'minecraft:diamond_block') diff --git a/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/DraconicEvolution.java b/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/DraconicEvolution.java index 024d89332..25f78e117 100644 --- a/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/DraconicEvolution.java +++ b/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/DraconicEvolution.java @@ -5,8 +5,10 @@ public class DraconicEvolution extends ModPropertyContainer { public final Fusion fusion = new Fusion(); + public final EnergyCore energyCore = new EnergyCore(); public DraconicEvolution() { addRegistry(fusion); + addRegistry(energyCore); } } diff --git a/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/EnergyCore.java b/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/EnergyCore.java new file mode 100644 index 000000000..ab03550aa --- /dev/null +++ b/src/main/java/com/cleanroommc/groovyscript/compat/mods/draconicevolution/EnergyCore.java @@ -0,0 +1,138 @@ +package com.cleanroommc.groovyscript.compat.mods.draconicevolution; + +import com.brandon3055.brandonscore.lib.MultiBlockStorage; +import com.brandon3055.draconicevolution.world.EnergyCoreStructure; +import com.cleanroommc.groovyscript.api.GroovyBlacklist; +import com.cleanroommc.groovyscript.api.GroovyLog; +import com.cleanroommc.groovyscript.api.IScriptReloadable; +import com.cleanroommc.groovyscript.core.mixin.draconicevolution.EnergyCoreStructureAccessor; +import com.cleanroommc.groovyscript.core.mixin.draconicevolution.MultiBlockStorageAccessor; +import com.cleanroommc.groovyscript.helper.Alias; +import net.minecraft.block.Block; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.common.registry.ForgeRegistries; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Arrays; +import java.util.Collection; + +public class EnergyCore implements IScriptReloadable { + + private static final String DRACONIUM = "draconicevolution:draconium_block"; + private static final String DRACONIC = "draconicevolution:draconic_block"; + private static final String REDSTONE = "minecraft:redstone_block"; + + private int version = 0; + private MultiBlockStorage[] original; + private MultiBlockStorage[] edited; + private String[] inner; + private String[] outer; + + private void init() { + if (this.original != null) return; + EnergyCoreStructure ecs = new EnergyCoreStructure(); + ecs.initialize(null); + this.original = ((EnergyCoreStructureAccessor) ecs).getStructureTiers(); + this.edited = Arrays.copyOf(this.original, this.original.length); + this.inner = new String[this.original.length]; + this.outer = new String[this.original.length]; + onReload(); // increases version to 1 + } + + @Override + public Collection getAliases() { + return Alias.generateOfClass(EnergyCore.class); + } + + @Override + public void onReload() { + if (this.original == null) return; + this.edited = Arrays.copyOf(this.original, this.original.length); + Arrays.fill(this.inner, REDSTONE); + Arrays.fill(this.outer, DRACONIUM); + this.inner[this.inner.length - 1] = DRACONIUM; + this.outer[this.outer.length - 1] = DRACONIC; + this.version++; + } + + @Override + public void afterScriptLoad() {} + + @GroovyBlacklist + public int getVersion() { + return version; + } + + @GroovyBlacklist + @ApiStatus.Internal + public void applyEdit(MultiBlockStorage[] mbs) { + for (int i = 0; i < mbs.length; i++) { + ((MultiBlockStorageAccessor) mbs[i]).setBlockStorage(((MultiBlockStorageAccessor) this.edited[i]).getBlockStorage()); + } + } + + private void replaceBlock(int tier, String edit, boolean inner) { + if (tier < 1 || tier > 8) { + GroovyLog.msg("Error setting block of Draconic Evolution Energy Core") + .add("Tier {} is invalid. Must be between 1 and 8") + .error() + .post(); + return; + } + init(); + String old = inner ? this.inner[tier - 1] : this.outer[tier - 1]; + String[][][] blocks = ((MultiBlockStorageAccessor) this.edited[tier - 1]).getBlockStorage(); + for (int i = 0; i < blocks.length; i++) { + for (int j = 0; j < blocks[i].length; j++) { + for (int k = 0; k < blocks[i][j].length; k++) { + if (old.equals(blocks[i][j][k])) { + blocks[i][j][k] = edit; + } + } + } + } + (inner ? this.inner : this.outer)[tier - 1] = edit; + } + + public EnergyCore setInnerBlock(int tier, String id) { + if (!ForgeRegistries.BLOCKS.containsKey(new ResourceLocation(id))) { + GroovyLog.get().error("Can't set block '{}' as inner block of tier {} of Draconic Evolution Energy Core, because the block doesn't exist!", id, tier); + return this; + } + replaceBlock(tier, id, true); + return this; + } + + public EnergyCore setOuterBlock(int tier, String id) { + if (!ForgeRegistries.BLOCKS.containsKey(new ResourceLocation(id))) { + GroovyLog.get().error("Can't set block '{}' as outer block of tier {} of Draconic Evolution Energy Core, because the block doesn't exist!", id, tier); + return this; + } + replaceBlock(tier, id, false); + return this; + } + + public EnergyCore setInnerBlock(int tier, Block block) { + if (block == null) { + GroovyLog.msg("Error setting inner block of tier {} Draconic Evolution Energy Core", tier) + .add("block must not be null") + .error() + .post(); + return this; + } + replaceBlock(tier, block.getRegistryName().toString(), true); + return this; + } + + public EnergyCore setOuterBlock(int tier, Block block) { + if (block == null) { + GroovyLog.msg("Error setting outer block of tier {} Draconic Evolution Energy Core", tier) + .add("block must not be null") + .error() + .post(); + return this; + } + replaceBlock(tier, block.getRegistryName().toString(), false); + return this; + } +} diff --git a/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/EnergyCoreStructureAccessor.java b/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/EnergyCoreStructureAccessor.java new file mode 100644 index 000000000..aacf5ea4e --- /dev/null +++ b/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/EnergyCoreStructureAccessor.java @@ -0,0 +1,13 @@ +package com.cleanroommc.groovyscript.core.mixin.draconicevolution; + +import com.brandon3055.brandonscore.lib.MultiBlockStorage; +import com.brandon3055.draconicevolution.world.EnergyCoreStructure; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(EnergyCoreStructure.class) +public interface EnergyCoreStructureAccessor { + + @Accessor + MultiBlockStorage[] getStructureTiers(); +} diff --git a/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/EnergyCoreStructureMixin.java b/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/EnergyCoreStructureMixin.java new file mode 100644 index 000000000..427d503ae --- /dev/null +++ b/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/EnergyCoreStructureMixin.java @@ -0,0 +1,61 @@ +package com.cleanroommc.groovyscript.core.mixin.draconicevolution; + +import com.brandon3055.brandonscore.lib.MultiBlockStorage; +import com.brandon3055.draconicevolution.blocks.tileentity.TileEnergyStorageCore; +import com.brandon3055.draconicevolution.utils.LogHelper; +import com.brandon3055.draconicevolution.world.EnergyCoreStructure; +import com.cleanroommc.groovyscript.compat.mods.ModSupport; +import net.minecraft.util.math.BlockPos; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(value = EnergyCoreStructure.class, remap = false) +public abstract class EnergyCoreStructureMixin { + + @Shadow + private MultiBlockStorage[] structureTiers; + @Shadow + private TileEnergyStorageCore core; + + @Shadow + public abstract BlockPos getCoreOffset(int tier); + + @Unique + private int groovyScript$version = 0; + + @Inject(method = "checkTier", at = @At("HEAD"), cancellable = true) + public void checkTier(int tier, CallbackInfoReturnable cir) { + // check if the structure was edited and update + if (groovyScript$version != ModSupport.DRACONIC_EVOLUTION.get().energyCore.getVersion()) { + groovyScript$version = ModSupport.DRACONIC_EVOLUTION.get().energyCore.getVersion(); + ModSupport.DRACONIC_EVOLUTION.get().energyCore.applyEdit(structureTiers); + } + // this part is the same as de + // I just do this because theirs is ugly + if (tier <= 0) { + LogHelper.error("[EnergyCoreStructure] Tier value to small. As far as TileEnergyStorageCore is concerned the tiers now start at 1 not 0. This class automatically handles the conversion now"); + cir.setReturnValue(false); + return; + } + if (tier > 8) { + LogHelper.error("[EnergyCoreStructure#checkTeir] What exactly were you expecting after Tier 8? Infinity.MAX_VALUE?"); + cir.setReturnValue(false); + return; + } + cir.setReturnValue(this.structureTiers[tier - 1].checkStructure(this.core.getWorld(), this.core.getPos().add(getCoreOffset(tier)))); + } + + @Inject(method = "forTier", at = @At("HEAD")) + private void forTier(int tier, int flag, CallbackInfo ci) { + // check if the structure was edited and update + if (groovyScript$version != ModSupport.DRACONIC_EVOLUTION.get().energyCore.getVersion()) { + groovyScript$version = ModSupport.DRACONIC_EVOLUTION.get().energyCore.getVersion(); + ModSupport.DRACONIC_EVOLUTION.get().energyCore.applyEdit(structureTiers); + } + } +} diff --git a/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/MultiBlockStorageAccessor.java b/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/MultiBlockStorageAccessor.java new file mode 100644 index 000000000..d3e163d8d --- /dev/null +++ b/src/main/java/com/cleanroommc/groovyscript/core/mixin/draconicevolution/MultiBlockStorageAccessor.java @@ -0,0 +1,15 @@ +package com.cleanroommc.groovyscript.core.mixin.draconicevolution; + +import com.brandon3055.brandonscore.lib.MultiBlockStorage; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(MultiBlockStorage.class) +public interface MultiBlockStorageAccessor { + + @Accessor + String[][][] getBlockStorage(); + + @Accessor + void setBlockStorage(String[][][] blockStorage); +} diff --git a/src/main/resources/mixin.groovyscript.draconicevolution.json b/src/main/resources/mixin.groovyscript.draconicevolution.json index e6b1e772c..0132fda19 100644 --- a/src/main/resources/mixin.groovyscript.draconicevolution.json +++ b/src/main/resources/mixin.groovyscript.draconicevolution.json @@ -5,6 +5,9 @@ "minVersion": "0.8", "compatibilityLevel": "JAVA_8", "mixins": [ - "FusionRegistryAccessor" + "EnergyCoreStructureAccessor", + "EnergyCoreStructureMixin", + "FusionRegistryAccessor", + "MultiBlockStorageAccessor" ] } \ No newline at end of file