Skip to content

Commit

Permalink
Reset variant on logout
Browse files Browse the repository at this point in the history
  • Loading branch information
Vazkii committed Oct 3, 2023
1 parent baa3ac2 commit 7072c96
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import vazkii.quark.base.network.QuarkNetwork;
import vazkii.quark.base.network.message.experimental.PlaceVariantUpdateMessage;
import vazkii.quark.content.experimental.module.VariantSelectorModule;

public class VariantSelectorScreen extends Screen {
Expand Down Expand Up @@ -178,8 +176,7 @@ public void tick() {

if(slotSelected != -1) {
String variant = slotSelected == 0 ? "" : variants.get(slotSelected - 1);
VariantSelectorModule.setClientVariant(variant);
QuarkNetwork.sendToServer(new PlaceVariantUpdateMessage(variant));
VariantSelectorModule.setClientVariant(variant, true);
mc.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package vazkii.quark.content.experimental.module;

import java.util.Arrays;

import org.lwjgl.glfw.GLFW;

import com.mojang.blaze3d.platform.Window;

import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
Expand All @@ -12,6 +17,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
Expand All @@ -22,28 +28,29 @@
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.event.TickEvent.ClientTickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.lwjgl.glfw.GLFW;
import vazkii.quark.base.Quark;
import vazkii.quark.base.client.handler.ModKeybindHandler;
import vazkii.quark.base.module.LoadModule;
import vazkii.quark.base.module.ModuleCategory;
import vazkii.quark.base.module.QuarkModule;
import vazkii.quark.base.module.config.Config;
import vazkii.quark.base.network.QuarkNetwork;
import vazkii.quark.base.network.message.experimental.PlaceVariantUpdateMessage;
import vazkii.quark.content.experimental.client.screen.VariantSelectorScreen;
import vazkii.quark.content.experimental.config.BlockSuffixConfig;
import vazkii.quark.content.experimental.item.HammerItem;

import java.util.Arrays;

@LoadModule(category = ModuleCategory.EXPERIMENTAL, hasSubscriptions = true, enabledByDefault = false,
description = "Allows placing variant blocks automatically via a selector menu, can also disable all variant block recipes and items")
public class VariantSelectorModule extends QuarkModule {

private static final String TAG_CURRENT_VARIANT = Quark.MOD_ID + ":CurrentSelectedVariant";

private static String clientVariant = "";
private static String clientVariant = null;
private static boolean staticEnabled;

@Config(description = "Set this to true to automatically convert any dropped variant items into their originals. Do this ONLY if you intend to take control of every recipe via a data pack or equivalent, as this will introduce dupes otherwise.")
Expand Down Expand Up @@ -84,7 +91,6 @@ public static String getSavedVariant(Player player) {
return player.getPersistentData().getString(TAG_CURRENT_VARIANT);
}

// TODO on login player should lose variant
public static void setSavedVariant(ServerPlayer player, String variant) {
if(variant == null)
variant = "";
Expand All @@ -94,8 +100,14 @@ public static void setSavedVariant(ServerPlayer player, String variant) {
}

@OnlyIn(Dist.CLIENT)
public static void setClientVariant(String variant) {
public static void setClientVariant(String variant, boolean sync) {
clientVariant = variant;

if(sync) {
if(variant == null)
variant = "";
QuarkNetwork.sendToServer(new PlaceVariantUpdateMessage(variant));
}
}

private static Block getMainHandVariantBlock(Player player, String variant) {
Expand Down Expand Up @@ -149,6 +161,22 @@ public void keystroke(Key event) {
}
}
}

@SubscribeEvent
@OnlyIn(Dist.CLIENT)
public void clientTick(ClientTickEvent event) {
if(event.phase != Phase.END)
return;

Minecraft mc = Minecraft.getInstance();
Level level = mc.level;
if(level == null)
setClientVariant(null, false);
else {
if(clientVariant == null)
setClientVariant("", true);
}
}

@SubscribeEvent
public void addEntityToWorld(EntityJoinLevelEvent event) {
Expand Down

0 comments on commit 7072c96

Please sign in to comment.