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 1 commit
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -54,23 +55,26 @@ public ChemicalCamoContainer applyCamo(Level level, BlockPos pos, Player player,
return null;
}

ChemicalStack chemical = handler.getChemicalInTank(0);
if (!isValidChemical(chemical.getChemical(), player)) {
return null;
}

if (!player.isCreative() && ConfigView.Server.INSTANCE.shouldConsumeCamoItem()) {
ChemicalStack extracted = handler.extractChemical(0, FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT, Action.SIMULATE);
if (extracted.getAmount() != FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT) {
return null;
for (int tank = 0; tank < handler.getChemicalTanks(); tank++) {
ChemicalStack chemical = handler.getChemicalInTank(tank);
if (!isValidChemical(chemical.getChemical(), player)) {
continue;
}

if (!level.isClientSide()) {
handler.extractChemical(0, FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT, Action.EXECUTE);
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 new ChemicalCamoContainer(chemical.getChemical());
}
return null;
}

@Override
Expand All @@ -85,13 +89,30 @@ public boolean removeCamo(Level level, BlockPos pos, Player player, ItemStack st
}

ChemicalStack chemical = camo.getChemical().getStack(FramedBlocksIntegration.Constants.CHEMICAL_AMOUNT);
if (handler.insertChemical(chemical, Action.SIMULATE).isEmpty()) {
if (!level.isClientSide() && !player.isCreative() && ConfigView.Server.INSTANCE.shouldConsumeCamoItem()) {
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;
}
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;
}

Expand All @@ -114,11 +135,11 @@ private static boolean isValidChemical(Chemical chemical, @Nullable Player playe
if (chemical.isEmptyType()) {
return false;
}
if (chemical.isRadioactive()) {
if (chemical.hasAttributesWithValidation()) {
displayValidationMessage(player, MSG_RADIOACTIVE, CamoMessageVerbosity.DEFAULT);
return false;
}
if (chemical.is(FramedBlocksIntegration.Constants.CHEMICAL_BLACKLISTED)) {
if (chemical.is(MekanismAPITags.Chemicals.FRAMEDBLOCKS_BLACKLISTED)) {
displayValidationMessage(player, MSG_BLACKLISTED, CamoMessageVerbosity.DEFAULT);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mekanism.common.integration.framedblocks;

import mekanism.api.chemical.Chemical;
import mekanism.common.registration.impl.FluidDeferredRegister;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleOptions;
Expand All @@ -23,9 +24,11 @@
final class ChemicalCamoContent extends CamoContent<ChemicalCamoContent> {

private final Chemical chemical;
private final MapColor mapColor;

ChemicalCamoContent(Chemical chemical) {
this.chemical = chemical;
this.mapColor = FluidDeferredRegister.getClosestColor(chemical.getColorRepresentation());
}

Chemical getChemical() {
Expand Down Expand Up @@ -75,7 +78,7 @@ public boolean isEmissive() {

@Override
public SoundType getSoundType() {
return SoundType.STONE;
return SoundType.WET_GRASS;
}

@Override
Expand All @@ -101,8 +104,7 @@ public boolean canEntityDestroy(BlockGetter level, BlockPos pos, Entity entity)
@Override
@Nullable
public MapColor getMapColor(BlockGetter level, BlockPos pos) {
// TODO: Chemicals don't provide a map color
return null;
return mapColor;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mekanism.common.integration.framedblocks;

import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -51,7 +52,7 @@ public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction

@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, RandomSource rand, ModelData extraData, RenderType layer) {
return (side == null || layer != renderType) ? List.of() : quads.get(side);
return (side == null || layer != renderType) ? Collections.emptyList() : quads.get(side);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mekanism.common.integration.framedblocks;

import mekanism.api.chemical.Chemical;
import mekanism.client.render.MekanismRenderer;
import mekanism.common.util.WorldUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
Expand Down Expand Up @@ -29,9 +31,9 @@ final class ChemicalSpriteParticle extends TextureSheetParticle {
this.brightness = 0;

int tint = chemical.getTint();
this.rCol = .6F * (float)(tint >> 16 & 0xFF) / 255F;
this.gCol = .6F * (float)(tint >> 8 & 0xFF) / 255F;
this.bCol = .6F * (float)(tint & 0xFF) / 255F;
this.rCol = .6F * MekanismRenderer.getRed(tint);
this.gCol = .6F * MekanismRenderer.getGreen(tint);
this.bCol = .6F * MekanismRenderer.getBlue(tint);

setSprite(Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(chemical.getIcon()));
}
Expand Down Expand Up @@ -62,9 +64,8 @@ protected float getV1() {
}

@Override
@SuppressWarnings("deprecation")
public int getLightColor(float partialTick) {
int light = level.hasChunkAt(pos) ? LevelRenderer.getLightColor(level, pos) : 0;
int light = WorldUtils.isChunkLoaded(level, pos) ? LevelRenderer.getLightColor(level, pos) : 0;
int block = Math.max(brightness, LightTexture.block(light));
return LightTexture.pack(block, LightTexture.sky(light));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package mekanism.common.integration.framedblocks;

import mekanism.api.MekanismAPI;
import mekanism.api.chemical.Chemical;
import mekanism.common.Mekanism;
import mekanism.common.registration.impl.ParticleTypeDeferredRegister;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.client.event.ModelEvent;
Expand All @@ -23,9 +20,7 @@ public final class FramedBlocksIntegration {
FramedConstants.CAMO_CONTAINER_FACTORY_REGISTRY_KEY,
Mekanism.MODID
);
private static final DeferredRegister<ParticleType<?>> PARTICLE_TYPES = DeferredRegister.create(
Registries.PARTICLE_TYPE, Mekanism.MODID
);
private static final ParticleTypeDeferredRegister PARTICLE_TYPES = new ParticleTypeDeferredRegister(Mekanism.MODID);

static final DeferredHolder<CamoContainerFactory<?>, ChemicalCamoContainerFactory> CHEMICAL_FACTORY =
CAMO_FACTORIES.register("chemical", ChemicalCamoContainerFactory::new);
Expand All @@ -43,12 +38,6 @@ public static void init(IEventBus modBus) {

public static final class Constants {

/**
* Chemicals tagged with this tag cannot be inserted into a framed blocks
*/
public static final TagKey<Chemical> CHEMICAL_BLACKLISTED = TagKey.create(
MekanismAPI.CHEMICAL_REGISTRY_NAME, Mekanism.rl("framedblocks_blacklisted")
);
/**
* The amount of a given chemical to consume when applying it to a framed block
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public <BUCKET extends BucketItem> FluidRegistryObject<MekanismFluidType, Source
return new FluidRegistryObject<>(fluidType, stillFluid, flowingFluid, bucket, block);
}

private static MapColor getClosestColor(int tint) {
public static MapColor getClosestColor(int tint) {
if (tint == 0xFFFFFFFF) {
return MapColor.NONE;
}
Expand Down