-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71caabe
commit c3d6022
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
projects/pswg/src/main/java/com/parzivail/pswg/block/PlateBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters