Skip to content

Commit

Permalink
start of tank slot interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungOnionMC committed Dec 10, 2024
1 parent 35501c8 commit eb6cb66
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand Down Expand Up @@ -317,6 +320,22 @@ public void setAdapter(@Nullable UIAdapter<?> adapter) {
return this.hasParent() ? this.parent.focusHandler() : null;
}

@Nullable
public Player player() {
if(adapter() == null || adapter().container() == null) return null;
return adapter().container().player();
}

public ItemStack getCarried() {
if(adapter() == null || adapter().container() == null) return ItemStack.EMPTY;
return adapter().container().getCarried();
}

public void setCarried(@NotNull ItemStack stack) {
if(adapter() == null || adapter().container() == null) return;
adapter().container().setCarried(stack);
}

@Override
public BaseUIComponent positioning(Positioning positioning) {
this.positioning.set(positioning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.SoundActions;
import net.minecraftforge.fluids.FluidActionResult;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.templates.EmptyFluidHandler;

Expand Down Expand Up @@ -104,6 +112,59 @@ public void draw(UIGuiGraphics graphics, int mouseX, int mouseY, float partialTi
}
}

@Override
public boolean onMouseDown(double mouseX, double mouseY, int button) {
Player player = player();
if(player == null) return false;
boolean isShift = player.isShiftKeyDown();
ItemStack currentStack = getCarried();
var handler = FluidUtil.getFluidHandler(currentStack).resolve().orElse(null);
if(handler == null) return false;
int maxAttempts = isShift ? currentStack.getCount() : 1;
FluidStack initialFluid = this.handler.getFluidInTank(tank).copy();
if(initialFluid.getAmount() > 0) {
boolean performedFill = false;
ItemStack filledResult = ItemStack.EMPTY;
for(int i = 0; i < maxAttempts; i++) {
FluidActionResult res = FluidUtil.tryFillContainer(currentStack, this.handler, Integer.MAX_VALUE, null, false);
if(!res.isSuccess()) break;
ItemStack remaining = FluidUtil.tryFillContainer(currentStack, this.handler, Integer.MAX_VALUE, null, true).getResult();
performedFill = true;

currentStack.shrink(1);

if(filledResult.isEmpty()) {
filledResult = remaining.copy();
} else if(ItemStack.isSameItemSameTags(filledResult, remaining)) {
if(filledResult.getCount() < filledResult.getMaxStackSize())
filledResult.grow(1);
else
player.getInventory().placeItemBackInInventory(remaining);
}
else {
player.getInventory().placeItemBackInInventory(filledResult);
filledResult = remaining.copy();
}
}
if(performedFill) {
SoundEvent sound = initialFluid.getFluid().getFluidType().getSound(initialFluid, SoundActions.BUCKET_FILL);
if(sound == null)
sound = SoundEvents.BUCKET_FILL;
player.level().playSound(null, player.position().x, player.getEyeY(), player.position().z,
sound, SoundSource.BLOCKS, 1.0f, 1.0f);

if(currentStack.isEmpty()) { // todo properly sync this to the server(or client?)
setCarried(filledResult);
} else {
setCarried(currentStack);
player.getInventory().placeItemBackInInventory(filledResult);
}
return true;
}
}
return super.onMouseDown(mouseX, mouseY, button);
}

public static TankComponent parse(Element element) {
UIParsing.expectAttributes(element, "tank");
int tank = UIParsing.parseUnsignedInt(element.getAttributeNode("tank"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.network.chat.Component;

import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down

0 comments on commit eb6cb66

Please sign in to comment.