Skip to content

Commit

Permalink
add lunchbox right click to open so feature parity with craftingstick…
Browse files Browse the repository at this point in the history
…s; ran codestyle auto-formatter
  • Loading branch information
Lothrazar committed Jul 29, 2024
1 parent 97056d6 commit 84e1d09
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 80 deletions.
64 changes: 32 additions & 32 deletions src/main/java/com/lothrazar/cyclic/block/crafter/TileCrafter.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ public void serverTick() {
setPreviewSlot(recipeOutput);
//if we have space for the output, then go ahead
if (hasFreeSpace(outHandler, recipeOutput)) {
if(!checkInput(inputHandler)) { //Checks input before processing
this.timer = TIMER_FULL;
return;
}
if (--timer > 0) { //Get recipe before processing
return;
}
if (!checkInput(inputHandler)) { //Checks input before processing
this.timer = TIMER_FULL;
return;
}
if (--timer > 0) { //Get recipe before processing
return;
}
if (doCraft(lastValidRecipe)) {
//reset the timer
this.timer = TIMER_FULL;
Expand Down Expand Up @@ -215,32 +215,32 @@ private boolean hasFreeSpace(IItemHandler inv, ItemStack output) {

// This could be done better, but it works so ¯\_(ツ)_/¯
private boolean checkInput(IItemHandler inv) {
IItemHandler gridHandler = this.gridCap.orElse(null);
List<ItemStack> inputStacks = new ArrayList<ItemStack>();
List<ItemStack> gridStacks = new ArrayList<ItemStack>();
for(int i = 0; i < inv.getSlots(); i++) {
inputStacks.add(inv.getStackInSlot(i).copy());
}
for(int i = 0; i < gridHandler.getSlots(); i++) {
gridStacks.add(gridHandler.getStackInSlot(i).copy());
}
for(ItemStack stack : inputStacks) {
List<ItemStack> lolbit = new ArrayList<ItemStack>();
boolean match = false;
for(ItemStack grid : gridStacks) {
if(stack.getItem() == grid.getItem()) {
match = true;
lolbit.add(grid);
stack.shrink(1);
}
}
if(match) {
gridStacks.removeAll(lolbit);
}
}
return gridStacks.isEmpty();
IItemHandler gridHandler = this.gridCap.orElse(null);
List<ItemStack> inputStacks = new ArrayList<ItemStack>();
List<ItemStack> gridStacks = new ArrayList<ItemStack>();
for (int i = 0; i < inv.getSlots(); i++) {
inputStacks.add(inv.getStackInSlot(i).copy());
}
for (int i = 0; i < gridHandler.getSlots(); i++) {
gridStacks.add(gridHandler.getStackInSlot(i).copy());
}
for (ItemStack stack : inputStacks) {
List<ItemStack> lolbit = new ArrayList<ItemStack>();
boolean match = false;
for (ItemStack grid : gridStacks) {
if (stack.getItem() == grid.getItem()) {
match = true;
lolbit.add(grid);
stack.shrink(1);
}
}
if (match) {
gridStacks.removeAll(lolbit);
}
}
return gridStacks.isEmpty();
}

//TODO:? re-write this whole thing using ASSEMBLE?
//big change
// for (int i = 0; i < 9; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void setChanged() {
public boolean mayPlace(ItemStack stack) {
return false;
}

@Override
public void setChanged() {
tile.setChanged();
Expand All @@ -47,7 +47,7 @@ public void setChanged() {
public boolean mayPlace(ItemStack stack) {
return false;
}

@Override
public void setChanged() {
tile.setChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,19 @@ public void tick() {
return;
}
if (currentRecipe == null) {
this.findMatchingRecipe();
return;
this.findMatchingRecipe();
return;
}
//Checks if there is space in the output slots before processing
var res = currentRecipe.getResultItem(level.registryAccess()).copy();
var max = res.getMaxStackSize();
if(outputSlots.getStackInSlot(0).getCount() > max - res.getCount()) { return; }
if (outputSlots.getStackInSlot(0).getCount() > max - res.getCount()) {
return;
}
max = currentRecipe.randOutput.bonus.getMaxStackSize();
if(outputSlots.getStackInSlot(1).getCount() > max - currentRecipe.randOutput.bonus.getCount()) { return; }

if (outputSlots.getStackInSlot(1).getCount() > max - currentRecipe.randOutput.bonus.getCount()) {
return;
}
if (this.burnTime <= 0 && this.currentRecipe != null) {
this.burnTimeMax = 0;
this.burnTime = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lothrazar.cyclic.block.generatorfluid;

import java.util.List;

import com.google.gson.JsonObject;
import com.lothrazar.cyclic.ModCyclic;
import com.lothrazar.cyclic.registry.CyclicRecipeType;
Expand Down Expand Up @@ -67,11 +66,11 @@ public FluidStack getRecipeFluid() {
}

public List<Fluid> getFluidsFromTag() {
TagKey<Fluid> tag = ForgeRegistries.FLUIDS.tags().createTagKey(new ResourceLocation(this.fluidIng.getTag()));
List<Fluid> list = ForgeRegistries.FLUIDS.tags().getTag(tag).stream().toList();
return list;
TagKey<Fluid> tag = ForgeRegistries.FLUIDS.tags().createTagKey(new ResourceLocation(this.fluidIng.getTag()));
List<Fluid> list = ForgeRegistries.FLUIDS.tags().getTag(tag).stream().toList();
return list;
}

@Override
public boolean matches(TileGeneratorFluid inv, Level worldIn) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public void render(GuiGraphics gg, int mouseX, int mouseY, float partialTicks) {
this.renderTooltip(gg, mouseX, mouseY);
energy.renderHoveredToolTip(gg, mouseX, mouseY, menu.tile.getEnergy());
progress.renderHoveredToolTip(gg, mouseX, mouseY, menu.tile.getField(TileGeneratorFluid.Fields.TIMER.ordinal()));

btnRedstone.onValueUpdate(menu.tile);
var pose = gg.pose();
pose.pushPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public void tick() {
return;
}
//Fixes sync issue entirely
if(level.isClientSide()) return;
if (level.isClientSide()) {
return;
}
this.timer--;
if (timer < 0) {
timer = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ public void tick() {
return;
}
//Fixes sync issue entirely
if(level.isClientSide()) { return; }
if (level.isClientSide()) {
return;
}
//Checks if there is space in the output slot
var res = currentRecipe.getResultItem(level.registryAccess());
var max = res.getMaxStackSize();
if(this.outputSlots.getStackInSlot(0).getCount() > max - res.getCount()){ return; }
if (this.outputSlots.getStackInSlot(0).getCount() > max - res.getCount()) {
return;
}
final int energyCost = this.currentRecipe.getEnergy().getRfPertick();
final int fluidCost = this.currentRecipe.getRecipeFluid().getAmount();
if ((energy.getEnergyStored() < energyCost && energyCost > 0)
Expand Down Expand Up @@ -227,7 +231,7 @@ private boolean tryProcessRecipe() {
inputSlots.getStackInSlot(1).shrink(1);
inputSlots.getStackInSlot(2).shrink(1);
// if (!level.isClientSide()) { // only drain serverside to avoid desync issues # not needed anymore with above fix
tank.drain(this.currentRecipe.fluidIngredient.getAmount(), FluidAction.EXECUTE);
tank.drain(this.currentRecipe.fluidIngredient.getAmount(), FluidAction.EXECUTE);
// }
outputSlots.insertItem(0, currentRecipe.getResultItem(level.registryAccess()), false);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level world, Block
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(POWERED);
}

@Override // was onReplaced
public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CrusherRecipeCategory implements IRecipeCategory<RecipeCrusher> {
private EnergyBar bar;

public CrusherRecipeCategory(IGuiHelper helper) {
font = Minecraft.getInstance().font;
font = Minecraft.getInstance().font;
gui = helper.drawableBuilder(new ResourceLocation(ModCyclic.MODID, "textures/jei/crusher.png"), 0, 0, 155, 49).setTextureSize(155, 49).build();
icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(BlockRegistry.CRUSHER.get()));
bar = new EnergyBar(font, TileSolidifier.MAX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public void registerRecipes(IRecipeRegistration registry) {
@Override
public void registerGuiHandlers(IGuiHandlerRegistration registry) {
registry.addRecipeClickArea(ScreenMelter.class,
68, 37,
24, 17, MelterRecipeCategory.TYPE);
68, 37,
24, 17, MelterRecipeCategory.TYPE);
registry.addRecipeClickArea(ScreenSolidifier.class,
68, 37,
24, 17, SolidifierRecipeCategory.TYPE);
68, 37,
24, 17, SolidifierRecipeCategory.TYPE);
registry.addRecipeClickArea(ScreenGeneratorDrops.class,
38, 6,
100, 10, GenitemRecipeCategory.TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MelterRecipeCategory implements IRecipeCategory<RecipeMelter> {
private TexturedProgress progress;

public MelterRecipeCategory(IGuiHelper helper) {
font = Minecraft.getInstance().font;
font = Minecraft.getInstance().font;
gui = helper.drawableBuilder(new ResourceLocation(ModCyclic.MODID, "textures/jei/melter_recipe.png"), 0, 0, 169, 69).setTextureSize(169, 69).build();
icon = helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, new ItemStack(BlockRegistry.MELTER.get()));
bar = new EnergyBar(font, TileSolidifier.MAX);
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/lothrazar/cyclic/event/ClientInputEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public void onMouseButtonReleased(ScreenEvent.MouseButtonReleased.Pre event) {
if (stackTarget.getItem() instanceof ItemLunchbox
&& mc.player != null
&& mc.player.containerMenu != null) {
//
ItemStack maybeFood = mc.player.containerMenu.getCarried();
//
if (maybeFood.isEdible()) {
// inserting food must be done onMouse Released event
// this is important. opening screens is on the other event
// send the slot and info to the server to process with the lunchbox
int slotId = gui.getSlotUnderMouse().getContainerSlot();
SoundUtil.playSound(mc.player, SoundEvents.UI_BUTTON_CLICK.get());
PacketRegistry.INSTANCE.sendToServer(new PacketItemGui(slotId, stackTarget.getItem()));
event.setCanceled(true);
return;
}
}
}
Expand All @@ -104,6 +104,7 @@ public void onMouseButtonReleased(ScreenEvent.MouseButtonReleased.Pre event) {

@SubscribeEvent(priority = EventPriority.HIGH) // WAS MouseClickedEvent
public void onMouseEvent(ScreenEvent.MouseButtonPressed.Pre event) {
Minecraft mc = Minecraft.getInstance();
if (event.getScreen() == null || !(event.getScreen() instanceof AbstractContainerScreen<?>)) {
return;
}
Expand All @@ -124,6 +125,14 @@ else if (maybeCharm.getItem() instanceof ItemStorageBag
PacketRegistry.INSTANCE.sendToServer(new PacketItemGui(slotHit.index, maybeCharm.getItem()));
event.setCanceled(true);
}
else if (maybeCharm.getItem() instanceof ItemLunchbox) {
// if you have an EMPTY hand, use this to open the GUI screen of the lunchbox
ItemStack maybeFood = mc.player.containerMenu.getCarried();
if (maybeFood.isEmpty()) {
PacketRegistry.INSTANCE.sendToServer(new PacketItemGui(slotHit.index, maybeCharm.getItem()));
event.setCanceled(true);
}
}
}
}
catch (Exception e) { //array out of bounds, or we are in a strange third party GUI that doesnt have slots like this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.function.Supplier;
import com.lothrazar.cyclic.item.crafting.simple.CraftingStickContainer;
import com.lothrazar.cyclic.item.crafting.simple.CraftingStickContainerProvider;
import com.lothrazar.cyclic.item.lunchbox.ContainerProviderLunchbox;
import com.lothrazar.cyclic.item.lunchbox.ItemLunchbox;
import com.lothrazar.cyclic.item.storagebag.ContainerStorageBag;
import com.lothrazar.cyclic.item.storagebag.StorageBagContainerProvider;
import com.lothrazar.cyclic.registry.ItemRegistry;
Expand All @@ -29,27 +31,18 @@ public PacketItemGui(int slot, Item item) {
public static void handle(PacketItemGui message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
ServerPlayer player = ctx.get().getSender();
// if (message.item == ItemRegistry.STORAGE_BAG.get() && (player.containerMenu instanceof ContainerStorageBag) == false) {
// NetworkHooks.openScreen(player, new StorageBagContainerProvider(message.slot), buf -> buf.writeInt(message.slot));
// }
if (message.item == ItemRegistry.LUNCHBOX.get()) {
//put the food in the lunchbox
ItemStack itemFoodMouse = player.containerMenu.getCarried();
if (itemFoodMouse.isEmpty() || !itemFoodMouse.isEdible()) {
return; //its not food
if (itemFoodMouse.isEmpty()) {
//right click on mouse-empty-holding means open the GUI like the stick
NetworkHooks.openScreen(player, new ContainerProviderLunchbox(), buf -> buf.writeInt(message.slot));
}
ItemStack lunchbox = player.getInventory().getItem(message.slot);//why is it air
IItemHandler boxCap = lunchbox.getCapability(ForgeCapabilities.ITEM_HANDLER).orElse(null);
if (boxCap == null) {
return;
else if (itemFoodMouse.isEdible()) {
ItemStack lunchbox = player.getInventory().getItem(message.slot);//why is it air
ItemLunchbox.insertFoodIntoLunchbox(lunchbox, itemFoodMouse, player);
}
//try to put/stack into each slot
int i = 0;
while (i < boxCap.getSlots()) {
itemFoodMouse = boxCap.insertItem(i, itemFoodMouse, false);
i++;
}
player.containerMenu.setCarried(itemFoodMouse);
//else it is not edible so do nothing
} // end of lunchbox flow
else if (message.item == ItemRegistry.STORAGE_BAG.get() && (player.containerMenu instanceof ContainerStorageBag) == false) {
NetworkHooks.openScreen(player, new StorageBagContainerProvider(message.slot), buf -> buf.writeInt(message.slot));
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/com/lothrazar/cyclic/item/lunchbox/ItemLunchbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public UseAnim getUseAnimation(ItemStack st) {
return UseAnim.EAT;
}

// Show durability if our lunchbox has tagData, meaning it has or had food
@Override
public boolean isBarVisible(ItemStack stack) {
return stack.hasTag() || super.isBarVisible(stack);
Expand Down Expand Up @@ -119,7 +120,7 @@ public void readShareTag(ItemStack stack, CompoundTag nbt) {

@Override
public ItemStack finishUsingItem(ItemStack stack, Level worldIn, LivingEntity entityLiving) {
if (!worldIn.isClientSide && entityLiving instanceof Player player) {
if (!worldIn.isClientSide && entityLiving instanceof Player player) { // && !player.isCrouching()
IItemHandler handler = stack.getCapability(ForgeCapabilities.ITEM_HANDLER).orElse(null);
if (handler != null) {
ItemStack found = ItemStack.EMPTY;
Expand All @@ -132,6 +133,7 @@ public ItemStack finishUsingItem(ItemStack stack, Level worldIn, LivingEntity en
}
}
if (!found.isEmpty()) {
// found is edible and is not on cooldown
ChatUtil.addServerChatMessage(player, found.getDisplayName());
// entityLiving.eat(worldIn, found);
//moved from. eat() to forwarding the .finishUsingItem call
Expand All @@ -146,7 +148,6 @@ public ItemStack finishUsingItem(ItemStack stack, Level worldIn, LivingEntity en

@Override
public InteractionResultHolder<ItemStack> use(Level worldIn, Player player, InteractionHand handIn) {
// ItemStack itemstack = player.getItemInHand(handIn);
if (player.isCrouching()) {
if (!worldIn.isClientSide) {
NetworkHooks.openScreen((ServerPlayer) player, new ContainerProviderLunchbox(), player.blockPosition());
Expand Down Expand Up @@ -182,4 +183,28 @@ public static int getColour(ItemStack stack) {
}
return 0xFFFFFFFF;
}

/**
* for use by Item slot GUI Screen interactions. Mouse is holding an item to be inserted into a closed luncbbox from a different GUI
*
* @param lunchbox
* assumes this is a valid lunchbox with item as instance of this
* @param itemFoodMouse
* a valid food item nonempty and not a lunchbox
* @param player
* instance that is doing the insert
*/
public static void insertFoodIntoLunchbox(ItemStack lunchbox, ItemStack itemFoodMouse, ServerPlayer player) {
IItemHandler boxCap = lunchbox.getCapability(ForgeCapabilities.ITEM_HANDLER).orElse(null);
if (boxCap == null) {
return;
}
//try to put/stack into each slot
int i = 0;
while (i < boxCap.getSlots()) {
itemFoodMouse = boxCap.insertItem(i, itemFoodMouse, false);
i++;
}
player.containerMenu.setCarried(itemFoodMouse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public ScreenLunchbox(ContainerLunchbox screenContainer, Inventory inv, Componen
@Override
protected void init() {
super.init();
// CompoundTag nbt = this.menu.bag.getOrCreateTag();
}

@Override
Expand Down
Loading

0 comments on commit 84e1d09

Please sign in to comment.