Skip to content

Commit 653bb02

Browse files
committed
Add visuals to Anshar compat; fix bugs with it
1 parent b6f0078 commit 653bb02

11 files changed

Lines changed: 190 additions & 20 deletions

File tree

src/main/java/falseresync/lib/client/BetterDrawContext.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package falseresync.lib.client;
22

3-
import com.mojang.blaze3d.systems.*;
4-
import net.minecraft.client.*;
5-
import net.minecraft.client.gui.*;
3+
import com.mojang.blaze3d.systems.RenderSystem;
4+
import net.minecraft.client.MinecraftClient;
5+
import net.minecraft.client.gui.DrawContext;
66
import net.minecraft.client.render.*;
7-
import net.minecraft.util.*;
8-
import org.joml.*;
7+
import net.minecraft.util.Identifier;
8+
import org.joml.Matrix4f;
99

1010
public class BetterDrawContext extends DrawContext {
1111
public BetterDrawContext(MinecraftClient client, DrawContext context) {

src/main/java/falseresync/vivatech/client/ToolManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void onKeyPressed(MinecraftClient client, ClientPlayerEntity player) {
8585
}
8686

8787
@Nullable
88-
private ItemStack scanInventoryForGadgets(PlayerInventory inventory) {
88+
public static ItemStack scanInventoryForGadgets(PlayerInventory inventory) {
8989
var gadgetStack = inventory.getMainHandStack();
9090
return gadgetStack.isIn(VivatechItemTags.GADGETS) ? gadgetStack : null;
9191
}

src/main/java/falseresync/vivatech/client/VivatechClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import falseresync.vivatech.common.config.TranslatableEnum;
1212
import falseresync.vivatech.common.config.TranslatableEnumGuiProvider;
1313
import falseresync.vivatech.common.config.VivatechConfig;
14+
import falseresync.vivatech.compat.anshar.AnsharCompatClient;
1415
import falseresync.vivatech.network.VivatechClientReceivers;
1516
import me.shedaniel.autoconfig.AutoConfig;
1617
import net.fabricmc.api.ClientModInitializer;
1718
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
1819
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
20+
import net.fabricmc.loader.api.FabricLoader;
1921
import org.slf4j.LoggerFactory;
2022

2123
public class VivatechClient implements ClientModInitializer {
@@ -43,6 +45,10 @@ public void onInitializeClient() {
4345
clientWireManager = new ClientWireManager(client);
4446
hud = new VivatechHud(client);
4547
toolManager = new ToolManager();
48+
49+
if (FabricLoader.getInstance().isModLoaded("anshar")) {
50+
AnsharCompatClient.init(client);
51+
}
4652
});
4753

4854
ClientTickEvents.END_CLIENT_TICK.register(client -> {

src/main/java/falseresync/vivatech/common/ChargeManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public boolean cannotAddAnyCharge(ItemStack stack, PlayerEntity player) {
7373
return isGadgetFullyCharged(stack);// && areShellsFull(player);
7474
}
7575

76+
public boolean hasEnoughCharge(ItemStack stack, int cost, @Nullable PlayerEntity user) {
77+
if (user != null && (user.isCreative() && Vivatech.getConfig().infiniteCharge.isCreativeOnly() || Vivatech.getConfig().infiniteCharge.isAlways())) {
78+
return true;
79+
}
80+
return stack.getOrDefault(VivatechComponents.CHARGE, 0) >= cost;
81+
}
82+
83+
// This should not use hasEnoughCharge, as that doesn't bypass creative and makes a whoopsie
7684
public boolean tryExpendGadgetCharge(ItemStack stack, int cost, @Nullable PlayerEntity user) {
7785
if (user != null && (user.isCreative() && Vivatech.getConfig().infiniteCharge.isCreativeOnly() || Vivatech.getConfig().infiniteCharge.isAlways())) {
7886
return true;

src/main/java/falseresync/vivatech/common/data/ItemBarComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import net.minecraft.util.dynamic.Codecs;
99

1010
public record ItemBarComponent(int step, int color) {
11-
public static final ItemBarComponent DEFAULT = new ItemBarComponent(0, 0);
11+
public static final ItemBarComponent DEFAULT = new ItemBarComponent(0, -1);
1212
public static final Codec<ItemBarComponent> CODEC = RecordCodecBuilder.create(instance -> instance.group(
1313
Codec.intRange(0, 13).fieldOf("step").forGetter(ItemBarComponent::step),
1414
Codecs.ARGB.fieldOf("color").forGetter(ItemBarComponent::color)

src/main/java/falseresync/vivatech/common/data/VivatechComponents.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class VivatechComponents {
2222
ComponentType.<ItemBarComponent>builder().codec(ItemBarComponent.CODEC).packetCodec(ItemBarComponent.PACKET_CODEC).build();
2323
public static final @RegistryObject ComponentType<Boolean> IN_USE =
2424
ComponentType.<Boolean>builder().codec(Codec.BOOL).packetCodec(PacketCodecs.BOOL).build();
25+
public static final @RegistryObject ComponentType<Boolean> TOOLTIP_OVERRIDDEN =
26+
ComponentType.<Boolean>builder().codec(Codec.BOOL).packetCodec(PacketCodecs.BOOL).build();
2527
public static final @RegistryObject ComponentType<InventoryComponent> INVENTORY =
2628
ComponentType.<InventoryComponent>builder().codec(InventoryComponent.CODEC).packetCodec(InventoryComponent.PACKET_CODEC).build();
2729
public static final @RegistryObject ComponentType<Integer> INVENTORY_SIZE =

src/main/java/falseresync/vivatech/common/item/GadgetItem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ public int getItemBarColor(ItemStack stack) {
274274
var focusStack = getEquipped(stack);
275275
if (!focusStack.isEmpty() && focusStack.getItem() instanceof FocusItem focusItem) {
276276
for (Focus behaviorExtension : focusItem.getBehaviorExtensions()) {
277-
var step = behaviorExtension.focusGetItemBarStep(stack, focusStack);
278-
if (step > 0) {
279-
return step;
277+
var color = behaviorExtension.focusGetItemBarStep(stack, focusStack);
278+
if (color >= 0) {
279+
return color;
280280
}
281281
}
282282
return focusItem.focusGetItemBarColor(stack, focusStack);

src/main/java/falseresync/vivatech/common/item/focus/CometWarpFocusItem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public boolean focusHasGlintSelf(ItemStack stack) {
106106

107107
@Override
108108
public void focusAppendTooltip(ItemStack gadgetStack, ItemStack focusStack, TooltipContext context, List<Text> tooltip, TooltipType type) {
109+
if (focusStack.contains(VivatechComponents.TOOLTIP_OVERRIDDEN)) {
110+
return;
111+
}
109112
var anchor = gadgetStack.get(VivatechComponents.WARP_FOCUS_ANCHOR);
110113
if (anchor == null) {
111114
tooltip.add(Text.translatable("tooltip.vivatech.gadget.setup_anchor")

src/main/java/falseresync/vivatech/compat/anshar/AnsharCompat.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
import com.lgmrszd.anshar.frequency.FrequencyNetwork;
55
import com.lgmrszd.anshar.frequency.NetworkManagerComponent;
66
import com.lgmrszd.anshar.transport.PlayerTransportComponent;
7+
import falseresync.vivatech.common.Reports;
8+
import falseresync.vivatech.common.Vivatech;
79
import falseresync.vivatech.common.data.VivatechComponents;
810
import falseresync.vivatech.common.item.focus.Focus;
911
import net.minecraft.component.ComponentType;
12+
import net.minecraft.entity.EquipmentSlot;
1013
import net.minecraft.entity.player.PlayerEntity;
1114
import net.minecraft.item.Item;
1215
import net.minecraft.item.ItemStack;
1316
import net.minecraft.item.ItemUsageContext;
1417
import net.minecraft.item.tooltip.TooltipType;
1518
import net.minecraft.registry.Registries;
1619
import net.minecraft.registry.Registry;
20+
import net.minecraft.sound.SoundCategory;
21+
import net.minecraft.sound.SoundEvents;
1722
import net.minecraft.text.Text;
1823
import net.minecraft.util.*;
1924
import net.minecraft.world.World;
@@ -24,6 +29,7 @@
2429
import static falseresync.vivatech.common.Vivatech.vtId;
2530

2631
public class AnsharCompat implements Focus {
32+
public static final int DEFAULT_COST = 20;
2733
public static final ComponentType<UUID> NETWORK_UUID =
2834
ComponentType.<UUID>builder().codec(Uuids.INT_STREAM_CODEC).packetCodec(Uuids.PACKET_CODEC).build();
2935

@@ -47,6 +53,10 @@ public void focusOnUnequipped(ItemStack gadgetStack, ItemStack focusStack, Playe
4753

4854
@Override
4955
public ActionResult focusUseOnBlock(ItemStack gadgetStack, ItemStack focusStack, ItemUsageContext context) {
56+
if (gadgetStack.contains(NETWORK_UUID)) {
57+
return ActionResult.FAIL;
58+
}
59+
5060
if (!context.getWorld().isClient) {
5161
var networkUuid = IBeaconComponent.KEY.maybeGet(context.getWorld().getBlockEntity(context.getBlockPos()))
5262
.flatMap(IBeaconComponent::getFrequencyNetwork)
@@ -56,32 +66,47 @@ public ActionResult focusUseOnBlock(ItemStack gadgetStack, ItemStack focusStack,
5666
}
5767

5868
gadgetStack.set(NETWORK_UUID, networkUuid.get());
69+
focusStack.set(VivatechComponents.TOOLTIP_OVERRIDDEN, true); // overrides tooltip
70+
gadgetStack.remove(VivatechComponents.WARP_FOCUS_ANCHOR);
5971
return ActionResult.SUCCESS;
6072
}
73+
6174
return ActionResult.CONSUME;
6275
}
6376

6477
@Override
6578
public TypedActionResult<ItemStack> focusUse(ItemStack gadgetStack, ItemStack focusStack, World world, PlayerEntity user, Hand hand) {
66-
if (!world.isClient) {
67-
var networkUuid = gadgetStack.get(NETWORK_UUID);
68-
if (networkUuid == null) {
69-
return TypedActionResult.pass(gadgetStack);
70-
}
79+
if (user.isSneaking()) {
80+
return TypedActionResult.pass(gadgetStack);
81+
}
7182

83+
var networkUuid = gadgetStack.get(NETWORK_UUID);
84+
if (networkUuid == null) {
85+
return TypedActionResult.pass(gadgetStack);
86+
}
87+
88+
if (!world.isClient) {
7289
var transport = PlayerTransportComponent.KEY.get(user);
7390
if (transport.isInNetwork()) {
74-
return TypedActionResult.pass(gadgetStack);
91+
return TypedActionResult.fail(gadgetStack);
7592
}
7693

7794
var network = NetworkManagerComponent.KEY.get(world.getLevelProperties()).getNetwork(networkUuid);
7895
if (network.isEmpty()) {
7996
return TypedActionResult.fail(gadgetStack);
8097
}
8198

99+
if (!Vivatech.getChargeManager().tryExpendGadgetCharge(gadgetStack, DEFAULT_COST, user)) {
100+
Reports.insufficientCharge(user);
101+
return TypedActionResult.fail(gadgetStack);
102+
}
103+
104+
user.playSoundToPlayer(SoundEvents.ENTITY_PLAYER_TELEPORT, SoundCategory.PLAYERS, 1f, 1f);
105+
focusStack.damage(1, user, EquipmentSlot.MAINHAND);
82106
transport.enterNetwork(network.get(), user.getBlockPos());
107+
return TypedActionResult.success(gadgetStack);
83108
}
84-
return TypedActionResult.success(gadgetStack);
109+
return TypedActionResult.consume(gadgetStack);
85110
}
86111

87112
@Override
@@ -96,14 +121,14 @@ public boolean focusHasGlintSelf(ItemStack focusStack) {
96121

97122
@Override
98123
public void focusAppendTooltip(ItemStack gadgetStack, ItemStack focusStack, Item.TooltipContext context, List<Text> tooltip, TooltipType type) {
99-
focusAppendTooltipSelf(focusStack, context, tooltip, type);
124+
focusAppendTooltipSelf(gadgetStack, context, tooltip, type);
100125
}
101126

102127
@Override
103128
public void focusAppendTooltipSelf(ItemStack stack, Item.TooltipContext context, List<Text> tooltip, TooltipType type) {
104129
var networkUuid = stack.get(NETWORK_UUID);
105130
if (networkUuid != null) {
106-
tooltip.add(Text.translatable("tooltip.vivatech.gadget.anshar_compat").formatted(Formatting.GRAY));
131+
tooltip.add(Text.translatable("tooltip.vivatech.gadget.anshar_compat.bound").formatted(Formatting.GRAY));
107132
}
108133
}
109134
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package falseresync.vivatech.compat.anshar;
2+
3+
import com.mojang.blaze3d.systems.RenderSystem;
4+
import falseresync.lib.client.BetterDrawContext;
5+
import falseresync.vivatech.client.ToolManager;
6+
import falseresync.vivatech.client.hud.HudItem;
7+
import falseresync.vivatech.common.Vivatech;
8+
import falseresync.vivatech.common.item.VivatechItems;
9+
import net.fabricmc.api.EnvType;
10+
import net.fabricmc.api.Environment;
11+
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
12+
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
13+
import net.minecraft.client.MinecraftClient;
14+
import net.minecraft.client.render.RenderTickCounter;
15+
import net.minecraft.client.texture.Sprite;
16+
import net.minecraft.client.texture.SpriteAtlasTexture;
17+
import net.minecraft.client.util.SpriteIdentifier;
18+
import net.minecraft.client.util.math.MatrixStack;
19+
import net.minecraft.text.Text;
20+
import net.minecraft.util.Identifier;
21+
import net.minecraft.util.math.MathHelper;
22+
import org.joml.Matrix4f;
23+
import org.joml.Quaternionf;
24+
25+
import static falseresync.vivatech.common.Vivatech.vtId;
26+
27+
@Environment(EnvType.CLIENT)
28+
public class AnsharCompatClient implements HudItem {
29+
private static final Identifier BASE_TEX = vtId("textures/world/comet_warp_beacon.png");
30+
private static final SpriteIdentifier CROWN_ID = new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, vtId("world/comet_warp_beacon_crown"));
31+
private Sprite CROWN_SPRITE;
32+
public static AnsharCompatClient INSTANCE;
33+
private final MinecraftClient client;
34+
private boolean hidden = true;
35+
private int overlayMessageCooldown = 0;
36+
37+
public AnsharCompatClient(MinecraftClient client) {
38+
this.client = client;
39+
}
40+
41+
public static void init(MinecraftClient client) {
42+
INSTANCE = new AnsharCompatClient(client);
43+
44+
HudRenderCallback.EVENT.register((vanillaContext, tickCounter) -> {
45+
var context = new BetterDrawContext(client, vanillaContext);
46+
INSTANCE.render(context, tickCounter);
47+
});
48+
49+
ClientTickEvents.START_WORLD_TICK.register(world -> {
50+
if (client.isPaused()) return;
51+
52+
INSTANCE.tick();
53+
});
54+
}
55+
56+
@Override
57+
public void render(BetterDrawContext context, RenderTickCounter tickCounter) {
58+
if (hidden) {
59+
return;
60+
}
61+
62+
if (CROWN_SPRITE == null) {
63+
CROWN_SPRITE = CROWN_ID.getSprite();
64+
}
65+
66+
RenderSystem.enableBlend();
67+
68+
var matrices = context.getMatrices();
69+
matrices.push();
70+
71+
var centerX = context.getScaledWindowWidth() / 2f;
72+
var centerY = context.getScaledWindowHeight() / 2f;
73+
drawRing(12, 1f, 100, context, tickCounter, matrices, centerX, centerY);
74+
drawRing(12, 1.5f, -75, context, tickCounter, matrices, centerX, centerY);
75+
drawRing(18, 2f, 60, context, tickCounter, matrices, centerX, centerY);
76+
drawRing(24, 3f, -45, context, tickCounter, matrices, centerX, centerY);
77+
drawRing(28, 4f, 35, context, tickCounter, matrices, centerX, centerY);
78+
79+
matrices.pop();
80+
81+
RenderSystem.disableBlend();
82+
}
83+
84+
private void drawRing(int tilesNo, float scale, float speedInverse, BetterDrawContext context, RenderTickCounter tickCounter, MatrixStack matrices, float centerX, float centerY) {
85+
matrices.push();
86+
matrices.multiplyPositionMatrix(new Matrix4f()
87+
.scaleAround(scale, scale, 1, centerX, centerY, 0)
88+
.rotateAround(new Quaternionf().rotateZ((client.world.getTime() + tickCounter.getTickDelta(false)) / speedInverse), centerX, centerY, 0));
89+
90+
for (int i = 0; i < tilesNo; i++) {
91+
matrices.multiply(new Quaternionf().rotateZ(MathHelper.PI / tilesNo * 2), centerX, centerY, 0);
92+
context.drawNonDiscreteRect(BASE_TEX, centerX - 50, centerY - 50, 16, 16);
93+
}
94+
matrices.pop();
95+
}
96+
97+
@Override
98+
public void tick() {
99+
if (client.player == null || client.world == null) {
100+
hidden = true;
101+
overlayMessageCooldown = 0;
102+
return;
103+
}
104+
105+
var gadgetStack = ToolManager.scanInventoryForGadgets(client.player.getInventory());
106+
if (gadgetStack == null) {
107+
hidden = true;
108+
overlayMessageCooldown = 0;
109+
return;
110+
}
111+
112+
hidden = !(gadgetStack.contains(AnsharCompat.NETWORK_UUID) && Vivatech.getChargeManager().hasEnoughCharge(gadgetStack, AnsharCompat.DEFAULT_COST, client.player));
113+
114+
if (!hidden) {
115+
if (overlayMessageCooldown == 0) {
116+
client.inGameHud.setOverlayMessage(Text.translatable("tooltip.vivatech.gadget.anshar_compat.enter"), true);
117+
overlayMessageCooldown = 40;
118+
} else {
119+
overlayMessageCooldown -= 1;
120+
}
121+
} else {
122+
overlayMessageCooldown = 0;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)