From cf9327b08652f60a02e38315925150d9e8e9b07f Mon Sep 17 00:00:00 2001 From: quaternary Date: Thu, 9 Nov 2023 22:56:59 -0500 Subject: [PATCH] Variant selector cherrypicks (#4411) * Manual cherry-pick of 299645cf134f76ddf086491195abdc96068d8cb4 (fix #4383) * Manual cherry-pick of 299645cf134f76ddf086491195abdc96068d8cb4 (fix #4383) * Manual cherry-pick of d6289bd11e616ed921b6c33f33df1d1f28b33f5b (this makes sense) * Manual cherry-pick of fec4889bdbef06180d97c283f51365a3ad2c2284 (Add offset config to variant) * Manual cherry-pick of 4c8621d16f8a6904f967cc3abfc4fe0db6dbe5b6 (Better suffix config and saner defaults) * Manual cherry-pick of 6235e358ad7ceaa3300cac1c6247d25d97a6cee0 (Fix #4391) * Manual cherry-pick of 9a9850e65301973fb3c243226f333de3f4093adf (Fix #4392) --- .../client/screen/VariantSelectorScreen.java | 33 +++++++----- ...kSuffixConfig.java => VariantsConfig.java} | 50 ++++++++++++------- .../content/experimental/item/HammerItem.java | 41 +++++++++------ .../module/VariantSelectorModule.java | 27 ++++------ 4 files changed, 86 insertions(+), 65 deletions(-) rename src/main/java/org/violetmoon/quark/content/experimental/config/{BlockSuffixConfig.java => VariantsConfig.java} (82%) diff --git a/src/main/java/org/violetmoon/quark/content/experimental/client/screen/VariantSelectorScreen.java b/src/main/java/org/violetmoon/quark/content/experimental/client/screen/VariantSelectorScreen.java index 65fef65b66..13684273d1 100644 --- a/src/main/java/org/violetmoon/quark/content/experimental/client/screen/VariantSelectorScreen.java +++ b/src/main/java/org/violetmoon/quark/content/experimental/client/screen/VariantSelectorScreen.java @@ -22,18 +22,20 @@ import java.util.ArrayList; import java.util.List; +import javax.annotation.Nonnull; + public class VariantSelectorScreen extends Screen { - float timeIn = 0; - int slotSelected = -1; + private float timeIn = 0; + private int slotSelected = -1; - final Minecraft mc; - final ItemStack stack; - final KeyMapping key; - final String currentVariant; - final List variants; + private final Minecraft mc; + private final ItemStack stack; + private final KeyMapping key; + private final String currentVariant; + private final List variants; - final List drawStacks = new ArrayList<>(); + private final List drawStacks = new ArrayList<>(); public VariantSelectorScreen(ItemStack stack, KeyMapping key, String currentVariant, List variants) { super(Component.empty()); @@ -45,7 +47,7 @@ public VariantSelectorScreen(ItemStack stack, KeyMapping key, String currentVari } @Override - public void render(PoseStack ms, int mx, int my, float delta) { + public void render(@Nonnull PoseStack ms, int mx, int my, float delta) { super.render(ms, mx, my, delta); timeIn += delta; @@ -99,6 +101,9 @@ public void render(PoseStack ms, int mx, int my, float delta) { if(mouseInSector || rightVariant) radius *= 1.1f; + if(!variantExists) + radius *= 0.9f; + int gs = 0x39; if(seg % 2 == 0) gs += 0x29; @@ -106,11 +111,15 @@ public void render(PoseStack ms, int mx, int my, float delta) { int r = gs; int g = gs ; int b = gs; - int a = 0x33; + int a = 0x44; if(variantExists) { g += 0x22; a = 0x99; + } else { + r /= 4; + g /= 4; + b /= 4; } if(seg == 0) @@ -151,9 +160,7 @@ else if(rightVariant) { RenderSystem.blendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0); for(DrawStack ds : drawStacks) { - if(ds.stack().isEmpty()) - mc.font.draw(ms, "?", ds.x() + 6, ds.y() + 3, 0x99FFFFFF); - else + if(!ds.stack().isEmpty()) mc.getItemRenderer().renderGuiItem(ds.stack(), ds.x(), ds.y()); } RenderSystem.disableBlend(); diff --git a/src/main/java/org/violetmoon/quark/content/experimental/config/BlockSuffixConfig.java b/src/main/java/org/violetmoon/quark/content/experimental/config/VariantsConfig.java similarity index 82% rename from src/main/java/org/violetmoon/quark/content/experimental/config/BlockSuffixConfig.java rename to src/main/java/org/violetmoon/quark/content/experimental/config/VariantsConfig.java index c0c904d17e..e5d164eda8 100644 --- a/src/main/java/org/violetmoon/quark/content/experimental/config/BlockSuffixConfig.java +++ b/src/main/java/org/violetmoon/quark/content/experimental/config/VariantsConfig.java @@ -22,32 +22,35 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; -public class BlockSuffixConfig implements IConfigType { +public class VariantsConfig implements IConfigType { private static final VariantMap EMPTY_VARIANT_MAP = new VariantMap(new HashMap<>()); @Config(description = "The list of all variant types available for players to use. Values are treated as suffixes to block IDs for scanning.\n" + "Prefix any variant type with ! to make it show up for Manual Variants but not be automatically scanned for. (e.g. '!polish')") - private List variantTypes; + private List variantTypes = Arrays.asList("slab", "stairs", "wall", "fence", "fence_gate", "vertical_slab"); @Config(description = "By default, only a mod's namespace is scanned for variants for its items (e.g. if coolmod adds coolmod:fun_block, it'll search only for coolmod:fun_block_stairs).\n" + " Mods in this list are also scanned for variants if none are found in itself (e.g. if quark is in the list and coolmod:fun_block_stairs doesn't exist, it'll try to look for quark:fun_block_stairs next)") - private List testedMods; + private List testedMods = Arrays.asList("quark"); @Config private boolean printVariantMapToLog = false; @Config(description = "Format is 'alias=original' in each value (e.g. 'wall=fence' means that a failed search for, minecraft:cobblestone_fence will try cobblestone_wall next)") - private List aliases; + private List aliases = Arrays.asList("carpet=slab", "pane=fence"); @Config(description = "Ends of block IDs to try and remove when looking for variants. (e.g. minecraft:oak_planks goes into minecraft:oak_stairs, so we have to include '_planks' in this list for it to find them or else it'll only look for minecraft:oak_planks_stairs)") - private List stripCandidates = Arrays.asList("_planks", "_wool", "s"); + private List stripCandidates = Arrays.asList("_planks", "_wool", "_block", "s"); @Config(description = "Add manual variant overrides here, the format is 'type,block,output' (e.g. polish,minecraft:stone_bricks,minecraft:chiseled_stone_bricks). The type must be listed in Variant Types") private List manualVariants = new ArrayList<>(); - - @Config - private List blacklist = new ArrayList<>(); + + @Config(description = " A list of block IDs and mappings to be excluded from variant selection.\n" + + "To exclude a block from being turned into other blocks, just include the block ID (e.g. minecraft:cobblestone).\n" + + "To exclude a block from having other blocks turned into it, suffix it with = (e.g. =minecraft:cobblestone_stairs)\n" + + "To exclude a specific block->variant combination, put = between the two (e.g. minecraft:cobblestone=minecraft:cobblestone_stairs)") + private List blacklist = Arrays.asList("minecraft:snow", "minecraft:bamboo", "quark:bamboo_block"); private Map blockVariants = new HashMap<>(); private Map originals = new HashMap<>(); @@ -57,11 +60,7 @@ public class BlockSuffixConfig implements IConfigType { private List visibleVariants = new ArrayList<>(); private List sortedSuffixes; - public BlockSuffixConfig(List variantTypes, List testedMods, List aliases) { - this.variantTypes = variantTypes; - this.testedMods = testedMods; - this.aliases = aliases; - } + public VariantsConfig() { } @Override public void onReload(ZetaModule module, ConfigFlagManager flagManager) { @@ -153,11 +152,15 @@ public Collection getAllVariants(Block block) { } public Block getOriginalBlock(Block block) { - return originals.containsKey(block) ? originals.get(block) : block; + return originals.getOrDefault(block, block); + } + + public boolean isOriginal(Block block) { + return originals.containsValue(block); } public boolean isVariant(Block block) { - return blockVariants.containsKey(block) && !blockVariants.get(block).isEmpty(); + return originals.containsKey(block); } private VariantMap getVariants(Block block) { @@ -166,13 +169,13 @@ private VariantMap getVariants(Block block) { Map newVariants = new HashMap<>(); - if(!isBlacklisted(block)) + if(!isBlacklisted(block, null)) for(String s : sortedSuffixes) { if(!variantTypes.contains(s)) continue; // this means its marked with ! so it won't be searched Block suffixed = getSuffixedBlock(block, s); - if(suffixed != null && !isBlacklisted(block)) { + if(suffixed != null && !isBlacklisted(null, suffixed) && !isBlacklisted(block, suffixed)) { newVariants.put(s, suffixed); originals.put(suffixed, block); } @@ -236,8 +239,17 @@ private Block getSuffixedBlock(String namespace, String name, String suffix) { return ret; } - private boolean isBlacklisted(Block block) { - return !blacklist.isEmpty() && blacklist.contains(Registry.BLOCK.getKey(block).toString()); + private boolean isBlacklisted(Block block, Block result) { + if(blacklist.isEmpty()) + return false; + + String search = ""; + if(block != null) + search += Registry.BLOCK.getKey(block).toString(); + if(result != null) + search += ("=" + Registry.BLOCK.getKey(result).toString()); + + return !search.isEmpty() && blacklist.contains(search); } public boolean isKnownVariant(String variant) { diff --git a/src/main/java/org/violetmoon/quark/content/experimental/item/HammerItem.java b/src/main/java/org/violetmoon/quark/content/experimental/item/HammerItem.java index 2240b7ab55..0a776b2860 100644 --- a/src/main/java/org/violetmoon/quark/content/experimental/item/HammerItem.java +++ b/src/main/java/org/violetmoon/quark/content/experimental/item/HammerItem.java @@ -35,23 +35,30 @@ public InteractionResult useOn(UseOnContext context) { BlockPos pos = context.getClickedPos(); BlockState state = level.getBlockState(pos); Block block = state.getBlock(); - - String variant = VariantSelectorModule.getSavedVariant(player); - Block variantBlock = VariantSelectorModule.getVariantOrOriginal(block, variant); - if(variantBlock != null) { - BlockPlaceContext bpc = new YungsBetterBlockPlaceContext(context); - BlockState place = variantBlock.getStateForPlacement(bpc); - place = LockRotationModule.fixBlockRotation(place, bpc); - - if(!place.equals(state) && !level.isClientSide) { + + if(player != null) { + String variant = VariantSelectorModule.getSavedVariant(player); + Block variantBlock = VariantSelectorModule.getVariantOrOriginal(block, variant); + if(variantBlock != null) { level.removeBlock(pos, false); - level.setBlock(pos, place, 1 | 2); - player.swing(context.getHand()); - - level.playSound(null, pos, variantBlock.getSoundType(place).getPlaceSound(), SoundSource.BLOCKS, 1.0F, 1.0F); + + BlockPlaceContext bpc = new YungsBetterBlockPlaceContext(context); + BlockState place = variantBlock.getStateForPlacement(bpc); + + place = LockRotationModule.fixBlockRotation(place, bpc); + + if(place != null && !place.equals(state) && !level.isClientSide) { + level.removeBlock(pos, false); + level.setBlock(pos, place, 1 | 2); + player.swing(context.getHand()); + + level.playSound(null, pos, place.getSoundType().getPlaceSound(), SoundSource.BLOCKS, 1.0F, 1.0F); + } else { + level.setBlock(pos, state, 0); + } + + return InteractionResult.SUCCESS; } - - return InteractionResult.SUCCESS; } return InteractionResult.PASS; @@ -66,10 +73,12 @@ public YungsBetterBlockPlaceContext(UseOnContext ctx) { // vanilla BlockPlaceContext offsets the original clicked pos if replaceClicked is false // so that the block is placed on the edge, but in this case we want to place it in the // same blockpos that was clicked so we do this nonsense - + + @Nonnull @Override public BlockPos getClickedPos() { boolean oldRepl = replaceClicked; + replaceClicked = true; BlockPos pos = super.getClickedPos(); diff --git a/src/main/java/org/violetmoon/quark/content/experimental/module/VariantSelectorModule.java b/src/main/java/org/violetmoon/quark/content/experimental/module/VariantSelectorModule.java index 1dd40a40c8..a5b7f07c7f 100644 --- a/src/main/java/org/violetmoon/quark/content/experimental/module/VariantSelectorModule.java +++ b/src/main/java/org/violetmoon/quark/content/experimental/module/VariantSelectorModule.java @@ -40,7 +40,7 @@ import org.violetmoon.quark.base.network.message.experimental.PlaceVariantUpdateMessage; import org.violetmoon.quark.content.experimental.client.screen.VariantSelectorScreen; import org.violetmoon.quark.content.experimental.client.tooltip.VariantsComponent; -import org.violetmoon.quark.content.experimental.config.BlockSuffixConfig; +import org.violetmoon.quark.content.experimental.config.VariantsConfig; import org.violetmoon.quark.content.experimental.item.HammerItem; import org.violetmoon.zeta.client.event.load.ZKeyMapping; import org.violetmoon.zeta.client.event.load.ZTooltipComponents; @@ -80,13 +80,11 @@ public class VariantSelectorModule extends ZetaModule { @Config public static boolean showHud = true; @Config public static boolean enableGreenTint = true; @Config public static boolean overrideHeldItemRender = true; + @Config public static int hudOffsetX = 0; + @Config public static int hudOffsetY = 0; @Config - public static BlockSuffixConfig variants = new BlockSuffixConfig( - Arrays.asList("slab", "stairs", "wall", "fence", "fence_gate", "vertical_slab"), - Arrays.asList("quark"), - Arrays.asList("carpet=slab") - ); + public static VariantsConfig variants = new VariantsConfig(); public static Item hammer; @@ -126,14 +124,13 @@ private static Block getMainHandVariantBlock(Player player, String variant) { } public static Block getVariantForBlock(Block block, String variant) { - Block variantBlock = variants.getBlockForVariant(block, variant); - if(variantBlock != null) - return variantBlock; - - return null; + return variants.getBlockForVariant(block, variant); } public static Block getVariantOrOriginal(Block block, String variant) { + if(!variants.isVariant(block) && !variants.isOriginal(block)) + return null; + block = variants.getOriginalBlock(block); if(variant == null || variant.isEmpty()) @@ -317,10 +314,6 @@ public void onRender(ZRenderGuiOverlay.Crosshair.Pre event) { int x = window.getGuiScaledWidth() / 2; int y = window.getGuiScaledHeight() / 2 + 12; - - showSimpleHud = true; - alignHudToHotbar = true; - if(alignHudToHotbar) { HumanoidArm arm = mc.options.mainHand().get(); if(arm == HumanoidArm.RIGHT) @@ -335,8 +328,8 @@ public void onRender(ZRenderGuiOverlay.Crosshair.Pre event) { displayLeft.setCount(1); - int posX = x - offset - width; - int posY = y; + int posX = x - offset - width + hudOffsetX; + int posY = y + hudOffsetY; if(!showSimpleHud) { mc.getItemRenderer().renderAndDecorateItem(displayLeft, posX, posY);