Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for chemicals as camos on framed blocks #8209

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ dependencies {
compileOnly("cc.tweaked:cc-tweaked-${previous_minor_minecraft_version}-forge-api:${cc_tweaked_version}")
//localRuntime("cc.tweaked:cc-tweaked-${minecraft_version}-forge:${cc_tweaked_version}")
compileOnly("curse.maven:female-gender-forge-481655:${wildfire_gender_mod_id}")
compileOnly("curse.maven:framedblocks-441647:${framedblocks_mod_id}")

//Dependencies for data generators for mod compat reference
datagenMainImplementation("appeng:appliedenergistics2:${ae2_version}")
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ json_things_version=0.12.1
top_version=1.21_neo-12.0.3-5
wildfire_gender_mod_id=5579007
wthit_version=12.3.0
framedblocks_mod_id=5629510

#Mod dependency min version ranges

Expand Down
4 changes: 4 additions & 0 deletions src/api/java/mekanism/api/MekanismAPITags.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ private Chemicals() {
* Represents all clean slurries.
*/
public static final TagKey<Chemical> CLEAN = tag("clean");
/**
* Chemicals in this tag cannot be inserted into framed blocks
*/
XFactHD marked this conversation as resolved.
Show resolved Hide resolved
public static final TagKey<Chemical> FRAMEDBLOCKS_BLACKLISTED = tag("framedblocks_blacklisted");

private static TagKey<Chemical> tag(String name) {
return TagKey.create(MekanismAPI.CHEMICAL_REGISTRY_NAME, rl(name));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ private void addTags() {

addTag(MekanismAPITags.Chemicals.DIRTY, "Dirty Slurry");
addTag(MekanismAPITags.Chemicals.CLEAN, "Clean Slurry");

add(MekanismAPITags.Chemicals.FRAMEDBLOCKS_BLACKLISTED, "FramedBlocks Blacklisted");
}

private void addItems() {
Expand Down Expand Up @@ -1722,6 +1724,8 @@ private void addMisc() {
add(MekanismModules.MAGNETIC_ATTRACTION_UNIT, "Magnetic Attraction Unit", "Uses powerful magnets to draw distant items towards the player. Install multiple for a greater range.");
add(MekanismModules.FROST_WALKER_UNIT, "Frost Walker Unit", "Uses liquid hydrogen to freeze any water the player walks on. Install multiple for a greater range.");
add(MekanismModules.SOUL_SURFER_UNIT, "Soul Surfer Unit", "Allows the user to surf effortlessly across the top of souls. Install multiple for a greater speed.");
// FramedBlocks integration
add(MekanismLang.FRAMEDBLOCKS_CAMO_RADIOACTIVE, "Radioactive chemicals cannot be inserted into framed blocks!");
}

private void addOre(OreType type, String description) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mekanism/common/MekanismLang.java
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ public enum MekanismLang implements ILangEntry {
MODULE_MAGNETIC_ATTRACTION("module", "magnetic_attraction"),
MODULE_MODE_CHANGE("module", "mode_change"),
MODULE_VISION_ENHANCEMENT("module", "vision_enhancement"),
//FramedBlocks integration
FRAMEDBLOCKS_CAMO_RADIOACTIVE("msg", "compat.framedblocks.camo.radioactive")
;

private final String key;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mekanism/common/integration/MekanismHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import mekanism.common.integration.crafttweaker.content.CrTContentUtils;
import mekanism.common.integration.curios.CuriosIntegration;
import mekanism.common.integration.energy.EnergyCompatUtils;
import mekanism.common.integration.framedblocks.FramedBlocksIntegration;
import mekanism.common.integration.jsonthings.JsonThingsIntegration;
import mekanism.common.integration.lookingat.theoneprobe.TOPProvider;
import mekanism.common.integration.projecte.MekanismNormalizedSimpleStacks;
Expand Down Expand Up @@ -43,6 +44,7 @@ public final class MekanismHooks {
public static final String RECIPE_STAGES_MOD_ID = "recipestages";
public static final String TOP_MOD_ID = "theoneprobe";
public static final String WILDFIRE_GENDER_MOD_ID = "wildfire_gender";
public static final String FRAMEDBLOCKS_MOD_ID = "framedblocks";

public final boolean CCLoaded;
public final boolean CraftTweakerLoaded;
Expand All @@ -58,6 +60,7 @@ public final class MekanismHooks {
public final boolean RecipeStagesLoaded;
public final boolean TOPLoaded;
public final boolean WildfireGenderModLoaded;
public final boolean FramedBlocksLoaded;

public MekanismHooks() {
ModList modList = ModList.get();
Expand All @@ -77,6 +80,7 @@ public MekanismHooks() {
RecipeStagesLoaded = loadedCheck.test(RECIPE_STAGES_MOD_ID);
TOPLoaded = loadedCheck.test(TOP_MOD_ID);
WildfireGenderModLoaded = loadedCheck.test(WILDFIRE_GENDER_MOD_ID);
FramedBlocksLoaded = loadedCheck.test(FRAMEDBLOCKS_MOD_ID);
}

public void hookConstructor(final IEventBus bus) {
Expand All @@ -100,6 +104,9 @@ public void hookConstructor(final IEventBus bus) {
if (ProjectELoaded) {
MekanismNormalizedSimpleStacks.NSS_SERIALIZERS.register(bus);
}
if (FramedBlocksLoaded) {
FramedBlocksIntegration.init(bus);
}
}

public void hookCapabilityRegistration() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mekanism.common.integration.framedblocks;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import mekanism.api.chemical.Chemical;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.util.RandomSource;
import net.neoforged.neoforge.client.ChunkRenderTypeSet;
import net.neoforged.neoforge.client.model.data.ModelData;
import xfacthd.framedblocks.api.camo.CamoClientHandler;
import xfacthd.framedblocks.api.model.util.ModelUtils;

final class ChemicalCamoClientHandler extends CamoClientHandler<ChemicalCamoContent> {

static final CamoClientHandler<ChemicalCamoContent> INSTANCE = new ChemicalCamoClientHandler();
private static final Map<Chemical, BakedModel> CHEMICAL_MODEL_CACHE = new ConcurrentHashMap<>();

private ChemicalCamoClientHandler() { }

@Override
public ChunkRenderTypeSet getRenderTypes(ChemicalCamoContent camo, RandomSource random, ModelData data) {
return ModelUtils.TRANSLUCENT;
}

@Override
public BakedModel getOrCreateModel(ChemicalCamoContent camo) {
return CHEMICAL_MODEL_CACHE.computeIfAbsent(camo.getChemical(), ChemicalModel::create);
}

@Override
public Particle makeHitDestroyParticle(ClientLevel level, double x, double y, double z, double sx, double sy, double sz, ChemicalCamoContent camo, BlockPos pos) {
return new ChemicalSpriteParticle(level, x, y, z, sx, sy, sz, camo.getChemical());
}

static void clearModelCache() {
CHEMICAL_MODEL_CACHE.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mekanism.common.integration.framedblocks;

import mekanism.api.chemical.Chemical;
import org.jetbrains.annotations.Nullable;
import xfacthd.framedblocks.api.camo.CamoContainer;
import xfacthd.framedblocks.api.camo.CamoContainerFactory;

final class ChemicalCamoContainer extends CamoContainer<ChemicalCamoContent, ChemicalCamoContainer> {

ChemicalCamoContainer(Chemical chemical) {
super(new ChemicalCamoContent(chemical));
}

Chemical getChemical() {
return content.getChemical();
}

@Override
public boolean canRotateCamo() {
return false;
}

@Override
@Nullable
public ChemicalCamoContainer rotateCamo() {
return null;
}

@Override
public int hashCode() {
return content.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != ChemicalCamoContainer.class) return false;
return content.equals(((ChemicalCamoContainer) obj).content);
}

@Override
public String toString() {
return "ChemicalCamoContainer{" + content + "}";
}

@Override
public CamoContainerFactory<ChemicalCamoContainer> getFactory() {
return FramedBlocksIntegration.CHEMICAL_FACTORY.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package mekanism.common.integration.framedblocks;

import com.mojang.serialization.MapCodec;
import mekanism.api.Action;
import mekanism.api.MekanismAPI;
import mekanism.api.MekanismAPITags;
import mekanism.api.chemical.Chemical;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.chemical.IChemicalHandler;
import mekanism.api.text.TextComponentUtil;
import mekanism.common.MekanismLang;
import mekanism.common.capabilities.Capabilities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import xfacthd.framedblocks.api.camo.CamoContainerFactory;
import xfacthd.framedblocks.api.camo.TriggerRegistrar;
import xfacthd.framedblocks.api.util.CamoMessageVerbosity;
import xfacthd.framedblocks.api.util.ConfigView;

final class ChemicalCamoContainerFactory extends CamoContainerFactory<ChemicalCamoContainer> {

private static final MapCodec<ChemicalCamoContainer> CODEC = Chemical.CODEC
.xmap(ChemicalCamoContainer::new, ChemicalCamoContainer::getChemical)
.fieldOf("chemical");
private static final StreamCodec<RegistryFriendlyByteBuf, ChemicalCamoContainer> STREAM_CODEC =
Chemical.STREAM_CODEC.map(ChemicalCamoContainer::new, ChemicalCamoContainer::getChemical);
private static final Component MSG_RADIOACTIVE = TextComponentUtil.translate(
MekanismLang.FRAMEDBLOCKS_CAMO_RADIOACTIVE.getTranslationKey()
);

@Override
protected void writeToNetwork(CompoundTag tag, ChemicalCamoContainer camo) {
Chemical chemical = camo.getChemical();
tag.putInt("chemical", MekanismAPI.CHEMICAL_REGISTRY.getId(chemical));
}

@Override
protected ChemicalCamoContainer readFromNetwork(CompoundTag tag) {
Chemical chemical = MekanismAPI.CHEMICAL_REGISTRY.byId(tag.getInt("chemical"));
return new ChemicalCamoContainer(chemical);
}

@Override
@Nullable
public ChemicalCamoContainer applyCamo(Level level, BlockPos pos, Player player, ItemStack stack) {
IChemicalHandler handler = Capabilities.CHEMICAL.getCapability(stack);
if (handler == null || handler.getChemicalTanks() <= 0) {
return null;
}

for (int tank = 0; tank < handler.getChemicalTanks(); tank++) {
ChemicalStack chemical = handler.getChemicalInTank(tank);
if (!isValidChemical(chemical.getChemical(), player)) {
continue;
}

if (!player.isCreative() && ConfigView.Server.INSTANCE.shouldConsumeCamoItem()) {
ChemicalStack extracted = handler.extractChemical(tank, FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT, Action.SIMULATE);
if (extracted.getAmount() != FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT) {
continue;
}

if (!level.isClientSide()) {
handler.extractChemical(tank, FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT, Action.EXECUTE);
}
}

return new ChemicalCamoContainer(chemical.getChemical());
}
return null;
}

@Override
public boolean removeCamo(Level level, BlockPos pos, Player player, ItemStack stack, ChemicalCamoContainer camo) {
if (stack.isEmpty()) {
return false;
}

IChemicalHandler handler = Capabilities.CHEMICAL.getCapability(stack);
if (handler == null || handler.getChemicalTanks() <= 0) {
return false;
}

ChemicalStack chemical = camo.getChemical().getStack(FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT);
if (!isValidForHandler(handler, chemical)) {
return false;
}
if (!player.isCreative() && ConfigView.Server.INSTANCE.shouldConsumeCamoItem()) {
if (!handler.insertChemical(chemical, Action.SIMULATE).isEmpty()) {
return false;
}
if (!level.isClientSide()) {
handler.insertChemical(chemical, Action.EXECUTE);
}
}
return true;
}

private static boolean isValidForHandler(IChemicalHandler handler, ChemicalStack chemical) {
for (int tank = 0; tank < handler.getChemicalTanks(); tank++) {
if (!handler.isValid(tank, chemical)) {
continue;
}
ChemicalStack inTank = handler.getChemicalInTank(tank);
if (inTank.isEmpty() || inTank.is(chemical.getChemical())) {
return true;
}
}
return false;
}

@Override
public boolean canTriviallyConvertToItemStack() {
return false;
}

@Override
public ItemStack dropCamo(ChemicalCamoContainer camo) {
return ItemStack.EMPTY;
}

@Override
public boolean validateCamo(ChemicalCamoContainer camo) {
return isValidChemical(camo.getChemical(), null);
}

private static boolean isValidChemical(Chemical chemical, @Nullable Player player) {
if (chemical.isEmptyType()) {
return false;
}
if (chemical.hasAttributesWithValidation()) {
displayValidationMessage(player, MSG_RADIOACTIVE, CamoMessageVerbosity.DEFAULT);
return false;
}
if (chemical.is(MekanismAPITags.Chemicals.FRAMEDBLOCKS_BLACKLISTED)) {
displayValidationMessage(player, MSG_BLACKLISTED, CamoMessageVerbosity.DEFAULT);
return false;
}
return true;
}

@Override
public MapCodec<ChemicalCamoContainer> codec() {
return CODEC;
}

@Override
public StreamCodec<? super RegistryFriendlyByteBuf, ChemicalCamoContainer> streamCodec() {
return STREAM_CODEC;
}

@Override
public void registerTriggerItems(TriggerRegistrar registrar) {
registrar.registerApplicationPredicate(Capabilities.CHEMICAL::hasCapability);
registrar.registerRemovalPredicate(Capabilities.CHEMICAL::hasCapability);
}
}
Loading