/
RedstoneMachineBlock.java
234 lines (188 loc) · 8.77 KB
/
RedstoneMachineBlock.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package slimeknights.tmechworks.common.blocks;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.DirectionalBlock;
import net.minecraft.block.material.Material;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.state.StateContainer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.fml.network.NetworkHooks;
import slimeknights.mantle.tileentity.InventoryTileEntity;
import slimeknights.tmechworks.common.blocks.tileentity.RedstoneMachineTileEntity;
import slimeknights.tmechworks.library.Util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
public abstract class RedstoneMachineBlock extends DirectionalBlock {
public boolean dropState = true;
private TileEntity cachedTE;
protected RedstoneMachineBlock(Material material) {
super(Block.Properties.create(material).hardnessAndResistance(3.5F));
this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH));
}
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
super.fillStateContainer(builder);
builder.add(FACING);
}
protected boolean openGui(PlayerEntity player, World world, BlockPos pos) {
if (player instanceof ServerPlayerEntity && !(player instanceof FakePlayer)) {
TileEntity te = world.getTileEntity(pos);
if (!(te instanceof INamedContainerProvider))
return false;
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) te, pos);
}
return true;
}
@Override
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
super.neighborChanged(state, worldIn, pos, blockIn, fromPos, isMoving);
RedstoneMachineTileEntity logicBase = (RedstoneMachineTileEntity) worldIn.getTileEntity(pos);
if (logicBase != null) {
logicBase.updateRedstone();
}
}
@Override
public boolean shouldCheckWeakPower(BlockState state, IWorldReader world, BlockPos pos, Direction side) {
return true;
}
public boolean hasFacingDirection() {
return true;
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
if (!hasFacingDirection())
return super.getStateForPlacement(context);
return this.getDefaultState().with(FACING, context.getNearestLookingDirection().getOpposite());
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nonnull LivingEntity placer, ItemStack stack) {
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
// set custom name from named stack
if (stack.hasDisplayName()) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof InventoryTileEntity) {
((InventoryTileEntity) tileentity).setCustomName(stack.getDisplayName());
}
}
}
@Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.emptyList();
}
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (blockMatches(state, worldIn, pos, newState, isMoving))
return;
TileEntity te = worldIn.getTileEntity(pos);
if (te instanceof RedstoneMachineTileEntity) {
// Forego loot table as they scare me when this much data is needed
RedstoneMachineTileEntity machine = (RedstoneMachineTileEntity) te;
ItemStack item = new ItemStack(this, 1);
writeAdditionalItemData(state, worldIn, pos);
if (dropState) {
machine.storeTileData(item);
} else {
InventoryHelper.dropInventoryItems(worldIn, pos, machine);
}
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), item);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.onReplaced(state, worldIn, pos, newState, isMoving);
}
public void writeAdditionalItemData(BlockState state, World worldIn, BlockPos pos) {
}
public boolean blockMatches(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving){
return state.getBlock() == newState.getBlock();
}
@OnlyIn(Dist.CLIENT)
@Override
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
if (!stack.hasTag())
return;
CompoundNBT compound = stack.getTag();
if (compound.contains("BlockEntityTag", Constants.NBT.TAG_COMPOUND)) {
CompoundNBT tags = compound.getCompound("BlockEntityTag");
if (tags.contains("Disguise", Constants.NBT.TAG_COMPOUND)) {
ItemStack disguise = ItemStack.read(tags.getCompound("Disguise"));
if (disguise != ItemStack.EMPTY) {
tooltip.add(new TranslationTextComponent(Util.prefix("hud.disguise")).applyTextStyles(TextFormatting.GRAY, TextFormatting.BOLD));
tooltip.add(disguise.getDisplayName());
}
}
if (tags.contains("Items", Constants.NBT.TAG_LIST)) {
ListNBT items = tags.getList("Items", Constants.NBT.TAG_LIST);
if (items.size() > 0) {
tooltip.add(new TranslationTextComponent(Util.prefix("hud.items")).applyTextStyles(TextFormatting.GRAY, TextFormatting.BOLD));
}
for (int i = 0; i < items.size(); ++i) {
CompoundNBT itemTag = items.getCompound(i);
int slot = itemTag.getByte("Slot") & 255;
ItemStack item = ItemStack.read(itemTag);
tooltip.add(new TranslationTextComponent(Util.prefix("hud.slot"), slot, item.getDisplayName(), item.getCount()).applyTextStyles(TextFormatting.GRAY, TextFormatting.BOLD));
}
}
}
}
/////////////////////////
// BlockContainer Code //
/////////////////////////
/**
* Called on both Client and Server when World#addBlockEvent is called. On the Server, this may perform additional
* changes to the world, like pistons replacing the block with an extended base. On the client, the update may
* involve replacing tile entities, playing sounds, or performing other visual actions to reflect the server side
* changes.
*/
@Override
public boolean eventReceived(BlockState state, World worldIn, BlockPos pos, int id, int param) {
super.eventReceived(state, worldIn, pos, id, param);
TileEntity tileentity = worldIn.getTileEntity(pos);
return tileentity != null && tileentity.receiveClientEvent(id, param);
}
/////////////////////////
// BlockInventory Code //
/////////////////////////
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Nonnull
@Override
public abstract TileEntity createTileEntity(BlockState state, IBlockReader world);
@Override
public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
if (!worldIn.isRemote)
return this.openGui(player, worldIn, pos);
return true;
}
@Override
public boolean canHarvestBlock(BlockState state, IBlockReader world, BlockPos pos, PlayerEntity player) {
return true;
}
}