Another debug cube feature

This commit is contained in:
yrsegal@gmail.com 2022-10-17 21:53:19 -04:00
parent cf1d65cae0
commit 5193a20a0a
7 changed files with 88 additions and 36 deletions

View file

@ -10,6 +10,7 @@ import net.minecraft.world.item.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
@ -19,6 +20,7 @@ public class DiscoveryHandlers {
private static final List<ToFloatFunction<Player>> GRID_SCALE_MODIFIERS = new ArrayList<>();
private static final List<Function<CastingContext, List<ItemStack>>> ITEM_SLOT_DISCOVERER = new ArrayList<>();
private static final List<Function<CastingContext, List<ItemStack>>> OPERATIVE_SLOT_DISCOVERER = new ArrayList<>();
private static final List<BiFunction<Player, String, ItemStack>> DEBUG_DISCOVERER = new ArrayList<>();
public static boolean hasLens(Player player) {
for (var predicate : HAS_LENS_PREDICATE) {
@ -61,6 +63,16 @@ public class DiscoveryHandlers {
return stacks;
}
public static ItemStack findDebugItem(Player player, String type) {
for (var discoverer : DEBUG_DISCOVERER) {
var stack = discoverer.apply(player, type);
if (!stack.isEmpty()) {
return stack;
}
}
return ItemStack.EMPTY;
}
public static void addLensPredicate(Predicate<Player> predicate) {
HAS_LENS_PREDICATE.add(predicate);
}
@ -80,4 +92,8 @@ public class DiscoveryHandlers {
public static void addOperativeSlotDiscoverer(Function<CastingContext, List<ItemStack>> discoverer) {
OPERATIVE_SLOT_DISCOVERER.add(discoverer);
}
public static void addDebugItemDiscoverer(BiFunction<Player, String, ItemStack> discoverer) {
DEBUG_DISCOVERER.add(discoverer);
}
}

View file

@ -8,6 +8,7 @@ import at.petrak.hexcasting.api.spell.mishaps.MishapEntityTooFarAway
import at.petrak.hexcasting.api.spell.mishaps.MishapEvalTooDeep
import at.petrak.hexcasting.api.spell.mishaps.MishapLocationTooFarAway
import at.petrak.hexcasting.api.utils.otherHand
import at.petrak.hexcasting.common.items.magic.ItemCreativeUnlocker
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.server.level.ServerLevel
@ -200,6 +201,10 @@ data class CastingContext(
return advs.getOrStartProgress(adv!!).isDone
}
val debugPatterns: Boolean by lazy {
!DiscoveryHandlers.findDebugItem(this.caster, ItemCreativeUnlocker.DISPLAY_PATTERNS).isEmpty
}
companion object {
init {
DiscoveryHandlers.addItemSlotDiscoverer {

View file

@ -15,6 +15,7 @@ import at.petrak.hexcasting.api.spell.math.HexPattern
import at.petrak.hexcasting.api.spell.mishaps.*
import at.petrak.hexcasting.api.utils.*
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.Util
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.Tag
import net.minecraft.resources.ResourceLocation
@ -47,6 +48,12 @@ class CastingHarness private constructor(
*/
fun executeIota(iota: SpellDatum<*>, world: ServerLevel): ControllerInfo = executeIotas(listOf(iota), world)
private fun displayPattern(pattern: Operator?, iota: SpellDatum<*>) {
if (this.ctx.debugPatterns) {
this.ctx.caster.sendMessage(pattern?.displayName ?: iota.display(), Util.NIL_UUID)
}
}
private fun getOperatorForPattern(iota: SpellDatum<*>, world: ServerLevel): Operator? {
if (iota.getType() == DatumType.PATTERN)
return PatternRegistry.matchPattern(iota.payload as HexPattern, world)
@ -192,6 +199,7 @@ class CastingHarness private constructor(
var cont2 = continuation
if (!unenlightened || pattern.alwaysProcessGreatSpell) {
displayPattern(pattern, SpellDatum.make(newPat))
val result = pattern.operate(
continuation,
this.stack.toMutableList(),
@ -311,7 +319,7 @@ class CastingHarness private constructor(
}
}
return if (this.parenCount > 0) {
val out = if (this.parenCount > 0) {
if (this.escapeNext) {
val newParens = this.parenthesized.toMutableList()
newParens.add(iota)
@ -380,6 +388,11 @@ class CastingHarness private constructor(
} else {
null
}
if (out != null) {
displayPattern(operator, iota)
}
return out
}
/**

View file

@ -36,40 +36,58 @@ import static at.petrak.hexcasting.api.HexAPI.modLoc;
public class ItemCreativeUnlocker extends Item implements ManaHolderItem {
static {
DiscoveryHandlers.addManaHolderDiscoverer(harness -> {
var player = harness.getCtx().getCaster();
if (!player.isCreative())
return List.of();
public static final String DISPLAY_MEDIA = "media";
public static final String DISPLAY_PATTERNS = "patterns";
static {
DiscoveryHandlers.addDebugItemDiscoverer((player, type) -> {
for (ItemStack item : player.getInventory().items) {
if (isDebug(item)) {
return List.of(new DebugUnlockerHolder(item));
if (isDebug(item, type)) {
return item;
}
}
// Technically possible with commands!
for (ItemStack item : player.getInventory().armor) {
if (isDebug(item)) {
return List.of(new DebugUnlockerHolder(item));
if (isDebug(item, type)) {
return item;
}
}
for (ItemStack item : player.getInventory().offhand) {
if (isDebug(item)) {
return List.of(new DebugUnlockerHolder(item));
if (isDebug(item, type)) {
return item;
}
}
return ItemStack.EMPTY;
});
DiscoveryHandlers.addManaHolderDiscoverer(harness -> {
var player = harness.getCtx().getCaster();
if (!player.isCreative())
return List.of();
ItemStack stack = DiscoveryHandlers.findDebugItem(player, DISPLAY_MEDIA);
if (!stack.isEmpty())
return List.of(new DebugUnlockerHolder(stack));
return List.of();
});
}
public static boolean isDebug(ItemStack stack) {
return stack.is(HexItems.CREATIVE_UNLOCKER)
&& stack.hasCustomHoverName()
&& stack.getHoverName().getString().toLowerCase(Locale.ROOT).contains("debug");
return isDebug(stack, null);
}
public static boolean isDebug(ItemStack stack, String flag) {
if (!stack.is(HexItems.CREATIVE_UNLOCKER) || !stack.hasCustomHoverName()) {
return false;
}
var keywords = Arrays.asList(stack.getHoverName().getString().toLowerCase(Locale.ROOT).split(" "));
if (!keywords.contains("debug")) {
return false;
}
return flag == null || keywords.contains(flag);
}
public static Component infiniteMedia(Level level) {
@ -129,7 +147,7 @@ public class ItemCreativeUnlocker extends Item implements ManaHolderItem {
@Override
public int withdrawMana(ItemStack stack, int cost, boolean simulate) {
// In case it's withdrawn through other means
if (!simulate && isDebug(stack)) {
if (!simulate && isDebug(stack, DISPLAY_MEDIA)) {
addToIntArray(stack, TAG_EXTRACTIONS, cost);
}
@ -139,7 +157,7 @@ public class ItemCreativeUnlocker extends Item implements ManaHolderItem {
@Override
public int insertMana(ItemStack stack, int amount, boolean simulate) {
// In case it's inserted through other means
if (!simulate && isDebug(stack)) {
if (!simulate && isDebug(stack, DISPLAY_MEDIA)) {
addToIntArray(stack, TAG_INSERTIONS, amount);
}
@ -153,7 +171,7 @@ public class ItemCreativeUnlocker extends Item implements ManaHolderItem {
@Override
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, boolean selected) {
if (isDebug(stack) && !level.isClientSide) {
if (isDebug(stack, DISPLAY_MEDIA) && !level.isClientSide) {
debugDisplay(stack, TAG_EXTRACTIONS, "withdrawn", "all_mana", entity);
debugDisplay(stack, TAG_INSERTIONS, "inserted", "infinite_mana", entity);
}

View file

@ -182,10 +182,10 @@
"command.hexcasting.brainsweep.fail.badtype": "%s is not a villager",
"command.hexcasting.brainsweep.fail.already": "%s is already empty",
"hexcasting.pattern.unknown": "Unknown pattern resource location %s",
"hexcasting.debug.mana_withdrawn": "%s - Mana withdrawn: %s",
"hexcasting.debug.mana_withdrawn.with_dust": "%s - Mana withdrawn: %s (%s in dust)",
"hexcasting.debug.mana_inserted": "%s - Mana inserted: %s",
"hexcasting.debug.mana_inserted.with_dust": "%s - Mana inserted: %s (%s in dust)",
"hexcasting.debug.mana_withdrawn": "%s - Media withdrawn: %s",
"hexcasting.debug.mana_withdrawn.with_dust": "%s - Media withdrawn: %s (%s in dust)",
"hexcasting.debug.mana_inserted": "%s - Media inserted: %s",
"hexcasting.debug.mana_inserted.with_dust": "%s - Media inserted: %s (%s in dust)",
"hexcasting.debug.all_mana": "Entire contents",
"hexcasting.debug.infinite_mana": "Infinite",

View file

@ -2,7 +2,6 @@ package at.petrak.hexcasting.fabric.interop.trinkets;
import at.petrak.hexcasting.api.misc.DiscoveryHandlers;
import at.petrak.hexcasting.api.utils.ManaHelper;
import at.petrak.hexcasting.common.items.magic.DebugUnlockerHolder;
import at.petrak.hexcasting.common.items.magic.ItemCreativeUnlocker;
import at.petrak.hexcasting.common.lib.HexItems;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
@ -12,6 +11,7 @@ import dev.emi.trinkets.api.client.TrinketRendererRegistry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.Tuple;
import net.minecraft.world.item.ItemStack;
import java.util.List;
import java.util.Objects;
@ -41,16 +41,16 @@ public class TrinketsApiInterop {
return List.of();
});
DiscoveryHandlers.addManaHolderDiscoverer(harness -> {
Optional<TrinketComponent> optional = TrinketsApi.getTrinketComponent(harness.getCtx().getCaster());
DiscoveryHandlers.addDebugItemDiscoverer((player, type) -> {
Optional<TrinketComponent> optional = TrinketsApi.getTrinketComponent(player);
if (optional.isPresent()) {
TrinketComponent component = optional.get();
var equipped = component.getEquipped(ItemCreativeUnlocker::isDebug);
var equipped = component.getEquipped(stack -> ItemCreativeUnlocker.isDebug(stack, type));
if (!equipped.isEmpty()) {
return List.of(new DebugUnlockerHolder(equipped.get(0).getB()));
return equipped.get(0).getB();
}
}
return List.of();
return ItemStack.EMPTY;
});
}

View file

@ -3,12 +3,12 @@ package at.petrak.hexcasting.forge.interop.curios;
import at.petrak.hexcasting.api.addldata.ManaHolder;
import at.petrak.hexcasting.api.misc.DiscoveryHandlers;
import at.petrak.hexcasting.api.utils.ManaHelper;
import at.petrak.hexcasting.common.items.magic.DebugUnlockerHolder;
import at.petrak.hexcasting.common.items.magic.ItemCreativeUnlocker;
import at.petrak.hexcasting.common.lib.HexItems;
import at.petrak.hexcasting.interop.HexInterop;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
import com.google.common.collect.Lists;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
@ -19,6 +19,7 @@ import top.theillusivec4.curios.api.type.inventory.ICurioStacksHandler;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class CuriosApiInterop {
@ -61,22 +62,21 @@ public class CuriosApiInterop {
return holders;
});
DiscoveryHandlers.addManaHolderDiscoverer(harness -> {
List<ManaHolder> holders = Lists.newArrayList();
harness.getCtx().getCaster().getCapability(CuriosCapability.INVENTORY).ifPresent(handler -> {
DiscoveryHandlers.addDebugItemDiscoverer((player, type) -> {
AtomicReference<ItemStack> result = new AtomicReference<>(ItemStack.EMPTY);
player.getCapability(CuriosCapability.INVENTORY).ifPresent(handler -> {
for (var stacksHandler : handler.getCurios().values()) {
var stacks = stacksHandler.getStacks();
for (int i = 0; i < stacks.getSlots(); i++) {
var stack = stacks.getStackInSlot(i);
if (ItemCreativeUnlocker.isDebug(stack)) {
holders.add(new DebugUnlockerHolder(stack));
if (ItemCreativeUnlocker.isDebug(stack, type)) {
result.set(stack);
return;
}
}
}
});
return holders;
return result.get();
});
}