Skip to content

Commit

Permalink
Hide patterns on for-purchase scrolls; new creative tab with scrolls
Browse files Browse the repository at this point in the history
  • Loading branch information
Robotgiggle committed Dec 13, 2024
1 parent b34ec0f commit 1231285
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,45 @@
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
import at.petrak.hexcasting.common.misc.PatternTooltip;
import at.petrak.hexcasting.common.loot.AddPerWorldPatternToScrollFunc;
import at.petrak.hexcasting.interop.inline.InlinePatternData;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.List;

import static at.petrak.hexcasting.api.HexAPI.modLoc;

/**
* TAG_OP_ID and TAG_PATTERN: "Ancient Scroll of %s" (Great Spells)
* TAG_OP_ID and TAG_PATTERN: "Ancient Scroll of %s" (per-world pattern preloaded)
* <br>
* TAG_OP_ID: "Ancient Scroll of %s" (per-world pattern loaded on inv tick)
* <br>
* TAG_PATTERN: "Scroll" (custom)
* <br>
* (none): "Empty Scroll"
* <br>
* TAG_OP_ID: invalid
*/
public class ItemScroll extends Item implements IotaHolderItem {
public static final String TAG_OP_ID = "op_id";
public static final String TAG_PATTERN = "pattern";
public static final String TAG_NEEDS_PURCHASE = "needs_purchase";
public static final ResourceLocation ANCIENT_PREDICATE = modLoc("ancient");

public final int blockSize;
Expand All @@ -50,6 +57,14 @@ public ItemScroll(Properties pProperties, int blockSize) {
this.blockSize = blockSize;
}

public static ItemStack withPerWorldPattern(ItemStack stack, String op_id) {
Item item = stack.getItem();
if (item instanceof ItemScroll)
NBTHelper.putString(stack, TAG_OP_ID, op_id);

return stack;
}

@Override
public @Nullable
CompoundTag readIotaTag(ItemStack stack) {
Expand Down Expand Up @@ -133,7 +148,7 @@ public Component getName(ItemStack pStack) {
return Component.translatable(descID + ".of",
Component.translatable("hexcasting.action." + ResourceLocation.tryParse(ancientId)));
} else if (NBTHelper.hasCompound(pStack, TAG_PATTERN)) {
var compound = NBTHelper.getCompound(pStack, ItemScroll.TAG_PATTERN);
var compound = NBTHelper.getCompound(pStack, TAG_PATTERN);
var patternLabel = Component.literal("");
if (compound != null) {
var pattern = HexPattern.fromNBT(compound);
Expand All @@ -145,16 +160,39 @@ public Component getName(ItemStack pStack) {
}
}

// purposely no hover text
@Override
public void inventoryTick(ItemStack pStack, Level pLevel, Entity pEntity, int pSlotId, boolean pIsSelected) {
// the needs_purchase tag is used so you can't see the pattern on scrolls sold by a wandering trader
// once you put the scroll into your inventory, this removes the tag to reveal the pattern
if (NBTHelper.getBoolean(pStack, TAG_NEEDS_PURCHASE)) {
NBTHelper.remove(pStack, TAG_NEEDS_PURCHASE);
}
// if op_id is set but there's no stored pattern, load the pattern on inv tick
if (NBTHelper.hasString(pStack, TAG_OP_ID) && !NBTHelper.hasCompound(pStack, TAG_PATTERN) && pEntity.getServer() != null) {
AddPerWorldPatternToScrollFunc.doStatic(pStack, pLevel.getRandom(), pEntity.getServer().overworld());
}
}

@Override
public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents,
TooltipFlag pIsAdvanced) {
if (NBTHelper.getBoolean(pStack, TAG_NEEDS_PURCHASE)) {
var needsPurchase = Component.translatable("hexcasting.tooltip.scroll.needs_purchase");
pTooltipComponents.add(needsPurchase.withStyle(ChatFormatting.GRAY));
} else if (NBTHelper.hasString(pStack, TAG_OP_ID) && !NBTHelper.hasCompound(pStack, TAG_PATTERN)) {
var notLoaded = Component.translatable("hexcasting.tooltip.scroll.pattern_not_loaded");
pTooltipComponents.add(notLoaded.withStyle(ChatFormatting.GRAY));
}
}

@Override
public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
var compound = NBTHelper.getCompound(stack, ItemScroll.TAG_PATTERN);
if (compound != null) {
var compound = NBTHelper.getCompound(stack, TAG_PATTERN);
if (compound != null && !NBTHelper.getBoolean(stack, TAG_NEEDS_PURCHASE)) {
var pattern = HexPattern.fromNBT(compound);
return Optional.of(new PatternTooltip(
pattern,
NBTHelper.hasString(stack, ItemScroll.TAG_OP_ID)
NBTHelper.hasString(stack, TAG_OP_ID)
? PatternTooltipComponent.ANCIENT_BG
: PatternTooltipComponent.PRISTINE_BG));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ private static <T extends Block> T blockNoItem(String name, T block) {
return block;
}
private static <T extends Block> T blockItem(String name, T block) {
return blockItem(name, block, HexItems.props(), HexCreativeTabs.HEX);
return blockItem(name, block, HexItems.props(), HexCreativeTabs.MAIN);
}

private static <T extends Block> T blockItem(String name, T block, @Nullable CreativeModeTab tab) {
return blockItem(name, block, HexItems.props(), tab);
}
private static <T extends Block> T blockItem(String name, T block, Item.Properties props) {
return blockItem(name, block, props, HexCreativeTabs.HEX);
return blockItem(name, block, props, HexCreativeTabs.MAIN);
}

private static <T extends Block> T blockItem(String name, T block, Item.Properties props, @Nullable CreativeModeTab tab) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import at.petrak.hexcasting.common.items.storage.ItemScroll;

import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -20,11 +21,14 @@ public static void registerCreativeTabs(BiConsumer<CreativeModeTab, ResourceLoca

private static final Map<ResourceLocation, CreativeModeTab> TABS = new LinkedHashMap<>();

public static final CreativeModeTab HEX = register("hexcasting", CreativeModeTab.builder(CreativeModeTab.Row.TOP, 7)
public static final CreativeModeTab MAIN = register("main", CreativeModeTab.builder(CreativeModeTab.Row.TOP, 7)
.icon(() -> new ItemStack(HexItems.SPELLBOOK)));

public static final CreativeModeTab SCROLLS = register("scrolls", CreativeModeTab.builder(CreativeModeTab.Row.TOP, 7)
.icon(() -> ItemScroll.withPerWorldPattern(new ItemStack(HexItems.SCROLL_LARGE),"")));

private static CreativeModeTab register(String name, CreativeModeTab.Builder tabBuilder) {
var tab = tabBuilder.title(Component.translatable("itemGroup." + name)).build();
var tab = tabBuilder.title(Component.translatable("itemGroup.hexcasting." + name)).build();
var old = TABS.put(modLoc(name), tab);
if (old != null) {
throw new IllegalArgumentException("Typo? Duplicate id " + name);
Expand Down
57 changes: 50 additions & 7 deletions Common/src/main/java/at/petrak/hexcasting/common/lib/HexItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,67 @@ public static void registerItemCreativeTab(CreativeModeTab.Output r, CreativeMod
public static final Supplier<ItemStack> BATTERY_DUST_STACK = addToTab(() -> ItemMediaBattery.withMedia(
new ItemStack(HexItems.BATTERY),
MediaConstants.DUST_UNIT * 64,
MediaConstants.DUST_UNIT * 64), HexCreativeTabs.HEX);
MediaConstants.DUST_UNIT * 64), HexCreativeTabs.MAIN);
public static final Supplier<ItemStack> BATTERY_SHARD_STACK = addToTab(() -> ItemMediaBattery.withMedia(
new ItemStack(HexItems.BATTERY),
MediaConstants.SHARD_UNIT * 64,
MediaConstants.SHARD_UNIT * 64), HexCreativeTabs.HEX);
MediaConstants.SHARD_UNIT * 64), HexCreativeTabs.MAIN);
public static final Supplier<ItemStack> BATTERY_CRYSTAL_STACK = addToTab(() -> ItemMediaBattery.withMedia(
new ItemStack(HexItems.BATTERY),
MediaConstants.CRYSTAL_UNIT * 64,
MediaConstants.CRYSTAL_UNIT * 64), HexCreativeTabs.HEX);
MediaConstants.CRYSTAL_UNIT * 64), HexCreativeTabs.MAIN);
public static final Supplier<ItemStack> BATTERY_QUENCHED_SHARD_STACK = addToTab(() -> ItemMediaBattery.withMedia(
new ItemStack(HexItems.BATTERY),
MediaConstants.QUENCHED_SHARD_UNIT * 64,
MediaConstants.QUENCHED_SHARD_UNIT * 64), HexCreativeTabs.HEX);

MediaConstants.QUENCHED_SHARD_UNIT * 64), HexCreativeTabs.MAIN);
public static final Supplier<ItemStack> BATTERY_QUENCHED_BLOCK_STACK = addToTab(() -> ItemMediaBattery.withMedia(
new ItemStack(HexItems.BATTERY),
MediaConstants.QUENCHED_BLOCK_UNIT * 64,
MediaConstants.QUENCHED_BLOCK_UNIT * 64), HexCreativeTabs.HEX);
MediaConstants.QUENCHED_BLOCK_UNIT * 64), HexCreativeTabs.MAIN);

public static final Supplier<ItemStack> SCROLL_LIGHTNING = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:lightning"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_FLIGHT = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:flight"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_CREATE_LAVA = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:create_lava"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_GREATER_TELEPORT = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:teleport/great"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_GREATER_SENTINEL = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:sentinel/create/great"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_DISPEL_RAIN = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:dispel_rain"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_SUMMON_RAIN = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:summon_rain"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_FLAY_MIND = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:brainsweep"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_CRAFT_PHIAL = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:craft/battery"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_WHITE_ZENITH = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:potion/regeneration"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_BLUE_ZENITH = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:potion/night_vision"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_BLACK_ZENITH = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:potion/absorption"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_RED_ZENITH = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:potion/haste"), HexCreativeTabs.SCROLLS);
public static final Supplier<ItemStack> SCROLL_GREEN_ZENITH = addToTab(() -> ItemScroll.withPerWorldPattern(
new ItemStack(HexItems.SCROLL_LARGE),
"hexcasting:potion/strength"), HexCreativeTabs.SCROLLS);


public static final EnumMap<DyeColor, ItemDyePigment> DYE_PIGMENTS = Util.make(() -> {
var out = new EnumMap<DyeColor, ItemDyePigment>(DyeColor.class);
Expand Down Expand Up @@ -172,7 +215,7 @@ private static <T extends Item> T make(String id, T item, @Nullable CreativeMode
}

private static <T extends Item> T make(String id, T item) {
return make(modLoc(id), item, HexCreativeTabs.HEX);
return make(modLoc(id), item, HexCreativeTabs.MAIN);
}

private static Supplier<ItemStack> addToTab(Supplier<ItemStack> stack, CreativeModeTab tab) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public AddHexToAncientCypherFunc(LootItemCondition[] lootItemConditions) {
public static ItemStack doStatic(ItemStack stack, RandomSource rand) {
var fullHex = HexConfig.server().getRandomLootHex(rand.nextInt());
var patsTag = new ListTag();
Registry<ActionRegistryEntry> regi = IXplatAbstractions.INSTANCE.getActionRegistry();
// skip first element since it's the name, not a pattern
for (var patString : fullHex.subList(1,fullHex.size())){
var pieces = patString.split(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import at.petrak.hexcasting.api.casting.ActionRegistryEntry;
import at.petrak.hexcasting.api.mod.HexTags;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.api.utils.NBTHelper;
import at.petrak.hexcasting.common.casting.PatternRegistryManifest;
import at.petrak.hexcasting.common.items.storage.ItemScroll;
import at.petrak.hexcasting.common.lib.HexLootFunctions;
Expand All @@ -14,6 +15,7 @@
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.LootContext;
Expand All @@ -38,22 +40,25 @@ public AddPerWorldPatternToScrollFunc(LootItemCondition[] lootItemConditions) {
* This doesn't actually have any params so extract behaviour out for the benefit of forge
*/
public static ItemStack doStatic(ItemStack stack, RandomSource rand, ServerLevel overworld) {
var perWorldKeys = new ArrayList<ResourceKey<ActionRegistryEntry>>();
Registry<ActionRegistryEntry> regi = IXplatAbstractions.INSTANCE.getActionRegistry();
for (var key : regi.registryKeySet()) {
if (HexUtils.isOfTag(regi, key, HexTags.Actions.PER_WORLD_PATTERN)) {
perWorldKeys.add(key);
ResourceKey patternKey;
if (NBTHelper.hasString(stack, ItemScroll.TAG_OP_ID)) {
patternKey = ResourceKey.create(
IXplatAbstractions.INSTANCE.getActionRegistry().key(),
ResourceLocation.tryParse(NBTHelper.getString(stack, ItemScroll.TAG_OP_ID))
);
} else {
var perWorldKeys = new ArrayList<ResourceKey<ActionRegistryEntry>>();
Registry<ActionRegistryEntry> regi = IXplatAbstractions.INSTANCE.getActionRegistry();
for (var key : regi.registryKeySet()) {
if (HexUtils.isOfTag(regi, key, HexTags.Actions.PER_WORLD_PATTERN)) {
perWorldKeys.add(key);
}
}
patternKey = perWorldKeys.get(rand.nextInt(perWorldKeys.size()));
NBTHelper.putString(stack, ItemScroll.TAG_OP_ID, patternKey.location().toString());
}
var key = perWorldKeys.get(rand.nextInt(perWorldKeys.size()));

var pat = PatternRegistryManifest.getCanonicalStrokesPerWorld(key, overworld);
var tag = new CompoundTag();
tag.putString(ItemScroll.TAG_OP_ID, key.location().toString());
tag.put(ItemScroll.TAG_PATTERN, pat.serializeToNBT());

stack.getOrCreateTag().merge(tag);

var pat = PatternRegistryManifest.getCanonicalStrokesPerWorld(patternKey, overworld);
NBTHelper.put(stack, ItemScroll.TAG_PATTERN, pat.serializeToNBT());
return stack;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import net.minecraft.util.RandomSource;
import net.minecraft.server.MinecraftServer;
import at.petrak.hexcasting.api.mod.HexConfig;
import at.petrak.hexcasting.api.utils.NBTHelper;
import at.petrak.hexcasting.common.lib.HexItems;
import at.petrak.hexcasting.common.loot.AddPerWorldPatternToScrollFunc;
import at.petrak.hexcasting.common.items.storage.ItemScroll;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -28,6 +30,7 @@ private void addNewTrades(CallbackInfo ci) {
if (rand.nextFloat() < HexConfig.server().traderScrollChance() && self.getServer() != null) {
ItemStack scroll = new ItemStack(HexItems.SCROLL_LARGE);
AddPerWorldPatternToScrollFunc.doStatic(scroll, rand, self.getServer().overworld());
NBTHelper.putBoolean(scroll, ItemScroll.TAG_NEEDS_PURCHASE, true);
offerList.set(rand.nextInt(5), new MerchantOffer(new ItemStack(Items.EMERALD, 12), scroll, 1, 1, 1));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@
},

"itemGroup.hexcasting": {
"": "Hexcasting",
creative_tab: "Hexcasting",
main: "Hex Casting",
scrolls: "Hex Casting (Scrolls)",
},

"gui.hexcasting": {
Expand Down Expand Up @@ -573,6 +573,11 @@
},
sealed: "Sealed",
},

scroll: {
needs_purchase: "Purchase to show pattern",
pattern_not_loaded: "Place in inventory to load pattern",
}

abacus: {
"": "%d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": 1,
"show_progress": false,
"nameplate_color": "00072b",
"creative_tab": "hexcasting:hexcasting",
"creative_tab": "hexcasting:main",
"model": "hexcasting:patchouli_book",
"book_texture": "hexcasting:textures/gui/patchi_book.png",
"filler_texture": "hexcasting:textures/gui/patchi_filler.png",
Expand Down

0 comments on commit 1231285

Please sign in to comment.