Skip to content

Commit

Permalink
feat: allow use of external texture for i/o faces
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus8448 committed Oct 8, 2024
1 parent 44b91b2 commit b2a6f56
Show file tree
Hide file tree
Showing 63 changed files with 647 additions and 1,027 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ plugins {
id("fabric-loom") version("1.7-SNAPSHOT")
id("org.cadixdev.licenser") version("0.6.1")
id("org.ajoberstar.grgit") version("5.2.2")
id("dev.galacticraft.mojarn") version("0.4.0+10")
id("dev.galacticraft.mojarn") version("0.5.0+13")
}

group = "dev.galacticraft"
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ mod.version=0.7.0

# Minecraft and Fabric Loader
minecraft.version=1.21.1
loader.version=0.15.11
loader.version=0.16.5
yarn.build=3

# Mod Dependencies
badpackets.version=0.8.1
energy.version=4.1.0
fabric.version=0.102.1+1.21.1
fabric.version=0.105.0+1.21.1

# Optional Mod Dependencies
cloth.config.version=15.0.130
modmenu.version=11.0.1
rei.version=16.0.754
cloth.config.version=15.0.140
modmenu.version=11.0.2
rei.version=16.0.777
architectury.version=13.0.6
wthit.version=12.3.0
wthit.version=12.4.2
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=5b9c5eb3f9fc2c94abaea57d90bd78747ca117ddbbf96c859d3741181a12bf2a
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionSha256Sum=31c55713e40233a8303827ceb42ca48a47267a0ad4bab9177123121e71524c26
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable L
super.setPlacedBy(level, pos, state, placer, itemStack);
if (!level.isClientSide && placer instanceof ServerPlayer player) {
if (level.getBlockEntity(pos) instanceof MachineBlockEntity machine) {
machine.getSecurity().tryUpdate(player);
machine.getSecurity().tryUpdate(player.getUUID());
}
}
}
Expand Down Expand Up @@ -227,7 +227,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
if (entity instanceof MachineBlockEntity machine) {
SecuritySettings security = machine.getSecurity();

security.tryUpdate(player);
security.tryUpdate(player.getUUID());
if (security.hasAccess(player)) {
return super.useWithoutItem(state, level, pos, player, hit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand All @@ -51,6 +50,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public abstract class ConfiguredBlockEntity extends BaseBlockEntity implements RenderDataBlockEntity {
private final @NotNull IOConfig configuration;
private final @NotNull SecuritySettings security;
Expand Down Expand Up @@ -324,9 +325,9 @@ public void setPowered(boolean powered) {
*/
private class InternalSecuritySettings extends SecuritySettings {
@Override
public void tryUpdate(@NotNull Player player) {
public void tryUpdate(@NotNull UUID uuid) {
if (this.owner == null) {
this.owner = player.getUUID();
this.owner = uuid;
ConfiguredBlockEntity.this.setChanged();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ public TestFunction createDrainFluidIntoItemTest(int slot, Fluid fluid, int tank
*/
protected Machine createMachine(GameTestHelper helper) {
helper.setBlock(MACHINE_POS, this.block);
return (Machine) helper.getBlockEntity(MACHINE_POS);
return helper.getBlockEntity(MACHINE_POS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public class SecuritySettings implements Serializable<CompoundTag>, DeltaPacketS
/**
* Updates the owner of the linked machine if it is not already set.
*
* @param player the player to try to set as the owner
* @param uuid the player to try to set as the owner
*/
public void tryUpdate(@NotNull Player player) {
public void tryUpdate(@NotNull UUID uuid) {
if (this.owner == null) {
this.owner = player.getUUID();
this.owner = uuid;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

package dev.galacticraft.machinelib.api.menu;

import dev.architectury.utils.value.FloatSupplier;
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
import dev.galacticraft.machinelib.api.util.FloatSupplier;
import dev.galacticraft.machinelib.impl.menu.sync.*;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import it.unimi.dsi.fastutil.floats.FloatConsumer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.machinelib.api.util;

import java.util.function.Supplier;

/**
* A {@link Supplier}, but for shorts
* @see java.util.function.DoubleSupplier
*/
@FunctionalInterface
public interface FloatSupplier extends Supplier<Float> {
float getAsFloat();

@Override
default Float get() {
return this.getAsFloat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,84 +23,57 @@
package dev.galacticraft.machinelib.client.api.model;

import com.google.common.base.Preconditions;
import com.google.gson.JsonObject;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
import com.mojang.serialization.Codec;
import dev.galacticraft.machinelib.client.api.model.sprite.MachineTextureBase;
import dev.galacticraft.machinelib.client.api.model.sprite.SingleTextureProvider;
import dev.galacticraft.machinelib.client.api.model.sprite.TextureProvider;
import dev.galacticraft.machinelib.client.impl.model.MachineBakedModel;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import dev.galacticraft.machinelib.client.impl.model.MachineModelRegistryImpl;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* A registry for {@link MachineBakedModel} sprite providers.
*/
public interface MachineModelRegistry {
String MARKER = "machinelib:generate";
Map<ResourceLocation, SpriteProviderFactory> FACTORIES = new HashMap<>();
public final class MachineModelRegistry {
public static final String MARKER = "machinelib:generate";

/**
* Registers a sprite provider for a block.
*
* @param id The id to register the provider for.
* @param factory The provider to register.
* @param id the id to register the provider for
* @param codec the provider to register
*/
static void register(@NotNull ResourceLocation id, @NotNull SpriteProviderFactory factory) {
public static void register(@NotNull ResourceLocation id, @NotNull Codec<? extends TextureProvider<?>> codec) {
Preconditions.checkNotNull(id);
Preconditions.checkNotNull(factory);
Preconditions.checkNotNull(codec);

FACTORIES.put(id, factory);
MachineModelRegistryImpl.FACTORIES.put(id, codec);
}

public static void registerBase(@NotNull ResourceLocation id, @NotNull MachineTextureBase bundle) {
Preconditions.checkNotNull(id);
Preconditions.checkNotNull(bundle);

MachineModelRegistryImpl.TEXTURE_BASES.put(id, bundle);
}

/**
* {@return the registered provider, or null if none is registered}
*
* @param providerId The provider id to get the provider for.
* @param providerId the provider id to get the provider for
*/
static @Nullable SpriteProviderFactory getProviderFactory(@NotNull ResourceLocation providerId) {
return FACTORIES.get(providerId);
}

static @NotNull SpriteProviderFactory getProviderFactoryOrDefault(@NotNull ResourceLocation providerId) {
return FACTORIES.getOrDefault(providerId, SpriteProviderFactory.DEFAULT);
public static @Nullable Codec<? extends TextureProvider<?>> getProviderFactory(@NotNull ResourceLocation providerId) {
return MachineModelRegistryImpl.FACTORIES.get(providerId);
}

@FunctionalInterface
interface SpriteProviderFactory {
SpriteProviderFactory DEFAULT = new SpriteProviderFactory() {
@Contract(value = "_, _ -> new", pure = true)
@Override
public @NotNull SpriteProvider create(@NotNull JsonObject json, @NotNull Function<Material, TextureAtlasSprite> atlas) {
return new SpriteProvider() {
private final TextureAtlasSprite machineSide = atlas.apply(MachineBakedModel.MACHINE_SIDE);
private final TextureAtlasSprite machine = atlas.apply(MachineBakedModel.MACHINE);

@Override
public @NotNull TextureAtlasSprite getSpritesForState(@Nullable MachineRenderData renderData, @NotNull BlockFace face) {
if (face.side()) return this.machineSide;
return this.machine;
}
};
}
};

SpriteProvider create(@NotNull JsonObject json, @NotNull Function<Material, TextureAtlasSprite> atlas);
}

@FunctionalInterface
interface SpriteProvider {
/**
* @param face The face that is being textured.
* @return The appropriate sprite to render for the given face.
*/
@Contract(pure = true)
@NotNull
TextureAtlasSprite getSpritesForState(@Nullable MachineRenderData data, @NotNull BlockFace face);
/**
* {@return the registered provider, or {@link SingleTextureProvider#MISSING_CODEC} if none is registered}
*
* @param providerId the provider id to get the provider for
*/
public static @NotNull Codec<? extends TextureProvider<?>> getProviderFactoryOrDefault(@NotNull ResourceLocation providerId) {
return MachineModelRegistryImpl.FACTORIES.getOrDefault(providerId, SingleTextureProvider.CODEC);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2021-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.machinelib.client.api.model.sprite;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

public record AxisSpriteProvider(Material front, Material back, Material base, Material side) implements TextureProvider<AxisSpriteProvider.Bound> {
public static final Codec<AxisSpriteProvider> CODEC = RecordCodecBuilder.create(instance -> instance.group(
MATERIAL_CODEC.fieldOf("front").forGetter(AxisSpriteProvider::front),
MATERIAL_CODEC.fieldOf("back").forGetter(AxisSpriteProvider::back),
MATERIAL_CODEC.fieldOf("base").forGetter(AxisSpriteProvider::base),
MATERIAL_CODEC.fieldOf("side").forGetter(AxisSpriteProvider::side)
).apply(instance, AxisSpriteProvider::new));

@Override
public Bound bind(Function<Material, TextureAtlasSprite> atlas) {
return new Bound(atlas.apply(this.front), atlas.apply(this.base), atlas.apply(this.side));
}

public record Bound(TextureAtlasSprite front, TextureAtlasSprite base, TextureAtlasSprite side) implements BoundTextureProvider {
@Override
public TextureAtlasSprite getSprite(@Nullable MachineRenderData renderData, @NotNull BlockFace face) {
if (face == BlockFace.FRONT) return this.front;
if (face.side()) return this.side;
return this.base;
}

@Override
public TextureAtlasSprite getParticle() {
return this.base;
}
}
}
Loading

0 comments on commit b2a6f56

Please sign in to comment.