Skip to content

Commit

Permalink
Added wrench implementation
Browse files Browse the repository at this point in the history
Signed-off-by: SuperScary
  • Loading branch information
SuperScary committed Oct 25, 2024
1 parent 05eca84 commit 086f3f9
Show file tree
Hide file tree
Showing 66 changed files with 640 additions and 184 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ dependencies {
runtimeOnly("mezz.jei:jei-${minecraft_version}-neoforge:${jei_version}")

// TOP
implementation "mcjty.theoneprobe:theoneprobe:${top_version}"
implementation("mcjty.theoneprobe:theoneprobe:${top_version}")

// PATCHOULI
implementation("vazkii.patchouli:Patchouli:${patchouli_version}")

}

// This block of code expands all declared replace properties in the specified resource targets.
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ loader_version_range=[4,)
# Dependencies
jei_version=19.21.0.246
top_version=1.21_neo-12.0.4-6
patchouli_version=1.21-87-NEOFORGE

## Mod Properties
mod_id=fluxmachines
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.superscary.fluxmachines.api.energy;

import net.neoforged.neoforge.energy.EnergyStorage;

public class FMEnergyStorage extends EnergyStorage {

public FMEnergyStorage(int capacity) {
this(capacity, capacity, capacity, 0);
}

public FMEnergyStorage(int capacity, int maxTransfer) {
this(capacity, maxTransfer, maxTransfer, 0);
}

public FMEnergyStorage(int capacity, int maxReceive, int maxExtract) {
this(capacity, maxReceive, maxExtract, 0);
}

public FMEnergyStorage(int capacity, int maxReceive, int maxExtract, int energy) {
super(capacity, maxReceive, maxExtract, energy);
}

public void setStored (int energy) {
this.energy = energy;
}

public void setMaxStorage (int capacity) {
this.capacity = capacity;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface InventoryHolder {

ItemStackHandler getInventory();
ItemStackHandler getInventory ();

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package net.superscary.fluxmachines.block.base;

import com.mojang.serialization.MapCodec;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -21,11 +26,18 @@
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.superscary.fluxmachines.blockentity.base.FMBaseBlockEntity;
import net.superscary.fluxmachines.util.ContentDropper;
import net.superscary.fluxmachines.core.registries.FMDataComponents;
import net.superscary.fluxmachines.core.registries.FMItems;
import net.superscary.fluxmachines.core.util.FMProperties;
import net.superscary.fluxmachines.core.util.tags.FMTag;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.text.DecimalFormat;
import java.util.List;

import static net.minecraft.world.level.block.state.properties.BlockStateProperties.CRAFTING;
import static net.superscary.fluxmachines.core.util.FMProperties.DESTROYED_WITH_WRENCH;

public abstract class FMBaseEntityBlock<T extends FMBaseBlockEntity> extends BaseBlock implements EntityBlock {

Expand All @@ -34,7 +46,9 @@ public abstract class FMBaseEntityBlock<T extends FMBaseBlockEntity> extends Bas

public static final DirectionProperty FACING = BlockStateProperties.FACING;

private MapCodec<BaseBlock> codec = getCodec();
private boolean disassemble = false;

private final MapCodec<BaseBlock> codec = getCodec();

public FMBaseEntityBlock (Properties properties) {
super(properties);
Expand Down Expand Up @@ -68,21 +82,22 @@ public BlockEntityType<T> getBlockEntityType () {

@Nullable
@Override
public BlockEntity newBlockEntity (BlockPos pos, BlockState state) {
public BlockEntity newBlockEntity (@NotNull BlockPos pos, @NotNull BlockState state) {
return blockEntityType.create(pos, state);
}

@Nullable
@Override
public BlockState getStateForPlacement (BlockPlaceContext context) {
return this.defaultBlockState().setValue(BlockStateProperties.POWERED, false)
.setValue(FACING, context.getNearestLookingDirection().getOpposite());
.setValue(FACING, context.getNearestLookingDirection().getOpposite())
.setValue(CRAFTING, false);
}

@Override
protected void createBlockStateDefinition (StateDefinition.@NotNull Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder);
builder.add(BlockStateProperties.POWERED, FACING);
builder.add(BlockStateProperties.POWERED, FACING, CRAFTING);
}

@Override
Expand All @@ -92,22 +107,70 @@ protected void createBlockStateDefinition (StateDefinition.@NotNull Builder<Bloc

@Override
protected void onRemove (BlockState state, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean movedByPiston) {
remove(state, level, pos, newState, movedByPiston, getDisassembled());
super.onRemove(state, level, pos, newState, movedByPiston);
}

public void remove (BlockState state, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean movedByPiston, boolean wrenched) {
if (state.getBlock() != newState.getBlock()) {
var blockentity = this.getBlockEntity(level, pos);
if (blockentity != null) {
blockentity.drops();
if (!wrenched) {
blockentity.drops(blockentity.getInventory());
}
}
}
super.onRemove(state, level, pos, newState, movedByPiston);
}

/**
* Called when the block is to be destroyed by a wrench
*/
public void destroyBlockByWrench (Player player, BlockState state, @NotNull Level level, @NotNull BlockPos pos, boolean movedByPiston) {
disassemble = true;
playerWillDestroy(level, pos, state, player);
remove(state, level, pos, state, false, disassemble);
level.setBlock(pos, Blocks.AIR.defaultBlockState(), 3, 512);
disassemble = false;
}

@Override
protected ItemInteractionResult useItemOn (ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
return ItemInteractionResult.SUCCESS;
public void appendHoverText (ItemStack stack, Item.TooltipContext context, List<Component> tooltipComponents, TooltipFlag tooltipFlag) {
if (Screen.hasShiftDown()) {
if (stack.has(FMDataComponents.ENERGY_STORED) && stack.has(FMDataComponents.ENERGY_MAX)) {
DecimalFormat format = new DecimalFormat("#,###");
tooltipComponents.add(Component.literal("Stored: " + format.format(stack.get(FMDataComponents.ENERGY_STORED)) + "/" + format.format(stack.get(FMDataComponents.ENERGY_MAX)) + " FE"));
}

if (stack.has(FMDataComponents.INVENTORY)) {
var inventory = stack.get(FMDataComponents.INVENTORY);
for (int i = 0; i < inventory.inventory().size(); i++) {
if (!inventory.inventory().get(i).is(FMItems.EMPTY.asItem()) && !inventory.inventory().get(i).is(Items.AIR)) {
var itemstack = inventory.inventory().get(i);
tooltipComponents.add(Component.translatable("gui.fluxmachines.itemlist", i+1, itemstack.getCount(), itemstack.getDisplayName()));
}
}
}
} else {
tooltipComponents.add(Component.translatable("gui.fluxmachines.press_shift"));
}
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);
}

@Override
protected MapCodec<? extends Block> codec () {
protected @NotNull MapCodec<? extends Block> codec () {
return codec;
}

@Override
protected @NotNull ItemInteractionResult useItemOn (ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (stack.is(FMTag.Items.WRENCH)) {
return ItemInteractionResult.FAIL;
}
return ItemInteractionResult.SUCCESS;
}

public boolean getDisassembled () {
return disassemble;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import net.superscary.fluxmachines.block.base.FMBaseEntityBlock;
import net.superscary.fluxmachines.blockentity.base.FMBasePoweredBlockEntity;
import net.superscary.fluxmachines.blockentity.machine.FluxFurnaceBlockEntity;
import net.superscary.fluxmachines.registries.FMBlocks;
import net.superscary.fluxmachines.util.tags.FMTag;
import net.superscary.fluxmachines.core.registries.FMBlocks;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -48,8 +47,8 @@ public MapCodec<BaseBlock> getCodec () {
}

@Override
protected ItemInteractionResult useItemOn (ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (!level.isClientSide() && !stack.is(FMTag.Items.WRENCH)) {
protected @NotNull ItemInteractionResult useItemOn (ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (!level.isClientSide()) {
BlockEntity entity = level.getBlockEntity(pos);
if (entity instanceof FluxFurnaceBlockEntity blockEntity) {
player.openMenu(new SimpleMenuProvider(blockEntity, Component.translatable("block.fluxmachines.flux_furnace")), pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -23,11 +23,19 @@
import net.neoforged.neoforge.items.ItemStackHandler;
import net.superscary.fluxmachines.api.data.BlockData;
import net.superscary.fluxmachines.api.inventory.InventoryHolder;
import net.superscary.fluxmachines.hook.WrenchHook;
import net.superscary.fluxmachines.util.keys.Keys;
import net.superscary.fluxmachines.block.base.FMBaseEntityBlock;
import net.superscary.fluxmachines.core.components.InventoryComponent;
import net.superscary.fluxmachines.core.hook.WrenchHook;
import net.superscary.fluxmachines.core.registries.FMDataComponents;
import net.superscary.fluxmachines.core.registries.FMItems;
import net.superscary.fluxmachines.core.util.inventory.ContentDropper;
import net.superscary.fluxmachines.core.util.keys.Keys;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public abstract class FMBaseBlockEntity extends BlockEntity implements MenuProvider, BlockData, InventoryHolder {

public final ItemStackHandler INVENTORY_SINGLE = new ItemStackHandler(1) {
Expand Down Expand Up @@ -105,56 +113,56 @@ public void handleUpdateTag (@NotNull CompoundTag tag, HolderLookup.@NotNull Pro
loadClientData(tag, lookupProvider);
}

/**
* Save entity data to the dropped item
* @param stack
* @param registries
*/
@Override
public void saveToItem (@NotNull ItemStack stack, HolderLookup.@NotNull Provider registries) {
super.saveToItem(stack, registries);
}

public void clearContents () {
for (int i = 0; i < inventory.getSlots(); i++) {
inventory.setStackInSlot(i, ItemStack.EMPTY);
}
}

public void drops () {
var container = new SimpleContainer(getInventory().getSlots());
public void drops (ItemStackHandler inventory) {
var container = new SimpleContainer(inventory.getSlots());
for (int i = 0; i < getInventory().getSlots(); i++) {
container.setItem(i, getInventory().getStackInSlot(i));
if (inventory.getStackInSlot(i).is(Items.AIR)) {
container.setItem(i, ItemStack.EMPTY);
} else {
container.setItem(i, inventory.getStackInSlot(i));
}
}
assert level != null;
Containers.dropContents(level, worldPosition, container);
ContentDropper.spawnDrops(level, worldPosition, container);
}

@Override
public void saveToItem (ItemStack stack, HolderLookup.Provider registries) {
super.saveToItem(stack, registries);
}

/**
* TODO: save data to item and drop that item rather than drop contents.
* Allows disassembly with wrench. Called by {@link WrenchHook#onPlayerUseBlock(Player, Level, InteractionHand, BlockHitResult)}
* @param player {@link Player} the player
* @param level {@link Level} the level
* @param hitResult {@link BlockHitResult} hit result of the interaction
* @param stack the {@link ItemStack} used. Already checked to contain {@link net.superscary.fluxmachines.util.tags.FMTag.Items#WRENCH}
* @param stack {@link ItemStack} used. Already checked to contain {@link net.superscary.fluxmachines.core.util.tags.FMTag.Items#WRENCH}
* @return {@link InteractionResult}
*/
public InteractionResult disassemble (Player player, Level level, BlockHitResult hitResult, ItemStack stack) {
public InteractionResult disassemble (Player player, Level level, BlockHitResult hitResult, ItemStack stack, @Nullable ItemStack existingData) {
var pos = hitResult.getBlockPos();
var state = level.getBlockState(pos);
var block = state.getBlock();

if (level instanceof ServerLevel serverLevel) {
var drops = Block.getDrops(state, serverLevel, pos, this, player, stack);
var block = (FMBaseEntityBlock<?>) state.getBlock();
var itemstack = getEither(existingData, new ItemStack(block));

for (var item : drops) {
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), item);
if (level instanceof ServerLevel) {
List<ItemStack> inventory = new ArrayList<>();
for (int i = 0; i < getInventory().getSlots(); i++) {
inventory.add(i, getInventory().getStackInSlot(i));
}

InventoryComponent inventoryComponent = new InventoryComponent(inventory);
itemstack.set(FMDataComponents.INVENTORY, inventoryComponent);
ContentDropper.drop(level, pos, itemstack);
}

block.playerWillDestroy(level, pos, state, player);
level.removeBlock(pos, false);
block.destroy(level, pos, getBlockState());
block.destroyBlockByWrench(player, state, level, pos, false);
return InteractionResult.sidedSuccess(level.isClientSide());
}

Expand All @@ -172,8 +180,31 @@ public InteractionResult rotateOnAxis (Level level, BlockHitResult hitResult, Bl
return InteractionResult.sidedSuccess(level.isClientSide());
}

/**
* Sets stored data level from via {@link FMDataComponents}
* @param stack the itemstack containing data.
*/
public void setData (ItemStack stack) {
if (stack.has(FMDataComponents.INVENTORY)) {
var inventoryComponent = stack.get(FMDataComponents.INVENTORY);
for (int i = 0; i < getInventory().getSlots(); i++) {
var itemstack = inventoryComponent.inventory().get(i);
if (itemstack.is(FMItems.EMPTY.asItem())) {
getInventory().setStackInSlot(i, ItemStack.EMPTY);
} else {
getInventory().setStackInSlot(i, itemstack);
}
}
}
}

@Override
public ItemStackHandler getInventory () {
return inventory;
}

public <T> T getEither (T obj, T obj2) {
return obj != null ? obj : obj2;
}

}
Loading

0 comments on commit 086f3f9

Please sign in to comment.