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

[1.21.x] Equipment Integration Abstraction and Accessories Integration #336

Open
wants to merge 2 commits into
base: 1.21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ allprojects {
includeGroup "cpw.mods"
}
}
maven { url 'https://maven.wispforest.io/releases' }
maven { url 'https://maven.su5ed.dev/releases' }
}

tasks.withType(JavaCompile) {
Expand Down
8 changes: 8 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ dependencies {
modCompileOnly "be.florens:expandability-fabric:${rootProject.expandability_version}"

compileOnly "com.electronwill.night-config:toml:${rootProject.night_config_version}"

modCompileOnly("io.wispforest:accessories-common:${project.accessories_version}"){
exclude(group: "net.fabricmc.fabric-api")
}

modCompileOnly("dev.emi:trinkets:${rootProject.trinkets_version}"){
exclude(group: "net.fabricmc.fabric-api")
}
}
13 changes: 13 additions & 0 deletions common/src/main/java/artifacts/Artifacts.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import artifacts.config.ModConfig;
import artifacts.entity.MimicEntity;
import artifacts.event.ArtifactEvents;
import artifacts.integration.EquipmentIntegrationUtils;
import artifacts.integration.VanillaEquipmentIntegration;
import artifacts.network.NetworkHandler;
import artifacts.platform.PlatformServices;
import artifacts.registry.*;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.PlayerEvent;
Expand Down Expand Up @@ -38,6 +41,8 @@ public static <T> ResourceKey<T> key(ResourceKey<? extends Registry<T>> registry
public static void init() {
CONFIG = new ModConfig();

setupIntegrations();

NetworkHandler.register();

ModDataComponents.register();
Expand All @@ -64,4 +69,12 @@ public static void init() {
public static void setupConfigs() {
CONFIG.setup();
}

public static void setupIntegrations() {
PlatformServices.platformHelper.setupIntegrations();

VanillaEquipmentIntegration.INSTANCE.registerIntegration();

EquipmentIntegrationUtils.setupIntegrations();
}
}
4 changes: 4 additions & 0 deletions common/src/main/java/artifacts/ArtifactsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import artifacts.client.mimic.MimicRenderer;
import artifacts.client.mimic.model.MimicChestLayerModel;
import artifacts.client.mimic.model.MimicModel;
import artifacts.platform.PlatformHelper;
import artifacts.platform.PlatformServices;
import artifacts.registry.ModEntityTypes;
import artifacts.registry.ModItems;
import artifacts.registry.ModKeyMappings;
Expand All @@ -24,6 +26,8 @@ public static void init() {
registerLayerDefinitions();
registerRenderers();
ClientLifecycleEvent.CLIENT_STARTED.register(clientState -> onClientStarted());

PlatformServices.platformHelper.setupClientIntegratons();
}

public static void onClientStarted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import artifacts.config.value.Value;
import artifacts.config.value.ValueTypes;
import artifacts.integration.EquipmentIntegrationUtils;
import artifacts.platform.PlatformServices;
import artifacts.registry.ModAbilities;
import artifacts.util.AbilityHelper;
Expand Down Expand Up @@ -54,8 +55,7 @@ public static ItemStack findTotem(LivingEntity entity) {
}
}

return PlatformServices.platformHelper
.findAllEquippedBy(entity, stack -> AbilityHelper.hasAbility(ModAbilities.TELEPORT_ON_DEATH.value(), stack)
return EquipmentIntegrationUtils.findAllEquippedBy(entity, stack -> AbilityHelper.hasAbility(ModAbilities.TELEPORT_ON_DEATH.value(), stack)
&& !(entity instanceof Player player && player.getCooldowns().isOnCooldown(stack.getItem())))
.findFirst()
.orElse(ItemStack.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package artifacts.fabric.client;
package artifacts.client;

import artifacts.fabric.registry.ModDataComponentsFabric;
import artifacts.item.ArtifactItem;
import artifacts.registry.ModDataComponents;
import net.minecraft.world.item.ItemStack;

public class CosmeticsHelper {

public static boolean areCosmeticsToggledOffByPlayer(ItemStack stack) {
Boolean enabled = stack.get(ModDataComponentsFabric.COSMETICS_ENABLED.get());
Boolean enabled = stack.get(ModDataComponents.COSMETICS_ENABLED.get());
return enabled != null && !enabled && !isCosmeticOnly(stack);
}

public static void toggleCosmetics(ItemStack stack) {
if (!isCosmeticOnly(stack)) {
stack.set(ModDataComponentsFabric.COSMETICS_ENABLED.get(), areCosmeticsToggledOffByPlayer(stack));
stack.set(ModDataComponents.COSMETICS_ENABLED.get(), areCosmeticsToggledOffByPlayer(stack));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import artifacts.client.item.model.*;
import artifacts.client.item.renderer.*;
import artifacts.integration.client.ClientEquipmentIntegrationUtils;
import artifacts.platform.PlatformServices;
import artifacts.registry.ModItems;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -78,6 +79,6 @@ public static ModelPart bakeLayer(ModelLayerLocation layerLocation) {
}

public static void register(Item item, Supplier<ArtifactRenderer> rendererSupplier) {
PlatformServices.platformHelper.registerArtifactRenderer(item, rendererSupplier);
ClientEquipmentIntegrationUtils.registerArtifactRenderer(item, rendererSupplier);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package artifacts.client.item.renderer;

import artifacts.client.item.model.ArmsModel;
import artifacts.integration.client.ClientEquipmentIntegrationUtils;
import artifacts.platform.PlatformServices;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
Expand Down Expand Up @@ -45,7 +46,7 @@ public GloveArtifactRenderer(ResourceLocation wideTexture, ResourceLocation slim

@Nullable
public static GloveArtifactRenderer getGloveRenderer(ItemStack stack) {
if (!stack.isEmpty() && PlatformServices.platformHelper.getArtifactRenderer(stack.getItem()) instanceof GloveArtifactRenderer gloveRenderer) {
if (!stack.isEmpty() && ClientEquipmentIntegrationUtils.getArtifactRenderer(stack.getItem()) instanceof GloveArtifactRenderer gloveRenderer) {
return gloveRenderer;
}
return null;
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/java/artifacts/event/ArtifactEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import artifacts.ability.mobeffect.AttacksInflictMobEffectAbility;
import artifacts.ability.retaliation.RetaliationAbility;
import artifacts.attribute.DynamicAttributeModifier;
import artifacts.integration.EquipmentIntegrationUtils;
import artifacts.item.UmbrellaItem;
import artifacts.mixin.accessors.MobAccessor;
import artifacts.platform.PlatformServices;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static void onItemTick(LivingEntity entity) {
if (entity.level().isClientSide()) {
return;
}
PlatformServices.platformHelper.findAllEquippedBy(entity, stack -> stack.has(ModDataComponents.ABILITIES.value()))
EquipmentIntegrationUtils.findAllEquippedBy(entity, stack -> stack.has(ModDataComponents.ABILITIES.value()))
.forEach(stack -> {
for (ArtifactAbility ability : AbilityHelper.getAbilities(stack)) {
boolean isActive = ability.isActive(entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package artifacts.integration;

import artifacts.item.WearableArtifactItem;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

public abstract class BaseEquipmentIntegration {

protected BaseEquipmentIntegration() {}

public final void registerIntegration() {
EquipmentIntegrationUtils.registerIntegration(this);
}

//--

public abstract void setup();

public void processWearableArtifactBuilder(WearableArtifactItem.Builder builder) { /* NO-OP */ }

public void addCosmeticToggleTooltip(List<MutableComponent> tooltip, ItemStack stack) { /* NO-OP */ }

//--

public abstract Stream<ItemStack> findAllEquippedBy(LivingEntity entity, Predicate<ItemStack> predicate);

public abstract void iterateEquippedAccessories(LivingEntity entity, Consumer<ItemStack> consumer);

public abstract <T> T reduceAccessories(LivingEntity entity, T init, BiFunction<ItemStack, T, T> f);

public abstract boolean equipAccessory(LivingEntity entity, ItemStack stack);

public abstract boolean isVisibleOnHand(LivingEntity entity, InteractionHand hand, Item item);

public abstract String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package artifacts.integration;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class EquipmentIntegrationUtils {

private static final Map<String, BaseEquipmentIntegration> INTEGRATIONS = new LinkedHashMap<>();

public static void setupIntegrations() {
INTEGRATIONS.values().forEach(BaseEquipmentIntegration::setup);
}

public static void registerIntegration(BaseEquipmentIntegration integration) {
var name = integration.name();

if (INTEGRATIONS.containsKey(name)) throw new IllegalStateException("Duplicate Equipment Integration detected! [Name: " + name + "]");

INTEGRATIONS.put(name, integration);
}

public static Stream<ItemStack> findAllEquippedBy(LivingEntity entity, Predicate<ItemStack> predicate) {
var allEquippedStacks = Stream.<ItemStack>of();

for (var integration : INTEGRATIONS.values()) {
allEquippedStacks = Stream.concat(allEquippedStacks, integration.findAllEquippedBy(entity, predicate));
}

return allEquippedStacks;
}

public static void iterateEquippedAccessories(LivingEntity entity, Consumer<ItemStack> consumer) {
for (var integration : INTEGRATIONS.values()) {
integration.iterateEquippedAccessories(entity, consumer);
}
}

public static <T> T reduceAccessories(LivingEntity entity, T init, BiFunction<ItemStack, T, T> f) {
for (var integration : INTEGRATIONS.values()) {
init = integration.reduceAccessories(entity, init, f);
}

return init;
}

public static boolean equipAccessory(LivingEntity entity, ItemStack stack) {
for (var integration : INTEGRATIONS.values()) {
if (integration.equipAccessory(entity, stack)) return true;
}

return false;
}

public static boolean isVisibleOnHand(LivingEntity entity, InteractionHand hand, Item item) {
for (var integration : INTEGRATIONS.values()) {
if (integration.isVisibleOnHand(entity, hand, item)) return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package artifacts.integration;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class VanillaEquipmentIntegration extends BaseEquipmentIntegration {

public static final VanillaEquipmentIntegration INSTANCE = new VanillaEquipmentIntegration();

@Override
public void setup() {}

@Override
public Stream<ItemStack> findAllEquippedBy(LivingEntity entity, Predicate<ItemStack> predicate) {
return StreamSupport.stream(entity.getArmorAndBodyArmorSlots().spliterator(), false).filter(predicate);
}

@Override
public void iterateEquippedAccessories(LivingEntity entity, Consumer<ItemStack> consumer) {
for (ItemStack item : entity.getArmorAndBodyArmorSlots()) {
if (!item.isEmpty()) consumer.accept(item);
}
}

@Override
public <T> T reduceAccessories(LivingEntity entity, T init, BiFunction<ItemStack, T, T> f) {
for (ItemStack item : entity.getArmorAndBodyArmorSlots()) {
if (!item.isEmpty()) init = f.apply(item, init);
}
return init;
}

@Override
public boolean equipAccessory(LivingEntity entity, ItemStack stack) {
return false;
}

@Override
public boolean isVisibleOnHand(LivingEntity entity, InteractionHand hand, Item item) {
return false;
}

@Override
public String name() {
return "vanilla";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package artifacts.integration.client;

import artifacts.client.item.renderer.ArtifactRenderer;
import net.minecraft.world.item.Item;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

public abstract class BaseClientEquipmentIntegration {

protected BaseClientEquipmentIntegration() {}

public final void registerIntegration() {
ClientEquipmentIntegrationUtils.registerIntegration(this);
}

public abstract void registerArtifactRenderer(Item item, Supplier<ArtifactRenderer> rendererSupplier);

@Nullable
public abstract ArtifactRenderer getArtifactRenderer(Item item);

public abstract String name();
}
Loading