Refactored StaliniumPress logic. Simplified methods, added party-based crafting speed boost.

This commit is contained in:
IM23a-spirgif
2025-06-09 13:45:56 +02:00
parent f5f48c42a5
commit 91350af9df
@@ -1,9 +1,10 @@
package net.krituximon.stalinium.block.entity; package net.krituximon.stalinium.block.entity;
import net.krituximon.stalinium.event.ComradeHandler;
import net.krituximon.stalinium.item.ModItems; import net.krituximon.stalinium.item.ModItems;
import net.krituximon.stalinium.recipe.ModRecipes;
import net.krituximon.stalinium.recipe.StaliniumPressRecipe; import net.krituximon.stalinium.recipe.StaliniumPressRecipe;
import net.krituximon.stalinium.recipe.StaliniumPressRecipeInput; import net.krituximon.stalinium.recipe.StaliniumPressRecipeInput;
import net.krituximon.stalinium.recipe.ModRecipes;
import net.krituximon.stalinium.screen.custom.StaliniumPressMenu; import net.krituximon.stalinium.screen.custom.StaliniumPressMenu;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup; import net.minecraft.core.HolderLookup;
@@ -15,7 +16,6 @@ import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.Containers; import net.minecraft.world.Containers;
import net.minecraft.world.MenuProvider; import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleContainer; import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData; import net.minecraft.world.inventory.ContainerData;
@@ -25,59 +25,49 @@ import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.neoforged.neoforge.items.ItemStackHandler; import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import javax.swing.plaf.basic.BasicComboBoxUI;
import java.util.Optional; import java.util.Optional;
public class StaliniumPressBlockEntity extends BlockEntity implements MenuProvider { public class StaliniumPressBlockEntity extends BlockEntity implements MenuProvider {
public final ItemStackHandler itemHandler = new ItemStackHandler(4) {
@Override
protected void onContentsChanged(int slot) {
setChanged();
if (!level.isClientSide()) {
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
}
}
};
public static final int INPUT_SLOT = 0; public static final int INPUT_SLOT = 0;
public static final int REDSTONE_FUEL_SLOT = 1; public static final int REDSTONE_FUEL_SLOT = 1;
public static final int LAVA_FUEL_SLOT = 2; public static final int LAVA_FUEL_SLOT = 2;
public static final int OUTPUT_SLOT = 3; public static final int OUTPUT_SLOT = 3;
protected final ContainerData data;
private int progress = 0; private int progress = 0;
private int maxProgress = 72; private int maxProgress = 600; // base
public StaliniumPressBlockEntity(BlockPos pos, BlockState blockState) { public final ItemStackHandler itemHandler = new ItemStackHandler(4) {
super(ModBlockEntities.STALINIUM_PRESS_BE.get(), pos, blockState);
data = new ContainerData() {
@Override @Override
public int get(int i) { protected void onContentsChanged(int slot) {
return switch (i) { setChanged();
case 0 -> net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.progress; if (!level.isClientSide()) {
case 1 -> net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.maxProgress; level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
default -> 0; }
}
}; };
}
protected final ContainerData data = new ContainerData() {
@Override @Override
public void set(int i, int value) { public int get(int index) {
switch (i) { return index == 0 ? progress : (index == 1 ? maxProgress : 0);
case 0:
net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.progress = value;
case 1:
net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.maxProgress = value;
} }
@Override
public void set(int index, int value) {
if (index == 0) progress = value;
if (index == 1) maxProgress = value;
} }
@Override @Override
public int getCount() { public int getCount() {
return 2; return 2;
} }
}; };
public StaliniumPressBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.STALINIUM_PRESS_BE.get(), pos, state);
} }
@Override @Override
@@ -87,116 +77,113 @@ public class StaliniumPressBlockEntity extends BlockEntity implements MenuProvid
@Nullable @Nullable
@Override @Override
public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) { public AbstractContainerMenu createMenu(int id, net.minecraft.world.entity.player.Inventory inv, Player player) {
return new StaliniumPressMenu(i, inventory, this, this.data); return new StaliniumPressMenu(id, inv, this, data);
}
public void drops() {
SimpleContainer inventory = new SimpleContainer(itemHandler.getSlots());
for (int i = 0; i < itemHandler.getSlots(); i++) {
inventory.setItem(i, itemHandler.getStackInSlot(i));
}
Containers.dropContents(this.level, this.worldPosition, inventory);
} }
@Override @Override
protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) { protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
pTag.put("inventory", itemHandler.serializeNBT(pRegistries)); tag.put("inventory", itemHandler.serializeNBT(registries));
pTag.putInt("growth_chamber.progress", progress); tag.putInt("progress", progress);
pTag.putInt("growth_chamber.max_progress", maxProgress); tag.putInt("maxProgress", maxProgress);
super.saveAdditional(tag, registries);
super.saveAdditional(pTag, pRegistries);
} }
@Override @Override
protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) { protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(pTag, pRegistries); super.loadAdditional(tag, registries);
itemHandler.deserializeNBT(registries, tag.getCompound("inventory"));
itemHandler.deserializeNBT(pRegistries, pTag.getCompound("inventory")); progress = tag.getInt("progress");
progress = pTag.getInt("growth_chamber.progress"); maxProgress = tag.getInt("maxProgress");
maxProgress = pTag.getInt("growth_chamber.max_progress");
} }
public void tick(Level level, BlockPos pos, BlockState state) {
if (hasRecipe()) {
increaseCraftingProgress();
setChanged(level, pos, state);
if (hasCraftingFinished()) {
craftItem();
resetProgress();
}
} else {
resetProgress();
}
}
private void craftItem() {
Optional<RecipeHolder<StaliniumPressRecipe>> recipe = getCurrentRecipe();
itemHandler.extractItem(INPUT_SLOT, 9, false);
itemHandler.extractItem(REDSTONE_FUEL_SLOT, 1, false);
itemHandler.extractItem(LAVA_FUEL_SLOT, 1, false);
itemHandler.setStackInSlot(LAVA_FUEL_SLOT, new ItemStack(Items.BUCKET, 1));
ItemStack out = recipe.get().value().output();
itemHandler.setStackInSlot(
OUTPUT_SLOT,
new ItemStack(
out.getItem(),
itemHandler.getStackInSlot(OUTPUT_SLOT).getCount() + 1
)
);
}
private void resetProgress() {
progress = 0;
maxProgress = 600;
}
private boolean hasCraftingFinished() {
return this.progress >= this.maxProgress;
}
private void increaseCraftingProgress() {
progress++;
}
private boolean hasRecipe() {
Optional<RecipeHolder<StaliniumPressRecipe>> recipe = getCurrentRecipe();
if (recipe.isEmpty()) return false;
ItemStack out = recipe.get().value().output();
return canInsertItemIntoOutputSlot(out) && canInsertAmountIntoOutputSlot(out.getCount());
}
private Optional<RecipeHolder<StaliniumPressRecipe>> getCurrentRecipe() {
return this.level.getRecipeManager()
.getRecipeFor(ModRecipes.STALINIUM_PRESS_TYPE.get(), new StaliniumPressRecipeInput(
itemHandler.getStackInSlot(REDSTONE_FUEL_SLOT),
itemHandler.getStackInSlot(INPUT_SLOT),
itemHandler.getStackInSlot(LAVA_FUEL_SLOT)
), level);
}
private boolean canInsertItemIntoOutputSlot(ItemStack output) {
return itemHandler.getStackInSlot(OUTPUT_SLOT).isEmpty() ||
itemHandler.getStackInSlot(OUTPUT_SLOT).getItem() == output.getItem();
}
private boolean canInsertAmountIntoOutputSlot(int count) {
int maxCount = itemHandler.getStackInSlot(OUTPUT_SLOT).isEmpty() ? 64 : itemHandler.getStackInSlot(OUTPUT_SLOT).getMaxStackSize();
int currentCount = itemHandler.getStackInSlot(OUTPUT_SLOT).getCount();
return maxCount >= currentCount + count;
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) {
return saveWithoutMetadata(pRegistries);
}
@Nullable
@Override @Override
public Packet<ClientGamePacketListener> getUpdatePacket() { public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this); return ClientboundBlockEntityDataPacket.create(this);
} }
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
public static void tick(Level level, BlockPos pos, BlockState state, StaliniumPressBlockEntity be) {
if (be.hasRecipe()) {
// count nearby partymembers within 10 blocks
AABB box = new AABB(pos).inflate(10);
int nearbyComrades = 0;
for (Player p : level.getEntitiesOfClass(Player.class, box, _ignored -> true)) {
if (ComradeHandler.findPartyOf(p.getUUID()).isPresent()) {
nearbyComrades++;
}
}
// base + bonus per comrade
int speed = 1 + nearbyComrades;
for (int i = 0; i < speed; i++) {
be.progress++;
}
be.setChanged(level, pos, state);
if (be.progress >= be.maxProgress) {
be.craftItem();
be.resetProgress();
}
} else {
be.resetProgress();
}
}
private boolean hasRecipe() {
// require ≥9 nuggets
ItemStack in = itemHandler.getStackInSlot(INPUT_SLOT);
if (in.getItem() != ModItems.STALINIUM_NUGGET.get() || in.getCount() < 9) {
return false;
}
Optional<RecipeHolder<StaliniumPressRecipe>> o = getCurrentRecipe();
if (o.isEmpty()) return false;
ItemStack out = o.get().value().output();
return canInsertItemIntoOutputSlot(out) &&
canInsertAmountIntoOutputSlot(out.getCount());
}
private Optional<RecipeHolder<StaliniumPressRecipe>> getCurrentRecipe() {
return level.getRecipeManager()
.getRecipeFor(ModRecipes.STALINIUM_PRESS_TYPE.get(),
new StaliniumPressRecipeInput(
itemHandler.getStackInSlot(REDSTONE_FUEL_SLOT),
itemHandler.getStackInSlot(INPUT_SLOT),
itemHandler.getStackInSlot(LAVA_FUEL_SLOT)
),
level);
}
private void craftItem() {
Optional<RecipeHolder<StaliniumPressRecipe>> o = getCurrentRecipe();
if (o.isEmpty()) return;
itemHandler.extractItem(INPUT_SLOT, 9, false);
itemHandler.extractItem(REDSTONE_FUEL_SLOT, 1, false);
itemHandler.extractItem(LAVA_FUEL_SLOT, 1, false);
itemHandler.setStackInSlot(LAVA_FUEL_SLOT, new ItemStack(Items.BUCKET));
ItemStack out = o.get().value().output();
ItemStack current = itemHandler.getStackInSlot(OUTPUT_SLOT);
itemHandler.setStackInSlot(
OUTPUT_SLOT,
new ItemStack(out.getItem(), current.getCount() + 1)
);
}
private void resetProgress() {
this.progress = 0;
this.maxProgress = 600;
}
private boolean canInsertItemIntoOutputSlot(ItemStack out) {
ItemStack current = itemHandler.getStackInSlot(OUTPUT_SLOT);
return current.isEmpty() || current.getItem() == out.getItem();
}
private boolean canInsertAmountIntoOutputSlot(int count) {
ItemStack current = itemHandler.getStackInSlot(OUTPUT_SLOT);
int max = current.isEmpty() ? 64 : current.getMaxStackSize();
return max >= current.getCount() + count;
}
} }