ConstructionWand/src/main/java/thetadev/constructionwand/client/ClientEvents.java
Theta-Dev bb3d36fa56 BIG Refactoring.
Modular WandActions and WandSuppliers.
Wand Cores and Wand Reservoirs can be added to your wand, they determine which action and supplier gets used.
2021-02-14 02:20:53 +01:00

103 lines
3.8 KiB
Java

package thetadev.constructionwand.client;
import net.minecraft.client.Minecraft;
import net.minecraft.client.util.InputMappings;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import thetadev.constructionwand.ConstructionWand;
import thetadev.constructionwand.basics.ConfigClient;
import thetadev.constructionwand.basics.WandUtil;
import thetadev.constructionwand.basics.option.WandOptions;
import thetadev.constructionwand.items.wand.ItemWand;
import thetadev.constructionwand.network.PacketQueryUndo;
import thetadev.constructionwand.network.PacketWandOption;
public class ClientEvents
{
private boolean optPressed;
public ClientEvents() {
optPressed = false;
}
// Send state of OPT key to server
@SubscribeEvent
public void KeyEvent(InputEvent.KeyInputEvent event) {
PlayerEntity player = Minecraft.getInstance().player;
if(player == null) return;
if(WandUtil.holdingWand(player) == null) return;
boolean optState = isOptKeyDown();
if(optPressed != optState) {
optPressed = optState;
PacketQueryUndo packet = new PacketQueryUndo(optPressed);
ConstructionWand.instance.HANDLER.sendToServer(packet);
//ConstructionWand.LOGGER.debug("OPT key update: " + optPressed);
}
}
// Sneak+(OPT)+Scroll to change direction lock
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void MouseScrollEvent(InputEvent.MouseScrollEvent event) {
PlayerEntity player = Minecraft.getInstance().player;
double scroll = event.getScrollDelta();
if(player == null || !modeKeyCombDown(player) || scroll == 0) return;
ItemStack wand = WandUtil.holdingWand(player);
if(wand == null) return;
WandOptions wandOptions = new WandOptions(wand);
wandOptions.lock.next(scroll < 0);
ConstructionWand.instance.HANDLER.sendToServer(new PacketWandOption(wandOptions.lock, true));
event.setCanceled(true);
}
// Sneak+(OPT)+Left click wand to change core
@SubscribeEvent
public void onLeftClickEmpty(PlayerInteractEvent.LeftClickEmpty event) {
PlayerEntity player = event.getPlayer();
if(player == null || !modeKeyCombDown(player)) return;
ItemStack wand = event.getItemStack();
if(!(wand.getItem() instanceof ItemWand)) return;
WandOptions wandOptions = new WandOptions(wand);
wandOptions.cores.next();
ConstructionWand.instance.HANDLER.sendToServer(new PacketWandOption(wandOptions.cores, true));
}
// Sneak+(OPT)+Right click wand to open GUI
@SubscribeEvent
public void onRightClickItem(PlayerInteractEvent.RightClickItem event) {
PlayerEntity player = event.getPlayer();
if(player == null || !guiKeyCombDown(player)) return;
ItemStack wand = event.getItemStack();
if(!(wand.getItem() instanceof ItemWand)) return;
Minecraft.getInstance().displayGuiScreen(new ScreenWand(wand));
event.setCanceled(true);
}
private static boolean isKeyDown(int id) {
return InputMappings.isKeyDown(Minecraft.getInstance().getMainWindow().getHandle(), id);
}
public static boolean isOptKeyDown() {
return isKeyDown(ConfigClient.OPT_KEY.get());
}
public static boolean modeKeyCombDown(PlayerEntity player) {
return player.isSneaking() && (isOptKeyDown() || !ConfigClient.SHIFTOPT_MODE.get());
}
public static boolean guiKeyCombDown(PlayerEntity player) {
return player.isSneaking() && (isOptKeyDown() || !ConfigClient.SHIFTOPT_GUI.get());
}
}