-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
TreeTapBlockEntity.java
199 lines (166 loc) · 6.43 KB
/
TreeTapBlockEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package net.id.paradiselost.blocks.blockentity;
import net.id.paradiselost.blocks.mechanical.TreeTapBlock;
import net.id.paradiselost.recipe.ParadiseLostRecipeTypes;
import net.id.paradiselost.recipe.TreeTapRecipe;
import net.minecraft.block.BeehiveBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.Packet;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
public class TreeTapBlockEntity extends LootableContainerBlockEntity implements SidedInventory {
private final DefaultedList<ItemStack> inventory;
public TreeTapBlockEntity(BlockPos pos, BlockState state) {
super(ParadiseLostBlockEntityTypes.TREE_TAP, pos, state);
inventory = DefaultedList.ofSize(1, ItemStack.EMPTY);
}
public void handleUse(PlayerEntity player, Hand hand, ItemStack handStack) {
ItemStack stored = inventory.get(0);
if (!handStack.isEmpty() && inventory.get(0).isEmpty()) {
inventory.set(0, handStack.split(1));
} else {
player.giveItemStack(stored);
inventory.set(0, ItemStack.EMPTY);
}
markDirty();
}
public int[] getAvailableSlots(Direction side) {
return new int[1];
}
@Override
public boolean canInsert(int slot, ItemStack stack, @Nullable Direction dir) {
return dir != Direction.DOWN && this.inventory.get(0).isEmpty();
}
@Override
public boolean canExtract(int slot, ItemStack stack, Direction dir) {
return false;
}
public DefaultedList<ItemStack> getItems() {
return inventory;
}
@Override
public int size() {
return 1;
}
@Override
public void setStack(int slot, ItemStack stack) {
getItems().set(slot, stack);
if (stack.getCount() > 1) {
stack.setCount(1);
}
inventoryChanged();
}
@Override
protected DefaultedList<ItemStack> getInvStackList() {
return inventory;
}
@Override
protected void setInvStackList(DefaultedList<ItemStack> list) {
inventory.set(0, list.get(0));
}
private void inventoryChanged() {
markDirty();
if (world != null && !world.isClient) updateInClientWorld();
}
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
this.inventory.clear();
Inventories.readNbt(nbt, inventory);
}
@Override
public void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
Inventories.writeNbt(nbt, inventory);
}
@Override
protected Text getContainerName() {
return null;
}
@Override
protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) {
return null;
}
public BlockState getTappedState() {
return this.world.getBlockState(this.pos.offset(getCachedState().get(Properties.HORIZONTAL_FACING).getOpposite()));
}
public void tryCraft() {
ItemStack stack = getStack(0);
if (stack.isEmpty()) {
return;
}
Optional<TreeTapRecipe> recipe = this.world.getRecipeManager().getFirstMatch(ParadiseLostRecipeTypes.TREE_TAP_RECIPE_TYPE, this, this.world);
if (recipe.isPresent() && world.random.nextInt(recipe.get().getChance()) == 0) {
ItemStack output = recipe.get().craft(this);
Block convertBlock = recipe.get().getOutputBlock();
BlockPos attachedPos = this.pos.offset(world.getBlockState(this.pos).get(TreeTapBlock.FACING).getOpposite());
BlockState attachedBlock = world.getBlockState(attachedPos);
if (convertBlock != Blocks.BEE_NEST) {
stack.decrement(1);
if (convertBlock != world.getBlockState(this.pos).getBlock()) {
world.setBlockState(attachedPos, convertBlock.getDefaultState(), 0);
}
if (!world.isClient) world.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 0.5f, world.getRandom().nextFloat() * 0.4f + 0.8f);
this.inventory.set(0, output);
inventoryChanged();
} else if (attachedBlock.get(BeehiveBlock.HONEY_LEVEL) == 5) {
stack.decrement(1);
world.setBlockState(attachedPos, attachedBlock.with(BeehiveBlock.HONEY_LEVEL, 0));
if (!world.isClient) world.playSound(null, pos, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 0.5f, world.getRandom().nextFloat() * 0.4f + 0.8f);
this.inventory.set(0, output);
inventoryChanged();
}
}
tryTansferItemsOut();
}
public void tryTansferItemsOut() {
ItemStack stack = getStack(0);
if (stack.isEmpty()) {
return;
}
ItemStack contents = this.inventory.get(0);
BlockEntity possibleHopper = world.getBlockEntity(pos.down());
if (possibleHopper instanceof Inventory) {
contents = HopperBlockEntity.transfer(this, (Inventory) possibleHopper, contents, Direction.UP);
}
this.inventory.set(0, contents);
inventoryChanged();
}
@Override
public NbtCompound toInitialChunkDataNbt() {
NbtCompound nbtCompound = new NbtCompound();
this.writeNbt(nbtCompound);
return nbtCompound;
}
@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
public void updateInClientWorld() {
((ServerWorld) world).getChunkManager().markForUpdate(pos);
}
}