Skip to content

Commit

Permalink
初步添加储罐
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Dec 24, 2023
1 parent b9b960b commit 8c6c348
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ protected void renderBg(GuiGraphics graphics, float partialTicks, int x, int y)
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, BACKPACK);
graphics.blit(BACKPACK, leftPos + 85, topPos + 36, 0, 0, 165, 128);
graphics.drawString(font, this.menu.getFluidCount() + " mB", leftPos + 145, topPos + 130, 0x000000, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean hasBackpackData() {
@Nullable
@Override
public IBackpackData getBackpackData(EntityMaid maid) {
return new TankBackpackData();
return new TankBackpackData(maid);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,9 @@ public void setItem(int index, ItemStack stack) {
ItemStack slotItem = this.getItem(index);
boolean isSameItem = !stack.isEmpty() && ItemStack.isSameItemSameTags(slotItem, stack);
super.setItem(index, stack);
if (stack.getCount() > this.getMaxStackSize()) {
stack.setCount(this.getMaxStackSize());
}
if (index == 0 && !isSameItem) {
this.cookingTotalTime = getTotalCookTime(this.level);
this.cookingProgress = 0;
this.setChanged();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fluids.FluidActionResult;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.capability.templates.FluidTank;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;

public class TankBackpackData extends SimpleContainer implements IBackpackData {
public static final int CAPACITY = 10 * FluidType.BUCKET_VOLUME;
private static final int INPUT_INDEX = 0;
private static final int OUTPUT_INDEX = 1;
private final EntityMaid maid;
private final FluidTank tank = new FluidTank(CAPACITY);
private final ContainerData dataAccess = new ContainerData() {
public int get(int index) {
if (index == 0) {
TankBackpackData.this.tank.getFluidAmount();
return TankBackpackData.this.tank.getFluidAmount();
}
return 0;
}
Expand All @@ -38,13 +41,27 @@ public int getCount() {
}
};

public TankBackpackData() {
super(2);
public TankBackpackData(EntityMaid maid) {
super(1);
this.maid = maid;
}

@Override
public void setItem(int index, ItemStack stack) {
super.setItem(index, stack);
CombinedInvWrapper availableInv = this.maid.getAvailableInv(false);
FluidActionResult fluidActionResult = FluidUtil.tryFillContainerAndStow(stack, tank, availableInv, Integer.MAX_VALUE, null, true);
if (!fluidActionResult.isSuccess()) {
fluidActionResult = FluidUtil.tryEmptyContainerAndStow(stack, tank, availableInv, Integer.MAX_VALUE, null, true);
}
if (fluidActionResult.isSuccess()) {
ItemStack result = fluidActionResult.getResult();
ItemStack remainder = ItemHandlerHelper.insertItemStacked(availableInv, result, false);
if (!remainder.isEmpty()) {
this.maid.spawnAtLocation(remainder);
}
this.getItem(0).shrink(result.getCount());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.github.tartaricacid.touhoulittlemaid.entity.backpack.data.TankBackpackData;
import com.github.tartaricacid.touhoulittlemaid.inventory.container.MaidMainContainer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

Expand All @@ -20,11 +23,10 @@ public TankBackpackContainer(int id, Inventory inventory, int entityId) {
if (this.getMaid().getBackpackData() instanceof TankBackpackData) {
tankData = (TankBackpackData) this.getMaid().getBackpackData();
} else {
tankData = new TankBackpackData();
tankData = new TankBackpackData(this.getMaid());
}
this.data = tankData.getDataAccess();
this.addSlot(new Slot(tankData, 0, 161, 101));
this.addSlot(new Slot(tankData, 1, 161, 141));
this.addSlot(new TankInputSlot(tankData, 0, 161, 101));
this.addDataSlots(this.data);
}

Expand All @@ -42,4 +44,19 @@ protected void addBackpackInv(Inventory inventory) {
public int getTankPercent() {
return this.data.get(0) / TankBackpackData.CAPACITY;
}

public int getFluidCount() {
return this.data.get(0);
}

public static class TankInputSlot extends Slot {
public TankInputSlot(Container pContainer, int pSlot, int pX, int pY) {
super(pContainer, pSlot, pX, pY);
}

@Override
public boolean mayPlace(ItemStack stack) {
return FluidUtil.getFluidHandler(stack).isPresent();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8c6c348

Please sign in to comment.