Skip to content

Commit

Permalink
Plate block start
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaHelios committed Apr 21, 2024
1 parent 71caabe commit c3d6022
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.parzivail.pswg.block;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class PlateBlock extends BlockWithEntity
{
public final static IntProperty FOOD_AMOUNT = IntProperty.of("food_amount", 0, 5);

public PlateBlock(Settings settings)
{
super(settings);
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state)
{
return new PlateBlockEntity(pos, state);
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
{
super.appendProperties(builder.add(FOOD_AMOUNT));
}

private void addFood(World world, BlockPos pos, BlockState state, ItemStack stack)
{
BlockEntity blockEntity = world.getBlockEntity(pos);
int foodAmount = state.get(FOOD_AMOUNT);
if (stack.isFood() && blockEntity instanceof PlateBlockEntity plateBlockEntity && foodAmount < 5)
plateBlockEntity.addFood(stack);
}

private void takeFood(World world, BlockPos pos, BlockState state, PlayerEntity player)
{
if (state.get(FOOD_AMOUNT) > 0)
{
var blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof PlateBlockEntity plateBlockEntity)
plateBlockEntity.removeFood(player);
}
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit)
{
var stack = player.getStackInHand(hand);
if (player.isSneaking() && state.get(FOOD_AMOUNT) > 0)
{
takeFood(world, pos, state, player);
world.setBlockState(pos, state.with(FOOD_AMOUNT, state.get(FOOD_AMOUNT) - 1));
return ActionResult.SUCCESS;
}
else if (!stack.isEmpty() && state.get(FOOD_AMOUNT) < 5)
{
var newStack = new ItemStack(stack.getItem(), 1);
addFood(world, pos, state, newStack);
if (player.isCreative())
stack.decrement(1);
world.setBlockState(pos, state.with(FOOD_AMOUNT, state.get(FOOD_AMOUNT) + 1));
return ActionResult.SUCCESS;
}
return ActionResult.PASS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.parzivail.pswg.block;

import com.parzivail.pswg.container.SwgBlocks;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;

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

public class PlateBlockEntity extends BlockEntity
{
public static int FOOD_AMOUNT;
public List<ItemStack> FOODS = new ArrayList<>();

public PlateBlockEntity(BlockPos pos, BlockState state)
{
super(SwgBlocks.Misc.PlateBlockEntityType, pos, state);
FOOD_AMOUNT = 0;
}

@Override
protected void writeNbt(NbtCompound nbt)
{
nbt.putInt("food_amount", FOODS.size());
for (int i = 0; i < FOODS.size(); i++)
{
nbt.put("Food" + i, this.FOODS.get(i).getNbt());
}
super.writeNbt(nbt);
}

@Override
public void readNbt(NbtCompound nbt)
{
super.readNbt(nbt);
FOOD_AMOUNT = nbt.getInt("food_amount");
for (int i = 0; i < FOOD_AMOUNT; i++)
{
this.FOODS.set(i, ItemStack.fromNbt(nbt.getCompound("Food" + i)));
}
}

public void addFood(ItemStack stack)
{
FOODS.add(stack);
FOOD_AMOUNT++;
}

public void removeFood(PlayerEntity player)
{
player.giveItemStack(FOODS.get(FOODS.size() - 1));
FOODS.remove(FOODS.size() - 1);
FOOD_AMOUNT--;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,14 @@ public static class Misc
@ClientBlockRegistryData(renderLayer = RenderLayerHint.CUTOUT_MIPPED)
@TarkinBlock(state = TrState.None, model = TrModel.None, loot = TrLoot.Pickling)
public static final ThermalDetonatorBlock ThermalDetonatorBlock = new ThermalDetonatorBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).nonOpaque().strength(0.5F));

@RegistryName("plate")
@ClientBlockRegistryData(renderLayer = RenderLayerHint.CUTOUT_MIPPED)
@TarkinBlock(state = TrState.None, model = TrModel.None)
public static final Block Plate = new PlateBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).strength(1F));

@RegistryName("plate")
public static final BlockEntityType<PlateBlockEntity> PlateBlockEntityType = FabricBlockEntityTypeBuilder.create(PlateBlockEntity::new, Plate).build();
}

public static void register()
Expand Down

0 comments on commit c3d6022

Please sign in to comment.