Skip to content

Commit

Permalink
GPS & GPS Area tools now have quick mouse-wheel adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Jul 31, 2024
1 parent aa562fe commit 3c1ab53
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 44 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Changes are in reverse chronological order; newest changes at the top.
* Increased base damage in config from 4 to 6 (half-hearts per hit)
* Added a Muffler Upgrade
* One or more of these in noisy items and blocks (Miniguns, Sentry Turrents, Jackhammers, Jet Boots) quietens down the racket
* Note that adjusting the volume of these items/blocks is still possible via client config
* GPS Tool and Area GPS Tool now have quick coordinate adjusting without needing to open the GUI
* Sneak and scroll the mouse-wheel while holding a tool to adjust the coordinate along the player's facing axis
* For GPS Area Tool, sneak and right- or left-click in the air to set the coordinate which will be adjusted

# Minecraft 1.20.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public interface IPositionProvider {
* Get block position data from the given ItemStack. It is up to the implementor to decide how the block positions
* should be stored on the itemstack and in what order they should be returned.
*
* @param playerId the player, for player-global variable context (may be null)
* @param playerId the player, for player-global variable context (which may be null)
* @param stack the itemstack
* @return a list of block positions that has been retrieved from the itemstack
* @return an immutable list of block positions that has been retrieved from the itemstack
*/
@Nonnull
List<BlockPos> getStoredPositions(UUID playerId, @Nonnull ItemStack stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -130,7 +129,7 @@ default boolean afterHackTick(BlockGetter world, BlockPos pos) {

/**
* Convenience method to fake up a ray trace result for a targeted block. This is intended to be passed into
* {@link BlockState#use(Level, Player, InteractionHand, BlockHitResult)} (often called by
* {@link BlockState#useWithoutItem(Level, Player, BlockHitResult)} (often called by
* {@link #onHackComplete(Level, BlockPos, Player)}), which needs a non-null ray trace result to know exactly
* where the player is looking.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ public void init() {

int x = xMiddle - CHANGE_AREA_BUTTON_WIDTH / 2;
int y = yMiddle + 100;

ItemStack stack = ClientUtils.getClientPlayer().getItemInHand(hand);
ProgWidgetArea area = GPSAreaToolItem.getArea(ClientUtils.getClientPlayer(), stack);
addRenderableWidget(new WidgetButtonExtended(x, y, CHANGE_AREA_BUTTON_WIDTH, 20, xlate("pneumaticcraft.gui.gps_area_tool.changeAreaType"), b -> {
ItemStack stack = ClientUtils.getClientPlayer().getItemInHand(hand);
ProgWidgetArea area = GPSAreaToolItem.getArea(ClientUtils.getClientPlayer(), stack);
minecraft.setScreen(new ProgWidgetAreaToolScreen(area, hand, () -> minecraft.setScreen(new GPSAreaToolScreen(stack, hand, index))));
}));

Expand All @@ -95,7 +96,7 @@ protected void syncToServer() {
for (int i = 0; i <= 1; i++) {
if (changed(i)) {
String varName = GlobalVariableHelper.getInstance().getPrefixedVar(vars[i], playerGlobals[i]);
NetworkHandler.sendToServer(new PacketChangeGPSToolCoordinate(p1p2Pos[i], hand, varName, i));
NetworkHandler.sendToServer(new PacketChangeGPSToolCoordinate(p1p2Pos[i], hand, varName, i, i == index));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void removed() {

protected void syncToServer() {
String varName = GlobalVariableHelper.getInstance().getPrefixedVar(GlobalVariableHelper.getInstance().stripVarPrefix(variableField.getValue()), playerGlobal);
NetworkHandler.sendToServer(new PacketChangeGPSToolCoordinate(getBlockPos(), hand, varName, getIndex()));
NetworkHandler.sendToServer(new PacketChangeGPSToolCoordinate(getBlockPos(), hand, varName, getIndex(), playerGlobal));
}

protected int getIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ private void maybeRenderPositionProvider(PoseStack matrixStack, MultiBufferSourc
lastItemHashCode = thisHash;
List<BlockPos> posList = positionProvider.getStoredPositions(player.getUUID(), curItem);
if (posList.size() > MAX_DISPLAYED_POS) {
posList.sort(Comparator.comparingDouble(blockPos -> blockPos.distSqr(player.blockPosition())));
posList = posList.stream()
.sorted(Comparator.comparingDouble(blockPos -> blockPos.distSqr(player.blockPosition())))
.toList();
player.displayClientMessage(xlate("pneumaticcraft.message.gps_tool.culledRenderArea", posList.size()).withStyle(ChatFormatting.GOLD), false);
}
Int2ObjectMap<Set<BlockPos>> colorsToPositions = new Int2ObjectOpenHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,20 @@ public List<Component> getExtraStringInfo() {
res.add(Component.literal(PneumaticCraftUtils.posToString(pos[1])));
}
if (res.size() == 2) {
MutableComponent c = xlate(areaType.getTranslationKey());
List<AreaTypeWidget> widgets = new ArrayList<>();
areaType.addUIWidgets(widgets);
widgets.forEach(w -> c.append("/").append(w.getDisplayName()));
res.add(c);
addAreaTypeInfo(areaType, res);
}
}
return res;
}

public static void addAreaTypeInfo(AreaType areaType, List<Component> res) {
MutableComponent c = xlate(areaType.getTranslationKey());
List<AreaTypeWidget> widgets = new ArrayList<>();
areaType.addUIWidgets(widgets);
widgets.forEach(w -> c.append("/").append(w.getDisplayName()));
res.add(c);
}

@Override
public ProgWidgetType<?> getType() {
return ModProgWidgetTypes.AREA.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import me.desht.pneumaticcraft.client.util.ClientUtils;
import me.desht.pneumaticcraft.common.drone.progwidgets.ProgWidgetArea;
import me.desht.pneumaticcraft.common.drone.progwidgets.SavedDroneProgram;
import me.desht.pneumaticcraft.common.network.NetworkHandler;
import me.desht.pneumaticcraft.common.network.PacketLeftClickEmpty;
import me.desht.pneumaticcraft.common.registry.ModDataComponents;
import me.desht.pneumaticcraft.common.registry.ModItems;
import me.desht.pneumaticcraft.common.registry.ModSounds;
import me.desht.pneumaticcraft.common.util.PneumaticCraftUtils;
import me.desht.pneumaticcraft.common.variables.GlobalVariableHelper;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -57,10 +60,11 @@

import static me.desht.pneumaticcraft.common.util.PneumaticCraftUtils.xlate;

public class GPSAreaToolItem extends Item implements IPositionProvider, IGPSToolSync {
public class GPSAreaToolItem extends Item implements IPositionProvider, IGPSToolSync, ILeftClickableItem, IShiftScrollable {
public GPSAreaToolItem() {
super(ModItems.defaultProps()
.component(ModDataComponents.SAVED_DRONE_PROGRAM, SavedDroneProgram.EMPTY)
.component(ModDataComponents.GPS_AREA_TOOL_INDEX, 0)
);
}

Expand All @@ -71,6 +75,7 @@ public InteractionResult useOn(UseOnContext ctx) {
if (!ctx.getClickedPos().equals(optPos.orElse(null))) {
setGPSPosAndNotify(ctx.getPlayer(), ctx.getHand(), ctx.getClickedPos(), 0);
ctx.getPlayer().playSound(ModSounds.CHIRP.get(), 1.0f, 1.5f);
setActiveIndex(ctx.getPlayer(), ctx.getItemInHand(), 0);
}
}
return InteractionResult.SUCCESS; // we don't want to use the item.
Expand All @@ -79,24 +84,44 @@ public InteractionResult useOn(UseOnContext ctx) {
@Override
public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
ItemStack stack = playerIn.getItemInHand(handIn);
if (worldIn.isClientSide) {
if (worldIn.isClientSide && !playerIn.isCrouching()) {
GPSAreaToolScreen.showGUI(stack, handIn, 0);
} else if (playerIn.isCrouching()) {
setActiveIndex(playerIn, stack, 0);
}
return InteractionResultHolder.success(stack);
}

public static void setActiveIndex(Player player, ItemStack stack, int index) {
Validate.isTrue(index == 0 || index == 1);
stack.set(ModDataComponents.GPS_AREA_TOOL_INDEX, index);
String str = index == 0 ? ChatFormatting.RED + "P1" : ChatFormatting.GREEN + "P2";
player.displayClientMessage(stack.getDisplayName().copy().withStyle(ChatFormatting.AQUA)
.append(" ")
.append(Component.translatable("pneumaticcraft.message.gps_tool.activeIndex", str).withStyle(ChatFormatting.YELLOW)),
true);
}

public static int getActiveIndex(ItemStack stack) {
return stack.getOrDefault(ModDataComponents.GPS_AREA_TOOL_INDEX, 0);
}

public static void setGPSPosAndNotify(Player player, ItemStack stack, BlockPos pos, int index) {
setGPSLocation(player, stack, pos, null, index, true);
if (player instanceof ServerPlayer) {
player.displayClientMessage(Component.literal(ChatFormatting.AQUA + String.format("[%s] ", stack.getHoverName().getString()))
.append(getMessageText(player.level(), pos, index)), false);
notifyPlayer(player, stack, pos, index, false);
}
}

public static void setGPSPosAndNotify(Player player, InteractionHand hand, BlockPos pos, int index) {
setGPSPosAndNotify(player, player.getItemInHand(hand), pos, index);
}

private static void notifyPlayer(Player player, ItemStack stack, BlockPos pos, int index, boolean actionBar) {
player.displayClientMessage(Component.literal(ChatFormatting.AQUA + String.format("[%s] ", stack.getHoverName().getString()))
.append(getMessageText(player.level(), pos, index)), actionBar);
}

private static Component getMessageText(Level worldIn, BlockPos pos, int index) {
Component translated = PneumaticCraftUtils.getBlockNameAt(worldIn, pos);
MutableComponent blockName = worldIn.isLoaded(pos) ?
Expand All @@ -120,7 +145,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
.ifPresent(pos -> infoList.add(getMessageText(level, pos, i)));
String varName = area.getVarName(index);
if (!varName.isEmpty()) {
infoList.add(xlate("pneumaticcraft.gui.tooltip.gpsTool.variable", varName));
infoList.add(Component.literal(" ").append(xlate("pneumaticcraft.gui.tooltip.gpsTool.variable", varName)));
}
}
if (infoList.size() - n >= 2) area.addAreaTypeTooltip(infoList);
Expand Down Expand Up @@ -238,12 +263,37 @@ public boolean disableDepthTest() {
}

@Override
public void syncFromClient(Player player, ItemStack stack, int index, BlockPos pos, String varName) {
GPSAreaToolItem.setVariable(player, stack, varName, index);
GPSAreaToolItem.setGPSPosAndNotify(player, stack, pos, index);
public void syncFromClient(Player player, ItemStack stack, int index, BlockPos pos, String varName, boolean activeIndex) {
setVariable(player, stack, varName, index);
setGPSPosAndNotify(player, stack, pos, index);
if (activeIndex) {
setActiveIndex(player, stack, index);
}
if (!varName.isEmpty()) {
GlobalVariableHelper.getInstance().setPos(player.getUUID(), varName, pos);
}

}

@Override
public void onLeftClickEmpty(ServerPlayer sender) {
if (sender.getMainHandItem().getItem() instanceof GPSAreaToolItem) {
setActiveIndex(sender, sender.getMainHandItem(), 1);
}
}

@Override
public void onShiftScrolled(Player player, boolean forward, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (stack.getItem() instanceof GPSAreaToolItem) {
int index = getActiveIndex(stack);
getGPSLocation(player, stack, index).ifPresent(loc -> {
Direction facing = Direction.orderedByNearest(player)[0];
BlockPos newPos = loc.relative(forward ? facing : facing.getOpposite());
setGPSLocation(player, stack, newPos, null, index, true);
notifyPlayer(player, stack, newPos, index, true);
});
}
}

public static class EventHandler {
Expand All @@ -255,15 +305,19 @@ public static void onBlockLeftClick(PlayerInteractEvent.LeftClickBlock event) {
event.getEntity().playSound(ModSounds.CHIRP.get(), 1.0f, 1.5f);
setGPSPosAndNotify(event.getEntity(), event.getHand(), event.getPos(), 1);
}
setActiveIndex(event.getEntity(), event.getItemStack(), 1);
event.setCanceled(true);
}
}

@SubscribeEvent
public static void onLeftClickAir(PlayerInteractEvent.LeftClickEmpty event) {
if (event.getItemStack().getItem() == ModItems.GPS_AREA_TOOL.get()) {
GPSAreaToolScreen.showGUI(event.getItemStack(), event.getHand(), 1);
}
if (event.getItemStack().getItem() == ModItems.GPS_AREA_TOOL.get())
if (!event.getEntity().isCrouching()) {
GPSAreaToolScreen.showGUI(event.getItemStack(), event.getHand(), 1);
} else {
NetworkHandler.sendToServer(PacketLeftClickEmpty.INSTANCE);
}
}
}
}
30 changes: 24 additions & 6 deletions src/main/java/me/desht/pneumaticcraft/common/item/GPSToolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import me.desht.pneumaticcraft.common.variables.GlobalVariableHelper;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -49,7 +50,7 @@

import static me.desht.pneumaticcraft.common.util.PneumaticCraftUtils.xlate;

public class GPSToolItem extends Item implements IPositionProvider, IGPSToolSync {
public class GPSToolItem extends Item implements IPositionProvider, IGPSToolSync, IShiftScrollable {
public GPSToolItem() {
super(ModItems.defaultProps());
}
Expand All @@ -60,15 +61,19 @@ public InteractionResult useOn(UseOnContext ctx) {
if (ctx.getPlayer() == null) return InteractionResult.PASS;
setGPSLocation(ctx.getPlayer().getUUID(), ctx.getPlayer().getItemInHand(ctx.getHand()), pos);
if (!ctx.getLevel().isClientSide) {
ctx.getPlayer().displayClientMessage(ctx.getItemInHand().getDisplayName().copy().withStyle(ChatFormatting.AQUA)
.append(" ")
.append(Component.translatable("pneumaticcraft.message.gps_tool.targetSet",
pos.getX(), pos.getY(), pos.getZ()).withStyle(ChatFormatting.GREEN)), false);
displayPosition(ctx.getPlayer(), ctx.getItemInHand(), pos, false);
}
ctx.getPlayer().playSound(ModSounds.CHIRP.get(), 1.0f, 1.5f);
return InteractionResult.SUCCESS; // we don't want to use the item.
}

private static void displayPosition(Player player, ItemStack itemInHand, BlockPos pos, boolean actionBar) {
player.displayClientMessage(itemInHand.getDisplayName().copy().withStyle(ChatFormatting.AQUA)
.append(" ")
.append(Component.translatable("pneumaticcraft.message.gps_tool.targetSet",
pos.getX(), pos.getY(), pos.getZ()).withStyle(ChatFormatting.GREEN)), actionBar);
}

@Override
public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
ItemStack stack = playerIn.getItemInHand(handIn);
Expand Down Expand Up @@ -181,11 +186,24 @@ public void syncVariables(ServerPlayer player, ItemStack stack) {
}

@Override
public void syncFromClient(Player player, ItemStack stack, int index, BlockPos pos, String varName) {
public void syncFromClient(Player player, ItemStack stack, int index, BlockPos pos, String varName, boolean activeIndex) {
GPSToolItem.setVariable(stack, varName);
GPSToolItem.setGPSLocation(player.getUUID(), stack, pos);
if (!varName.isEmpty()) {
GlobalVariableHelper.getInstance().setPos(player.getUUID(), varName, pos);
}
}

@Override
public void onShiftScrolled(Player player, boolean forward, InteractionHand hand) {
if (!player.level().isClientSide) {
ItemStack stack = player.getItemInHand(hand);
getGPSLocation(stack).ifPresent(loc -> {
Direction facing = Direction.orderedByNearest(player)[0];
BlockPos newPos = loc.relative(forward ? facing : facing.getOpposite());
setGPSLocation(player.getUUID(), stack, newPos);
displayPosition(player, stack, newPos, true);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public interface IGPSToolSync {
/**
* Called when the GPS (Area) Tool GUI is closed, to send position &amp; variable information to server
* @param player the player
* @param stack the gps (area) tool itemstack
* @param index index of the pos &amp; var (ignore for GPS Tool)
* @param pos the new position for this index
* @param varName the new variable name for this index (empty string if absent)
*
* @param player the player
* @param stack the gps (area) tool itemstack
* @param index index of the pos &amp; var (ignore for GPS Tool)
* @param pos the new position for this index
* @param varName the new variable name for this index (empty string if absent)
* @param activeIndex
*/
void syncFromClient(Player player, ItemStack stack, int index, BlockPos pos, String varName);
void syncFromClient(Player player, ItemStack stack, int index, BlockPos pos, String varName, boolean activeIndex);
}
Loading

0 comments on commit 3c1ab53

Please sign in to comment.