Skip to content

Commit 6750b0e

Browse files
committed
Use ID abstractions to simplify network element logic
1 parent 273da61 commit 6750b0e

9 files changed

Lines changed: 218 additions & 709 deletions

File tree

src/main/java/org/cyclops/integratednbt/Capabilities.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/main/java/org/cyclops/integratednbt/GeneralConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.cyclops.integratednbt;
22

3+
import net.minecraftforge.fml.config.ModConfig;
4+
import org.cyclops.cyclopscore.config.ConfigurableProperty;
35
import org.cyclops.cyclopscore.config.extendedconfig.DummyConfig;
46

57
/**
@@ -9,6 +11,9 @@
911
*/
1012
public class GeneralConfig extends DummyConfig {
1113

14+
@ConfigurableProperty(category = "general", comment = "The base energy usage for the NBT Extractor.", minimalValue = 0, configLocation = ModConfig.Type.SERVER)
15+
public static int nbtExtractorBaseConsumption = 2;
16+
1217
public GeneralConfig() {
1318
super(IntegratedNbt._instance, "general");
1419
}

src/main/java/org/cyclops/integratednbt/block/BlockNbtExtractor.java

Lines changed: 19 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,54 @@
22

33
import net.minecraft.core.BlockPos;
44
import net.minecraft.core.Direction;
5-
import net.minecraft.server.level.ServerPlayer;
6-
import net.minecraft.world.Container;
7-
import net.minecraft.world.Containers;
5+
import net.minecraft.network.FriendlyByteBuf;
86
import net.minecraft.world.InteractionHand;
97
import net.minecraft.world.InteractionResult;
108
import net.minecraft.world.entity.player.Player;
11-
import net.minecraft.world.item.ItemStack;
129
import net.minecraft.world.item.context.BlockPlaceContext;
1310
import net.minecraft.world.level.Level;
1411
import net.minecraft.world.level.block.Block;
15-
import net.minecraft.world.level.block.EntityBlock;
16-
import net.minecraft.world.level.block.Mirror;
17-
import net.minecraft.world.level.block.Rotation;
1812
import net.minecraft.world.level.block.entity.BlockEntity;
1913
import net.minecraft.world.level.block.entity.BlockEntityTicker;
2014
import net.minecraft.world.level.block.entity.BlockEntityType;
2115
import net.minecraft.world.level.block.state.BlockState;
2216
import net.minecraft.world.level.block.state.StateDefinition;
17+
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
18+
import net.minecraft.world.level.block.state.properties.DirectionProperty;
2319
import net.minecraft.world.phys.BlockHitResult;
24-
import net.minecraftforge.network.NetworkHooks;
25-
import org.cyclops.integrateddynamics.core.helper.WrenchHelpers;
20+
import org.cyclops.integrateddynamics.core.block.BlockWithEntityGuiCabled;
2621
import org.cyclops.integratednbt.RegistryEntries;
2722
import org.cyclops.integratednbt.blockentity.BlockEntityNbtExtractor;
2823

29-
import javax.annotation.Nonnull;
3024
import javax.annotation.Nullable;
3125

32-
public class BlockNbtExtractor extends CabledHorizontalBlock implements EntityBlock { // TODO: extend BlockEntityActiveVariableBase, like BlockEntityProxy
26+
public class BlockNbtExtractor extends BlockWithEntityGuiCabled {
27+
28+
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
3329

3430
public BlockNbtExtractor(Properties properties) {
35-
super(properties);
36-
this.registerDefaultState(this.defaultBlockState().setValue(FACING, Direction.NORTH));
31+
super(properties, BlockEntityNbtExtractor::new);
32+
this.registerDefaultState(this.stateDefinition.any()
33+
.setValue(FACING, Direction.NORTH));
3734
}
3835

3936
@Nullable
4037
@Override
41-
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
42-
if (level.isClientSide) {
43-
return null;
44-
}
45-
return type == RegistryEntries.BLOCK_ENTITY_NBT_EXTRACTOR ? BlockEntityNbtExtractor::tick : null;
38+
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> blockEntityType) {
39+
return level.isClientSide ? null : createTickerHelper(blockEntityType, RegistryEntries.BLOCK_ENTITY_NBT_EXTRACTOR, new BlockEntityNbtExtractor.Ticker<>());
4640
}
4741

4842
@Override
4943
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
50-
super.createBlockStateDefinition(builder);
5144
builder.add(FACING);
5245
}
5346

54-
@Override
55-
public void onRemove(
56-
BlockState oldState,
57-
@Nonnull Level worldIn,
58-
@Nonnull BlockPos pos,
59-
BlockState newState,
60-
boolean isMoving
61-
) {
62-
if (oldState.getBlock() != newState.getBlock()) {
63-
BlockEntity tileEntity = worldIn.getBlockEntity(pos);
64-
if (tileEntity instanceof BlockEntityNbtExtractor) {
65-
Container inventory = (BlockEntityNbtExtractor) tileEntity;
66-
for (int slot = 0; slot < inventory.getContainerSize(); ++slot) {
67-
Containers.dropItemStack(
68-
worldIn,
69-
pos.getX(),
70-
pos.getY(),
71-
pos.getZ(),
72-
inventory.getItem(slot)
73-
);
74-
}
75-
}
76-
}
77-
super.onRemove(oldState, worldIn, pos, newState, isMoving);
78-
}
79-
8047
@Override
8148
public BlockState getStateForPlacement(BlockPlaceContext context) {
82-
return this.defaultBlockState()
83-
.setValue(FACING, context.getHorizontalDirection().getOpposite());
49+
return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection());
8450
}
8551

8652
@Override
87-
@Nonnull
88-
@SuppressWarnings("deprecation")
8953
public InteractionResult use(
9054
BlockState blockState,
9155
Level world,
@@ -94,62 +58,15 @@ public InteractionResult use(
9458
InteractionHand hand,
9559
BlockHitResult rayTraceResult
9660
) {
97-
ItemStack heldItem = player.getItemInHand(hand);
98-
if (!world.isClientSide) {
99-
ServerPlayer playerMP = (ServerPlayer) player;
100-
if (WrenchHelpers.isWrench(player, heldItem, world, blockPos, rayTraceResult.getDirection())
101-
&& player.isCrouching()
102-
) {
103-
Block.dropResources(
104-
blockState,
105-
world,
106-
blockPos,
107-
world.getBlockEntity(blockPos),
108-
player,
109-
heldItem
110-
);
111-
world.destroyBlock(blockPos, false);
112-
return InteractionResult.SUCCESS;
113-
}
114-
if (heldItem.getItem() == RegistryEntries.ITEM_NBT_EXTRACTOR_REMOTE) {
115-
return InteractionResult.PASS;
116-
}
117-
if (!player.isCrouching()) {
118-
this.playerAccess(world, blockPos, playerMP);
119-
return InteractionResult.SUCCESS;
120-
}
61+
if (!world.isClientSide() && player.getItemInHand(hand).getItem() == RegistryEntries.ITEM_NBT_EXTRACTOR_REMOTE) {
62+
return InteractionResult.PASS;
12163
}
122-
return InteractionResult.SUCCESS;
123-
}
124-
125-
public void playerAccess(Level world, BlockPos pos, ServerPlayer playerMP) {
126-
BlockEntity tileentity = world.getBlockEntity(pos);
127-
if (tileentity instanceof BlockEntityNbtExtractor) {
128-
BlockEntityNbtExtractor nbtExtractorTileEntity = (BlockEntityNbtExtractor) tileentity;
129-
nbtExtractorTileEntity.refreshVariables(true);
130-
NetworkHooks.openScreen(playerMP, nbtExtractorTileEntity, pos);
131-
}
132-
}
133-
134-
@Override
135-
@Nonnull
136-
@SuppressWarnings("deprecation")
137-
public BlockState rotate(BlockState state, Rotation rot) {
138-
return state.setValue(FACING, rot.rotate(state.getValue(FACING)));
64+
return super.use(blockState, world, blockPos, player, hand, rayTraceResult);
13965
}
14066

14167
@Override
142-
@Nonnull
143-
@SuppressWarnings("deprecation")
144-
public BlockState mirror(BlockState state, Mirror mirrorIn) {
145-
return state.rotate(mirrorIn.getRotation(state.getValue(FACING)));
146-
}
147-
148-
@org.jetbrains.annotations.Nullable
149-
@Override
150-
public BlockEntity newBlockEntity(
151-
BlockPos blockPos, BlockState blockState
152-
) {
153-
return new BlockEntityNbtExtractor(blockPos, blockState);
68+
public void writeExtraGuiData(FriendlyByteBuf packetBuffer, Level world, Player player, BlockPos blockPos, InteractionHand hand, BlockHitResult rayTraceResult) {
69+
super.writeExtraGuiData(packetBuffer, world, player, blockPos, hand, rayTraceResult);
70+
packetBuffer.writeBlockPos(blockPos);
15471
}
15572
}

src/main/java/org/cyclops/integratednbt/block/CabledHorizontalBlock.java

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)