use of net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContainer in project SophisticatedBackpacks by P3pp3rF1y.
the class ClientProxy method tryCallSort.
private static boolean tryCallSort(Screen gui) {
Minecraft mc = Minecraft.getInstance();
if (mc.player != null && mc.player.containerMenu instanceof BackpackContainer && gui instanceof BackpackScreen) {
BackpackScreen screen = (BackpackScreen) gui;
MouseHelper mh = mc.mouseHandler;
double mouseX = mh.xpos() * mc.getWindow().getGuiScaledWidth() / mc.getWindow().getScreenWidth();
double mouseY = mh.ypos() * mc.getWindow().getGuiScaledHeight() / mc.getWindow().getScreenHeight();
BackpackContainer container = (BackpackContainer) mc.player.containerMenu;
Slot selectedSlot = screen.findSlot(mouseX, mouseY);
if (selectedSlot != null && !container.isPlayersInventorySlot(selectedSlot.index)) {
container.sort();
return true;
}
}
return false;
}
use of net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContainer in project SophisticatedBackpacks by P3pp3rF1y.
the class BackpackBlock method use.
@Override
public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
if (world.isClientSide) {
return ActionResultType.SUCCESS;
}
ItemStack heldItem = player.getItemInHand(hand);
if (player.isShiftKeyDown() && heldItem.isEmpty()) {
putInPlayersHandAndRemove(state, world, pos, player, hand);
return ActionResultType.SUCCESS;
}
if (!heldItem.isEmpty() && heldItem.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).isPresent()) {
WorldHelper.getTile(world, pos, BackpackTileEntity.class).flatMap(te -> te.getBackpackWrapper().getFluidHandler()).ifPresent(backpackFluidHandler -> player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(playerInventory -> {
FluidActionResult resultOfEmptying = FluidUtil.tryEmptyContainerAndStow(heldItem, backpackFluidHandler, playerInventory, FluidAttributes.BUCKET_VOLUME, player, true);
if (resultOfEmptying.isSuccess()) {
player.setItemInHand(hand, resultOfEmptying.getResult());
} else {
FluidActionResult resultOfFilling = FluidUtil.tryFillContainerAndStow(heldItem, backpackFluidHandler, playerInventory, FluidAttributes.BUCKET_VOLUME, player, true);
if (resultOfFilling.isSuccess()) {
player.setItemInHand(hand, resultOfFilling.getResult());
}
}
}));
return ActionResultType.SUCCESS;
}
BackpackContext.Block backpackContext = new BackpackContext.Block(pos);
NetworkHooks.openGui((ServerPlayerEntity) player, new SimpleNamedContainerProvider((w, p, pl) -> new BackpackContainer(w, pl, backpackContext), getBackpackDisplayName(world, pos)), backpackContext::toBuffer);
return ActionResultType.SUCCESS;
}
use of net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContainer in project SophisticatedBackpacks by P3pp3rF1y.
the class TankClickMessage method handleMessage.
private static void handleMessage(@Nullable ServerPlayerEntity sender, TankClickMessage msg) {
if (sender == null || !(sender.containerMenu instanceof BackpackContainer)) {
return;
}
UpgradeContainerBase<?, ?> upgradeContainer = ((BackpackContainer) sender.containerMenu).getUpgradeContainers().get(msg.upgradeSlot);
if (!(upgradeContainer instanceof TankUpgradeContainer)) {
return;
}
TankUpgradeContainer tankContainer = (TankUpgradeContainer) upgradeContainer;
ItemStack cursorStack = sender.inventory.getCarried();
cursorStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY).ifPresent(fluidHandler -> {
TankUpgradeWrapper tankWrapper = tankContainer.getUpgradeWrapper();
FluidStack tankContents = tankWrapper.getContents();
if (tankContents.isEmpty()) {
drainHandler(sender, fluidHandler, tankWrapper);
} else {
if (!tankWrapper.fillHandler(fluidHandler, itemStackIn -> {
sender.inventory.setCarried(itemStackIn);
sender.connection.send(new SSetSlotPacket(-1, -1, sender.inventory.getCarried()));
})) {
drainHandler(sender, fluidHandler, tankWrapper);
}
}
});
}
use of net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContainer in project SophisticatedBackpacks by P3pp3rF1y.
the class SBPPlugin method registerGuiHandlers.
@Override
public void registerGuiHandlers(IGuiHandlerRegistration registration) {
registration.addGuiContainerHandler(BackpackScreen.class, new IGuiContainerHandler<BackpackScreen>() {
@Override
public List<Rectangle2d> getGuiExtraAreas(BackpackScreen gui) {
List<Rectangle2d> ret = new ArrayList<>();
gui.getUpgradeSlotsRectangle().ifPresent(ret::add);
ret.addAll(gui.getUpgradeSettingsControl().getTabRectangles());
gui.getSortButtonsRectangle().ifPresent(ret::add);
return ret;
}
});
registration.addGuiContainerHandler(SettingsScreen.class, new IGuiContainerHandler<SettingsScreen>() {
@Override
public List<Rectangle2d> getGuiExtraAreas(SettingsScreen gui) {
return new ArrayList<>(gui.getSettingsTabControl().getTabRectangles());
}
});
registration.addGhostIngredientHandler(BackpackScreen.class, new IGhostIngredientHandler<BackpackScreen>() {
@Override
public <I> List<Target<I>> getTargets(BackpackScreen screen, I i, boolean b) {
List<Target<I>> targets = new ArrayList<>();
if (!(i instanceof ItemStack)) {
return targets;
}
ItemStack ghostStack = (ItemStack) i;
BackpackContainer container = screen.getMenu();
container.getOpenContainer().ifPresent(c -> c.getSlots().forEach(s -> {
if (s instanceof IFilterSlot && s.mayPlace(ghostStack)) {
targets.add(new Target<I>() {
@Override
public Rectangle2d getArea() {
return new Rectangle2d(screen.getGuiLeft() + s.x, screen.getGuiTop() + s.y, 17, 17);
}
@Override
public void accept(I i) {
PacketHandler.sendToServer(new SetGhostSlotMessage(ghostStack, s.index));
}
});
}
}));
return targets;
}
@Override
public void onComplete() {
// noop
}
});
}
use of net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContainer in project SophisticatedBackpacks by P3pp3rF1y.
the class TransferMessage method handleMessage.
private static void handleMessage(@Nullable ServerPlayerEntity player, TransferMessage msg) {
if (player == null || !(player.containerMenu instanceof BackpackContainer)) {
return;
}
BackpackContainer backpackContainer = (BackpackContainer) player.containerMenu;
BackpackInventoryHandler backpackInventory = backpackContainer.getBackpackWrapper().getInventoryHandler();
if (msg.isRestock) {
player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(playerInv -> {
InventoryHelper.transfer(backpackInventory, playerInv, s -> {
});
});
} else {
PlayerInventory inv = player.inventory;
for (int i = PlayerInventory.getSelectionSize(); i < inv.items.size(); i++) {
ItemStack stackAt = inv.getItem(i);
if (!stackAt.isEmpty()) {
inv.setItem(i, backpackInventory.insertItem(stackAt, false));
}
}
}
}
Aggregations