Refactored StaliniumPress logic. Simplified methods, added party-based crafting speed boost.
This commit is contained in:
+126
-139
@@ -1,9 +1,10 @@
|
||||
package net.krituximon.stalinium.block.entity;
|
||||
|
||||
import net.krituximon.stalinium.event.ComradeHandler;
|
||||
import net.krituximon.stalinium.item.ModItems;
|
||||
import net.krituximon.stalinium.recipe.ModRecipes;
|
||||
import net.krituximon.stalinium.recipe.StaliniumPressRecipe;
|
||||
import net.krituximon.stalinium.recipe.StaliniumPressRecipeInput;
|
||||
import net.krituximon.stalinium.recipe.ModRecipes;
|
||||
import net.krituximon.stalinium.screen.custom.StaliniumPressMenu;
|
||||
import net.minecraft.core.BlockPos;
|
||||
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.MenuProvider;
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
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.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.neoforged.neoforge.items.ItemStackHandler;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.plaf.basic.BasicComboBoxUI;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StaliniumPressBlockEntity extends BlockEntity implements MenuProvider {
|
||||
public static final int INPUT_SLOT = 0;
|
||||
public static final int REDSTONE_FUEL_SLOT = 1;
|
||||
public static final int LAVA_FUEL_SLOT = 2;
|
||||
public static final int OUTPUT_SLOT = 3;
|
||||
|
||||
private int progress = 0;
|
||||
private int maxProgress = 600; // base
|
||||
|
||||
public final ItemStackHandler itemHandler = new ItemStackHandler(4) {
|
||||
@Override
|
||||
protected void onContentsChanged(int slot) {
|
||||
setChanged();
|
||||
if (!level.isClientSide()) {
|
||||
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
|
||||
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static final int INPUT_SLOT = 0;
|
||||
public static final int REDSTONE_FUEL_SLOT = 1;
|
||||
public static final int LAVA_FUEL_SLOT = 2;
|
||||
public static final int OUTPUT_SLOT = 3;
|
||||
protected final ContainerData data = new ContainerData() {
|
||||
@Override
|
||||
public int get(int index) {
|
||||
return index == 0 ? progress : (index == 1 ? maxProgress : 0);
|
||||
}
|
||||
@Override
|
||||
public void set(int index, int value) {
|
||||
if (index == 0) progress = value;
|
||||
if (index == 1) maxProgress = value;
|
||||
}
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
|
||||
protected final ContainerData data;
|
||||
private int progress = 0;
|
||||
private int maxProgress = 72;
|
||||
|
||||
public StaliniumPressBlockEntity(BlockPos pos, BlockState blockState) {
|
||||
super(ModBlockEntities.STALINIUM_PRESS_BE.get(), pos, blockState);
|
||||
data = new ContainerData() {
|
||||
@Override
|
||||
public int get(int i) {
|
||||
return switch (i) {
|
||||
case 0 -> net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.progress;
|
||||
case 1 -> net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.maxProgress;
|
||||
default -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int i, int value) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.progress = value;
|
||||
case 1:
|
||||
net.krituximon.stalinium.block.entity.StaliniumPressBlockEntity.this.maxProgress = value;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
public StaliniumPressBlockEntity(BlockPos pos, BlockState state) {
|
||||
super(ModBlockEntities.STALINIUM_PRESS_BE.get(), pos, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,116 +77,113 @@ public class StaliniumPressBlockEntity extends BlockEntity implements MenuProvid
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
|
||||
return new StaliniumPressMenu(i, inventory, this, 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);
|
||||
public AbstractContainerMenu createMenu(int id, net.minecraft.world.entity.player.Inventory inv, Player player) {
|
||||
return new StaliniumPressMenu(id, inv, this, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) {
|
||||
pTag.put("inventory", itemHandler.serializeNBT(pRegistries));
|
||||
pTag.putInt("growth_chamber.progress", progress);
|
||||
pTag.putInt("growth_chamber.max_progress", maxProgress);
|
||||
|
||||
super.saveAdditional(pTag, pRegistries);
|
||||
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
tag.put("inventory", itemHandler.serializeNBT(registries));
|
||||
tag.putInt("progress", progress);
|
||||
tag.putInt("maxProgress", maxProgress);
|
||||
super.saveAdditional(tag, registries);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) {
|
||||
super.loadAdditional(pTag, pRegistries);
|
||||
|
||||
itemHandler.deserializeNBT(pRegistries, pTag.getCompound("inventory"));
|
||||
progress = pTag.getInt("growth_chamber.progress");
|
||||
maxProgress = pTag.getInt("growth_chamber.max_progress");
|
||||
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
super.loadAdditional(tag, registries);
|
||||
itemHandler.deserializeNBT(registries, tag.getCompound("inventory"));
|
||||
progress = tag.getInt("progress");
|
||||
maxProgress = tag.getInt("maxProgress");
|
||||
}
|
||||
|
||||
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
|
||||
public Packet<ClientGamePacketListener> getUpdatePacket() {
|
||||
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 party‐members 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user