Skip to content

Commit d67d055

Browse files
Clean up/implement item drop API
1 parent 86945a8 commit d67d055

File tree

6 files changed

+76
-15
lines changed

6 files changed

+76
-15
lines changed

parallelworlds/src/main/java/parallelmc/parallelworlds/ParallelWorlds.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
import com.github.retrooper.packetevents.event.PacketListenerPriority;
55
import org.bukkit.plugin.java.JavaPlugin;
66
import parallelmc.parallelworlds.events.BlockPacketListener;
7+
import parallelmc.parallelworlds.events.BreakBlockListener;
78
import parallelmc.parallelworlds.registry.ParallelBlockRegistry;
89

910
public final class ParallelWorlds extends JavaPlugin {
1011

1112
@Override
1213
public void onLoad() {
13-
1414
PacketEvents.getAPI().getEventManager().registerListener(new BlockPacketListener(ParallelBlockRegistry.getFirstCustomId()), PacketListenerPriority.HIGHEST);
1515
}
1616

1717
@Override
1818
public void onEnable() {
1919
super.onEnable();
20+
21+
getServer().getPluginManager().registerEvents(new BreakBlockListener(), this);
2022
}
2123

2224

parallelworlds/src/main/java/parallelmc/parallelworlds/ParallelWorldsBootstrapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
66
import net.minecraft.core.*;
77
import net.minecraft.core.registries.Registries;
8+
import net.minecraft.network.chat.Component;
89
import net.minecraft.resources.Identifier;
910
import net.minecraft.resources.ResourceKey;
1011
import net.minecraft.world.level.block.Block;
@@ -19,6 +20,7 @@
1920
import parallelmc.parallelworlds.blocks.TestBlock;
2021
import parallelmc.parallelworlds.registry.ParallelBlockRegistry;
2122

23+
2224
public class ParallelWorldsBootstrapper implements PluginBootstrap {
2325

2426
@Override
@@ -28,13 +30,14 @@ public void bootstrap(BootstrapContext context) {
2830

2931
ResourceKey<Block> testBlockKey = ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath("parallelutils", "testblock"));
3032
Block testBlock = new TestBlock(BlockBehaviour.Properties.of().mapColor(MapColor.STONE).strength(0.5f).sound(SoundType.AMETHYST).setId(testBlockKey));
31-
registry.registerBlock(testBlockKey, testBlock, Blocks.NOTE_BLOCK.getStateDefinition().any().setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.BANJO).setValue(NoteBlock.NOTE, 0));
33+
registry.registerBlock(testBlockKey, testBlock,
34+
Blocks.NOTE_BLOCK.getStateDefinition().any().setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.BANJO).setValue(NoteBlock.NOTE, 0), Component.literal("Test Block"), 10001);
3235

3336
ResourceKey<Block> quicksandKey = ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath("parallelutils", "quicksand"));
3437
Block quicksandBlock = new QuicksandBlock(BlockBehaviour.Properties.of().mapColor(MapColor.SAND).strength(0.25F).sound(SoundType.SAND)
3538
.dynamicShape().noOcclusion().isRedstoneConductor((blockState, blockGetter, blockPos) -> false).setId(quicksandKey));
36-
registry.registerBlock(quicksandKey, quicksandBlock, Blocks.NOTE_BLOCK.getStateDefinition().any().setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.BANJO).setValue(NoteBlock.NOTE, 1));
37-
39+
registry.registerBlock(quicksandKey, quicksandBlock,
40+
Blocks.NOTE_BLOCK.getStateDefinition().any().setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.BANJO).setValue(NoteBlock.NOTE, 1), Component.literal("Quicksand"), 10002);
3841

3942
registry.freeze();
4043
}

parallelworlds/src/main/java/parallelmc/parallelworlds/blocks/TestBlock.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mojang.serialization.MapCodec;
44
import net.minecraft.world.level.block.Block;
5+
import net.minecraft.world.level.block.SandBlock;
56

67
public class TestBlock extends Block {
78
public static final MapCodec<TestBlock> CODEC = simpleCodec(TestBlock::new);

parallelworlds/src/main/java/parallelmc/parallelworlds/events/BlockPacketListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class BlockPacketListener implements PacketListener {
3434

3535
public BlockPacketListener(int firstCustomId) {
3636
this.firstCustomId = firstCustomId;
37-
this.defaultReplaceState = Block.BLOCK_STATE_REGISTRY.getId(Blocks.NOTE_BLOCK.defaultBlockState());;
37+
this.defaultReplaceState = Block.BLOCK_STATE_REGISTRY.getId(Blocks.NOTE_BLOCK.defaultBlockState());
3838
this.registry = ParallelBlockRegistry.getInstance();
3939
}
4040

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,51 @@
11
package parallelmc.parallelworlds.events;
22

3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.server.level.ServerLevel;
5+
import net.minecraft.world.item.ItemStack;
36
import net.minecraft.world.item.Items;
47
import net.minecraft.world.level.block.Block;
58
import net.minecraft.world.level.block.ScaffoldingBlock;
69
import net.minecraft.world.level.block.state.BlockState;
10+
import org.bukkit.Location;
11+
import org.bukkit.craftbukkit.CraftWorld;
712
import org.bukkit.craftbukkit.block.CraftBlock;
813
import org.bukkit.event.EventHandler;
914
import org.bukkit.event.EventPriority;
1015
import org.bukkit.event.Listener;
1116
import org.bukkit.event.block.BlockBreakEvent;
17+
import parallelmc.parallelworlds.registry.ParallelBlockRegistry;
18+
19+
import java.util.List;
1220

1321
public class BreakBlockListener implements Listener {
1422

23+
private final ParallelBlockRegistry blockRegistry;
24+
25+
public BreakBlockListener() {
26+
blockRegistry = ParallelBlockRegistry.getInstance();
27+
}
28+
1529
@EventHandler(priority = EventPriority.HIGHEST)
1630
public void handleBlockBreak(BlockBreakEvent event) {
1731
CraftBlock cb = (CraftBlock)event.getBlock();
1832

19-
2033
BlockState blockState = cb.getNMS();
2134

2235
int id = Block.getId(blockState);
36+
37+
List<ItemStack> drops = blockRegistry.getDrops(id);
38+
39+
if (drops != null) {
40+
// This is a registered Parallel Block
41+
event.setDropItems(false); // Stop normal drops
42+
43+
ServerLevel level = cb.getCraftWorld().getHandle();
44+
BlockPos pos = cb.getPosition();
45+
46+
for (ItemStack item : drops) {
47+
Block.popResource(level, pos, item);
48+
}
49+
}
2350
}
2451
}

parallelworlds/src/main/java/parallelmc/parallelworlds/registry/ParallelBlockRegistry.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package parallelmc.parallelworlds.registry;
22

33
import net.minecraft.core.*;
4+
import net.minecraft.core.component.DataComponentPatch;
5+
import net.minecraft.core.component.DataComponents;
6+
import net.minecraft.core.component.TypedDataComponent;
47
import net.minecraft.core.registries.BuiltInRegistries;
58
import net.minecraft.core.registries.Registries;
9+
import net.minecraft.network.chat.Component;
610
import net.minecraft.resources.ResourceKey;
11+
import net.minecraft.world.item.ItemStack;
12+
import net.minecraft.world.item.Items;
13+
import net.minecraft.world.item.component.CustomModelData;
714
import net.minecraft.world.level.block.Block;
815
import net.minecraft.world.level.block.Blocks;
9-
import net.minecraft.world.level.block.NoteBlock;
1016
import net.minecraft.world.level.block.state.BlockState;
11-
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
17+
import net.minecraft.world.level.storage.loot.functions.*;
1218
import org.jetbrains.annotations.NotNull;
1319
import org.jetbrains.annotations.Nullable;
1420

15-
import java.util.ArrayDeque;
16-
import java.util.HashMap;
17-
import java.util.Map;
18-
import java.util.Queue;
21+
import java.util.*;
1922
import java.util.logging.Level;
2023
import java.util.logging.Logger;
2124

@@ -33,9 +36,11 @@ public class ParallelBlockRegistry {
3336
// this can be converted into map of BlockType -> Queue<Integer> since we can choose values entirely server side
3437
private final HashMap<BlockState, Integer> availableStates;
3538

39+
private final HashMap<Integer, List<ItemStack>> dropMap;
40+
3641
private boolean frozen = false;
3742

38-
private ParallelBlockRegistry() throws RuntimeException {
43+
private ParallelBlockRegistry() throws RuntimeException, NoSuchMethodException {
3944
// This is a ridiculous hack to force internal blocks to register first
4045
Block dummy = Blocks.DIRT;
4146
Logger.getGlobal().log(Level.INFO, dummy.toString());
@@ -50,6 +55,7 @@ private ParallelBlockRegistry() throws RuntimeException {
5055

5156
stateMap = new HashMap<>();
5257
availableStates = new HashMap<>();
58+
dropMap = new HashMap<>();
5359

5460
// TODO: Fill available states
5561
for (BlockState state : Blocks.NOTE_BLOCK.getStateDefinition().getPossibleStates()) {
@@ -69,7 +75,12 @@ private void addFullBlock(BlockState state) {
6975

7076
public static ParallelBlockRegistry getInstance() {
7177
if (instance == null) {
72-
instance = new ParallelBlockRegistry();
78+
try {
79+
instance = new ParallelBlockRegistry();
80+
} catch (NoSuchMethodException e) {
81+
e.printStackTrace();
82+
return null;
83+
}
7384
}
7485

7586
return instance;
@@ -100,7 +111,18 @@ public void freeze() {
100111
return null;
101112
}
102113

103-
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockState targetBlockstate) {
114+
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockState targetBlockstate, Component name, float customModelData) {
115+
ItemStack stack = Items.PAPER.getDefaultInstance();
116+
117+
stack.applyComponentsAndValidate(
118+
DataComponentPatch.builder()
119+
.set(TypedDataComponent.createUnchecked(DataComponents.CUSTOM_MODEL_DATA, new CustomModelData(List.of(customModelData), List.of(), List.of(), List.of())))
120+
.set(TypedDataComponent.createUnchecked(DataComponents.ITEM_NAME, name)).build());
121+
122+
return registerBlock(key, block, targetBlockstate, List.of(stack));
123+
}
124+
125+
public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, BlockState targetBlockstate, List<ItemStack> item) {
104126
if (frozen) return false;
105127

106128
if (!availableStates.containsKey(targetBlockstate)) throw new IllegalStateException("Block state is already used or does not exist");
@@ -113,6 +135,7 @@ public boolean registerBlock(ResourceKey<@NotNull Block> key, Block block, Block
113135
Integer stateId = availableStates.remove(targetBlockstate);
114136

115137
stateMap.put(Block.BLOCK_STATE_REGISTRY.size(), stateId); // Map the new block state to an unused state
138+
dropMap.put(Block.BLOCK_STATE_REGISTRY.size(), item);
116139

117140
Block.BLOCK_STATE_REGISTRY.add(blockState);
118141

@@ -130,4 +153,9 @@ public static int getFirstCustomId() {
130153
public Integer getMappedState(int state) {
131154
return stateMap.get(state);
132155
}
156+
157+
@Nullable
158+
public List<ItemStack> getDrops(int state) {
159+
return dropMap.get(state);
160+
}
133161
}

0 commit comments

Comments
 (0)