Skip to content

Commit

Permalink
Port other common stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
HyCraftHD committed Jul 31, 2024
1 parent 3aaacea commit d183c72
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

public class BackpackItem extends UItem implements AutoPickupBackpack {

public static final int DEFAULT_COLOR = 0x816040;

private final BackpackType backpack;

public BackpackItem(BackpackType backpack) {
Expand Down Expand Up @@ -71,11 +73,6 @@ public BackpackType getBackpack() {
return backpack;
}

// Default backpack color if not present
public int getDefaultColor() {
return 0x816040;
}

@Override
public boolean shouldPlayUpdateAnimation(ItemStack oldStack, ItemStack newStack) {
return !ItemStack.isSameItem(oldStack, newStack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package info.u_team.useful_backpacks.integration.jei;

import info.u_team.u_team_core.api.dye.DyeableItem;
import info.u_team.useful_backpacks.UsefulBackpacksReference;
import info.u_team.useful_backpacks.init.UsefulBackpacksItems;
import info.u_team.useful_backpacks.integration.jei.extension.BackpackCraftingRecipeCategoryExtension;
Expand All @@ -12,8 +11,10 @@
import mezz.jei.api.ingredients.subtypes.UidContext;
import mezz.jei.api.registration.ISubtypeRegistration;
import mezz.jei.api.registration.IVanillaCategoryExtensionRegistration;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.DyedItemColor;

@JeiPlugin
public class UsefulBackpacksJeiPlugin implements IModPlugin {
Expand All @@ -29,8 +30,9 @@ public ResourceLocation getPluginUid() {
public void registerItemSubtypes(ISubtypeRegistration registration) {
final IIngredientSubtypeInterpreter<ItemStack> interpreter = (stack, context) -> {
if (context == UidContext.Ingredient) {
if (stack.hasTag() && stack.getItem() instanceof final DyeableItem item) {
return Integer.toString(item.getColor(stack));
final DyedItemColor color = stack.get(DataComponents.DYED_COLOR);
if (color != null) {
return Integer.toString(color.rgb());
}
}
return IIngredientSubtypeInterpreter.NONE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import com.google.common.collect.Lists;

import info.u_team.u_team_core.api.dye.DyeableItem;
import info.u_team.u_team_core.util.ColorUtil;
import info.u_team.u_team_core.util.RGB;
import info.u_team.useful_backpacks.item.BackpackItem;
import info.u_team.useful_backpacks.recipe.BackpackCraftingRecipe;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
Expand All @@ -19,9 +19,12 @@
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.category.extensions.vanilla.crafting.ICraftingCategoryExtension;
import net.minecraft.client.Minecraft;
import net.minecraft.core.component.DataComponents;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.DyedItemColor;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -49,21 +52,22 @@ public void setRecipe(RecipeHolder<BackpackCraftingRecipe> holder, IRecipeLayout
focuses.getItemStackFocuses(RecipeIngredientRole.OUTPUT) //
.map(focus -> focus.getTypedValue().getIngredient()) //
.findFirst().ifPresent(outputStack -> {
if (outputStack.getItem() instanceof DyeableItem dyeable) {
final int dyeableColor = dyeable.getColor(outputStack);
final DyedItemColor dyedColor = outputStack.get(DataComponents.DYED_COLOR);
if (dyedColor != null) {
final int dyeableColor = dyedColor.rgb();
final DyeColor color = ColorUtil.findClosestDyeColor(new RGB(dyeableColor));
final Block wool = ColorUtil.getWoolFromColor(color);
for (int index = 0; index < inputs.size(); index++) {
final List<ItemStack> list = inputs.get(index);
if (list.stream().allMatch(stack -> stack.is(ItemTags.WOOL))) {
if (dyeableColor != dyeable.getDefaultColor()) {
if (dyeableColor != BackpackItem.DEFAULT_COLOR) {
inputs.set(index, List.of(new ItemStack(wool)));
} else {
inputs.set(index, List.of(new ItemStack(Blocks.WHITE_WOOL)));
}
}
}
outputs.set(0, DyeableItem.colorStack(outputs.get(0), List.of(color)));
outputs.set(0, DyedItemColor.applyDyes(outputs.get(0), List.of(DyeItem.byColor(color))));
changed.set(true);
}
});
Expand All @@ -74,7 +78,7 @@ public void setRecipe(RecipeHolder<BackpackCraftingRecipe> holder, IRecipeLayout
if (inputStack.is(ItemTags.WOOL)) {
final DyeColor color = ColorUtil.getColorFromWool(inputStack.getItem());
if (color != null && color != DyeColor.WHITE) {
outputs.set(0, DyeableItem.colorStack(outputs.get(0), List.of(color)));
outputs.set(0, DyedItemColor.applyDyes(outputs.get(0), List.of(DyeItem.byColor(color))));
}
changed.set(true);
}
Expand All @@ -85,7 +89,7 @@ public void setRecipe(RecipeHolder<BackpackCraftingRecipe> holder, IRecipeLayout
if (color == DyeColor.WHITE) {
return outputs.get(0);
}
return DyeableItem.colorStack(outputs.get(0), List.of(color));
return DyedItemColor.applyDyes(outputs.get(0), List.of(DyeItem.byColor(color)));
}).forEach(outputs::add);
outputs.remove(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SlotModIntegrationClientEvents {
private static void onEndClientTick(Minecraft minecraft) {
while (SlotModIntegrationKeyMappings.OPEN_BACKPACK.get().consumeClick()) {
if (minecraft.screen == null) {
SlotModIntegrationNetwork.NETWORK.sendToServer(new OpenBackpackMessage());
SlotModIntegrationNetwork.OPEN_BACKPACK_MESSAGE.sendToServer(new OpenBackpackMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package info.u_team.useful_backpacks.integration.slot_mod.init;

import java.util.Optional;

import info.u_team.u_team_core.api.network.NetworkEnvironment;
import info.u_team.u_team_core.api.network.NetworkHandler;
import info.u_team.u_team_core.api.network.NetworkHandlerEnvironment;
import info.u_team.u_team_core.api.network.NetworkMessage;
import info.u_team.useful_backpacks.UsefulBackpacksReference;
import info.u_team.useful_backpacks.integration.slot_mod.message.OpenBackpackMessage;
import net.minecraft.resources.ResourceLocation;

public class SlotModIntegrationNetwork {

public static final NetworkHandler NETWORK = NetworkHandler.create(0, new ResourceLocation(UsefulBackpacksReference.MODID, "slot_mod"));
public static final NetworkHandler NETWORK = NetworkHandler.create(ResourceLocation.fromNamespaceAndPath(UsefulBackpacksReference.MODID, "slot_mod"), 0);

public static final NetworkMessage<OpenBackpackMessage> OPEN_BACKPACK_MESSAGE = NETWORK.register("open_backpack", NetworkHandlerEnvironment.SERVER, OpenBackpackMessage.STREAM_CODEC, OpenBackpackMessage::handle);

public static void register() {
NETWORK.registerMessage(0, OpenBackpackMessage.class, OpenBackpackMessage::encode, OpenBackpackMessage::decode, OpenBackpackMessage.Handler::handle, Optional.of(NetworkEnvironment.SERVER));
static void register() {
NETWORK.register();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,35 @@
import info.u_team.u_team_core.api.network.NetworkContext;
import info.u_team.useful_backpacks.api.Backpack;
import info.u_team.useful_backpacks.integration.slot_mod.util.BackpackSlotModUtil;
import net.minecraft.network.FriendlyByteBuf;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;

public class OpenBackpackMessage {

public static void encode(OpenBackpackMessage message, FriendlyByteBuf buffer) {
}

public static OpenBackpackMessage decode(FriendlyByteBuf buffer) {
return new OpenBackpackMessage();
}
public static final OpenBackpackMessage INSTANCE = new OpenBackpackMessage();
public static final StreamCodec<ByteBuf, OpenBackpackMessage> STREAM_CODEC = StreamCodec.unit(INSTANCE);

public static class Handler {

public static void handle(OpenBackpackMessage message, NetworkContext context) {
if (!(context.getPlayer() instanceof ServerPlayer player)) {
public static void handle(OpenBackpackMessage message, NetworkContext context) {
if (!(context.getPlayer() instanceof ServerPlayer player)) {
return;
}
context.executeOnMainThread(() -> {
if (!player.isAlive()) {
return;
}
context.executeOnMainThread(() -> {
if (!player.isAlive()) {
return;
}
if (player.hasContainerOpen()) {
return;
}
final Optional<ItemStack> curioBackpack = BackpackSlotModUtil.findBackpack(player);
if (curioBackpack.isPresent()) {
final ItemStack stack = curioBackpack.get();
if (stack.getItem() instanceof final Backpack backpack) {
backpack.open(player, stack, -2);
}
if (player.hasContainerOpen()) {
return;
}
final Optional<ItemStack> curioBackpack = BackpackSlotModUtil.findBackpack(player);
if (curioBackpack.isPresent()) {
final ItemStack stack = curioBackpack.get();
if (stack.getItem() instanceof final Backpack backpack) {
backpack.open(player, stack, -2);
}
});
}
}
});
}

}

0 comments on commit d183c72

Please sign in to comment.