Skip to content

Commit

Permalink
fix: possible workaround for shift-crafting not calling onCraftedBy
Browse files Browse the repository at this point in the history
Closes #1179
  • Loading branch information
klikli-dev committed Aug 3, 2024
1 parent 61549ac commit e6530a5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.util.thread.EffectiveSide;

import javax.annotation.Nullable;
import java.util.List;
Expand All @@ -49,9 +53,23 @@ public void appendHoverText(ItemStack pStack, TooltipContext pContext, List<Comp
TextUtil.formatDemonName(ItemNBTUtil.getBoundSpiritName(pStack))));
}

@Override
public void verifyComponentsAfterLoad(ItemStack stack) {
if (EffectiveSide.get().isServer()) {
ItemNBTUtil.generateBoundSpiritNameIfNone(stack);
}
super.verifyComponentsAfterLoad(stack);
}

@Override
public void onCraftedBy(ItemStack pStack, Level pLevel, Player pPlayer) {
ItemNBTUtil.generateBoundSpiritName(pStack);
ItemNBTUtil.generateBoundSpiritNameIfNone(pStack);
super.onCraftedBy(pStack, pLevel, pPlayer);
}

@Override
public void onCraftedPostProcess(ItemStack stack, Level level) {
ItemNBTUtil.generateBoundSpiritNameIfNone(stack);
super.onCraftedPostProcess(stack, level);
}
}
29 changes: 15 additions & 14 deletions src/main/java/com/klikli_dev/occultism/registry/OccultismItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
import com.klikli_dev.occultism.common.item.storage.*;
import com.klikli_dev.occultism.common.item.tool.BrushItem;
import com.klikli_dev.occultism.common.item.tool.*;
import com.klikli_dev.occultism.util.TextUtil;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.*;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.neoforged.neoforge.common.DeferredSpawnEggItem;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.neoforge.registries.DeferredRegister;
Expand Down Expand Up @@ -207,43 +207,44 @@ public class OccultismItems {
() -> new Item(defaultProperties()));
public static final DeferredItem<Item> TABOO_BOOK = ITEMS.register("taboo_book",
() -> new Item(defaultProperties()));

//Books of Binding
public static final DeferredItem<BookOfBindingItem> BOOK_OF_BINDING_EMPTY = ITEMS.register(
"book_of_binding_empty", () -> new BookOfBindingItem(defaultProperties().stacksTo(1)));
public static final DeferredItem<BookOfBindingItem> BOOK_OF_BINDING_FOLIOT = ITEMS.register(
"book_of_binding_foliot", () -> new BookOfBindingItem(defaultProperties().stacksTo(1)));
public static final DeferredItem<BookOfBindingBoundItem> BOOK_OF_BINDING_BOUND_FOLIOT = ITEMS.register(
"book_of_binding_bound_foliot", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)")));
"book_of_binding_bound_foliot", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)));
public static final DeferredItem<BookOfBindingItem> BOOK_OF_BINDING_DJINNI = ITEMS.register(
"book_of_binding_djinni", () -> new BookOfBindingItem(defaultProperties().stacksTo(1)));
public static final DeferredItem<BookOfBindingBoundItem> BOOK_OF_BINDING_BOUND_DJINNI = ITEMS.register(
"book_of_binding_bound_djinni", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)")));
"book_of_binding_bound_djinni", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)));
public static final DeferredItem<BookOfBindingItem> BOOK_OF_BINDING_AFRIT = ITEMS.register(
"book_of_binding_afrit", () -> new BookOfBindingItem(defaultProperties().stacksTo(1)));
public static final DeferredItem<BookOfBindingBoundItem> BOOK_OF_BINDING_BOUND_AFRIT = ITEMS.register(
"book_of_binding_bound_afrit", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)")));
"book_of_binding_bound_afrit", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)));
public static final DeferredItem<BookOfBindingItem> BOOK_OF_BINDING_MARID = ITEMS.register(
"book_of_binding_marid", () -> new BookOfBindingItem(defaultProperties().stacksTo(1)));
public static final DeferredItem<BookOfBindingBoundItem> BOOK_OF_BINDING_BOUND_MARID = ITEMS.register(
"book_of_binding_bound_marid", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)")));
"book_of_binding_bound_marid", () -> new BookOfBindingBoundItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)));
//Books of Calling
//Foliot
public static final DeferredItem<BookOfCallingLumberjackItem> BOOK_OF_CALLING_FOLIOT_LUMBERJACK =
ITEMS.register("book_of_calling_foliot_lumberjack",
() -> new BookOfCallingLumberjackItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
() -> new BookOfCallingLumberjackItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
TranslationKeys.BOOK_OF_CALLING_GENERIC + "_foliot"));
public static final DeferredItem<BookOfCallingTransportItems> BOOK_OF_CALLING_FOLIOT_TRANSPORT_ITEMS =
ITEMS.register("book_of_calling_foliot_transport_items",
() -> new BookOfCallingTransportItems(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
() -> new BookOfCallingTransportItems(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
TranslationKeys.BOOK_OF_CALLING_GENERIC + "_foliot"));
public static final DeferredItem<BookOfCallingCleanerItem> BOOK_OF_CALLING_FOLIOT_CLEANER =
ITEMS.register("book_of_calling_foliot_cleaner",
() -> new BookOfCallingCleanerItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
() -> new BookOfCallingCleanerItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
TranslationKeys.BOOK_OF_CALLING_GENERIC + "_foliot"));
//Djinn
public static final DeferredItem<BookOfCallingManageMachineItem> BOOK_OF_CALLING_DJINNI_MANAGE_MACHINE =
ITEMS.register("book_of_calling_djinni_manage_machine",
() -> new BookOfCallingManageMachineItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
() -> new BookOfCallingManageMachineItem(defaultProperties().stacksTo(1).component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
TranslationKeys.BOOK_OF_CALLING_GENERIC + "_djinni"));

//Armor
Expand Down Expand Up @@ -278,30 +279,30 @@ public class OccultismItems {
public static final DeferredItem<MinerSpiritItem> MINER_DEBUG_UNSPECIALIZED =
ITEMS.register("miner_debug_unspecialized",
() -> new MinerSpiritItem(defaultProperties()
.component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)")
.component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)
.durability(10000), () -> 100, () -> 10, () -> 10000));
public static final DeferredItem<MinerSpiritItem> MINER_FOLIOT_UNSPECIALIZED = ITEMS.register("miner_foliot_unspecialized",
() -> new MinerSpiritItem(defaultProperties()
.component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)")
.component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)
.durability(1000),
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.maxMiningTime,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.rollsPerOperation,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerFoliotUnspecialized.durability));
public static final DeferredItem<MinerSpiritItem> MINER_DJINNI_ORES = ITEMS.register("miner_djinni_ores",
() -> new MinerSpiritItem(defaultProperties().durability(400)
.component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
.component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerDjinniOres.maxMiningTime,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerDjinniOres.rollsPerOperation,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerDjinniOres.durability));
public static final DeferredItem<MinerSpiritItem> MINER_AFRIT_DEEPS = ITEMS.register("miner_afrit_deeps",
() -> new MinerSpiritItem(defaultProperties().durability(800)
.component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
.component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerAfritDeeps.maxMiningTime,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerAfritDeeps.rollsPerOperation,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerAfritDeeps.durability));
public static final DeferredItem<MinerSpiritItem> MINER_MARID_MASTER = ITEMS.register("miner_marid_master",
() -> new MinerSpiritItem(defaultProperties().durability(1600)
.component(OccultismDataComponents.SPIRIT_NAME, "(Not yet known)"),
.component(OccultismDataComponents.SPIRIT_NAME, TextUtil.SPIRIT_NAME_NOT_YET_KNOWN),
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerMaridMaster.maxMiningTime,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerMaridMaster.rollsPerOperation,
Occultism.SERVER_CONFIG.dimensionalMineshaft.minerMaridMaster.durability));
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/klikli_dev/occultism/util/ItemNBTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public static void updateItemNBTFromEntity(ItemStack stack, SpiritEntity entity)

}

public static void generateBoundSpiritNameIfNone(ItemStack stack) {
if (!stack.has(OccultismDataComponents.SPIRIT_NAME) ||
stack.get(OccultismDataComponents.SPIRIT_NAME).isEmpty() ||
stack.get(OccultismDataComponents.SPIRIT_NAME).equals(TextUtil.SPIRIT_NAME_NOT_YET_KNOWN)
) {
generateBoundSpiritName(stack);
}
}

public static void generateBoundSpiritName(ItemStack stack) {
setBoundSpiritName(stack, TextUtil.generateName());
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/klikli_dev/occultism/util/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

public class TextUtil {

public static final String SPIRIT_NAME_NOT_YET_KNOWN = "(Not yet known)";
private static final Map<String, String> MOD_NAME_TO_ID = new HashMap<>();

//KliKli: Obvious :)
Expand Down

0 comments on commit e6530a5

Please sign in to comment.