Make stuff drop stuff.
This commit is contained in:
@@ -84,7 +84,10 @@ public final class BusCableBlock extends Block {
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
public BusCableBlock() {
|
||||
super(Properties.create(Material.IRON).sound(SoundType.METAL));
|
||||
super(Properties
|
||||
.create(Material.IRON)
|
||||
.sound(SoundType.METAL)
|
||||
.hardnessAndResistance(1.5F, 6.0F));
|
||||
|
||||
BlockState defaultState = getStateContainer().getBaseState();
|
||||
for (final EnumProperty<ConnectionType> property : FACING_TO_CONNECTION_MAP.values()) {
|
||||
@@ -166,6 +169,26 @@ public final class BusCableBlock extends Block {
|
||||
return ActionResultType.SUCCESS;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public List<ItemStack> getDrops(final BlockState state, final LootContext.Builder builder) {
|
||||
final List<ItemStack> drops = new ArrayList<>(super.getDrops(state, builder));
|
||||
|
||||
int plugCount = 0;
|
||||
for (final Direction side : FACING_VALUES) {
|
||||
final ConnectionType connectionType = state.get(FACING_TO_CONNECTION_MAP.get(side));
|
||||
if (connectionType == ConnectionType.PLUG) {
|
||||
plugCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (plugCount > 0) {
|
||||
drops.add(new ItemStack(Items.BUS_INTERFACE_ITEM.get(), plugCount));
|
||||
}
|
||||
|
||||
return drops;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getStateForPlacement(final BlockItemUseContext context) {
|
||||
BlockState state = getDefaultState();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package li.cil.oc2.common.block;
|
||||
|
||||
import li.cil.oc2.api.API;
|
||||
import li.cil.oc2.client.gui.TerminalScreen;
|
||||
import li.cil.oc2.common.block.entity.ComputerTileEntity;
|
||||
import li.cil.oc2.common.container.ComputerContainer;
|
||||
@@ -21,6 +22,7 @@ import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ActionResultType;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.BlockRayTraceResult;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
@@ -31,8 +33,13 @@ import net.minecraftforge.fml.network.NetworkHooks;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class ComputerBlock extends HorizontalBlock {
|
||||
public static final ResourceLocation CONTENTS = new ResourceLocation(API.MOD_ID, "contents");
|
||||
|
||||
public ComputerBlock() {
|
||||
super(Properties.create(Material.IRON).sound(SoundType.METAL));
|
||||
super(Properties
|
||||
.create(Material.IRON)
|
||||
.sound(SoundType.METAL)
|
||||
.hardnessAndResistance(1.5F, 6.0F));
|
||||
setDefaultState(getStateContainer().getBaseState().with(HORIZONTAL_FACING, Direction.NORTH));
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,10 @@ import javax.annotation.Nullable;
|
||||
|
||||
public final class RedstoneInterfaceBlock extends HorizontalBlock {
|
||||
public RedstoneInterfaceBlock() {
|
||||
super(Properties.create(Material.IRON).sound(SoundType.METAL));
|
||||
super(Properties
|
||||
.create(Material.IRON)
|
||||
.sound(SoundType.METAL)
|
||||
.hardnessAndResistance(1.5F, 6.0F));
|
||||
setDefaultState(getStateContainer().getBaseState().with(HORIZONTAL_FACING, Direction.NORTH));
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,10 @@ import net.minecraft.util.Direction;
|
||||
|
||||
public final class ScreenBlock extends HorizontalBlock {
|
||||
public ScreenBlock() {
|
||||
super(Properties.create(Material.IRON).sound(SoundType.METAL));
|
||||
super(Properties
|
||||
.create(Material.IRON)
|
||||
.sound(SoundType.METAL)
|
||||
.hardnessAndResistance(1.5F, 6.0F));
|
||||
setDefaultState(getStateContainer().getBaseState().with(HORIZONTAL_FACING, Direction.NORTH));
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic
|
||||
private static final String VFS_NBT_TAG_NAME = "vfs";
|
||||
private static final String RUNNER_NBT_TAG_NAME = "runner";
|
||||
private static final String RUN_STATE_NBT_TAG_NAME = "runState";
|
||||
private static final String ITEMS_NBT_TAG_NAME = "items";
|
||||
public static final String ITEMS_NBT_TAG_NAME = "items";
|
||||
|
||||
private static final int DEVICE_LOAD_RETRY_INTERVAL = 10 * 20; // In ticks.
|
||||
private static final int VFS_INTERRUPT = 0x4;
|
||||
|
||||
@@ -16,6 +16,7 @@ public final class DataGenerators {
|
||||
final ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
|
||||
generator.addProvider(new BlockStates(generator, existingFileHelper));
|
||||
generator.addProvider(new ItemModels(generator, existingFileHelper));
|
||||
generator.addProvider(new LootTables(generator));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
82
src/main/java/li/cil/oc2/data/LootTables.java
Normal file
82
src/main/java/li/cil/oc2/data/LootTables.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package li.cil.oc2.data;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import li.cil.oc2.api.API;
|
||||
import li.cil.oc2.common.block.ComputerBlock;
|
||||
import li.cil.oc2.common.block.entity.ComputerTileEntity;
|
||||
import li.cil.oc2.common.init.Blocks;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.LootTableProvider;
|
||||
import net.minecraft.data.loot.BlockLootTables;
|
||||
import net.minecraft.loot.*;
|
||||
import net.minecraft.loot.functions.CopyNbt;
|
||||
import net.minecraft.loot.functions.SetContents;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public final class LootTables extends LootTableProvider {
|
||||
private static final String BLOCK_ENTITY_TAG_NAME_IN_ITEM = "BlockEntityTag";
|
||||
|
||||
public LootTables(final DataGenerator generator) {
|
||||
super(generator);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void validate(final Map<ResourceLocation, LootTable> map, final ValidationTracker validationtracker) {
|
||||
map.forEach((location, table) -> LootTableManager.validateLootTable(validationtracker, location, table));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Pair<Supplier<Consumer<BiConsumer<ResourceLocation, LootTable.Builder>>>, LootParameterSet>> getTables() {
|
||||
return Collections.singletonList(Pair.of(ModBlockLootTables::new, LootParameterSets.BLOCK));
|
||||
}
|
||||
|
||||
private static String concatNbtPath(final String... paths) {
|
||||
return String.join(".", paths);
|
||||
}
|
||||
|
||||
private static final class ModBlockLootTables extends BlockLootTables {
|
||||
@Override
|
||||
protected void addTables() {
|
||||
registerDropSelfLootTable(Blocks.BUS_CABLE_BLOCK.get());
|
||||
registerDropSelfLootTable(Blocks.REDSTONE_INTERFACE_BLOCK.get());
|
||||
registerDropSelfLootTable(Blocks.SCREEN_BLOCK.get());
|
||||
|
||||
registerLootTable(Blocks.COMPUTER_BLOCK.get(), ModBlockLootTables::droppingWithInventory);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterable<Block> getKnownBlocks() {
|
||||
return StreamSupport.stream(super.getKnownBlocks().spliterator(), false)
|
||||
.filter(block -> requireNonNull(block.getRegistryName()).getNamespace().equals(API.MOD_ID))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static LootTable.Builder droppingWithInventory(final Block block) {
|
||||
return LootTable.builder()
|
||||
.addLootPool(withSurvivesExplosion(block, LootPool.builder()
|
||||
.rolls(ConstantRange.of(1))
|
||||
.addEntry(ItemLootEntry.builder(block)
|
||||
.acceptFunction(CopyNbt.builder(CopyNbt.Source.BLOCK_ENTITY)
|
||||
.addOperation(ComputerTileEntity.ITEMS_NBT_TAG_NAME,
|
||||
concatNbtPath(BLOCK_ENTITY_TAG_NAME_IN_ITEM, ComputerTileEntity.ITEMS_NBT_TAG_NAME),
|
||||
CopyNbt.Action.REPLACE)
|
||||
)
|
||||
.acceptFunction(SetContents.builderIn()
|
||||
.addLootEntry(DynamicLootEntry.func_216162_a(ComputerBlock.CONTENTS)))
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "oc2:bus_cable"
|
||||
}
|
||||
],
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "minecraft:survives_explosion"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
41
src/main/resources/data/oc2/loot_tables/blocks/computer.json
Normal file
41
src/main/resources/data/oc2/loot_tables/blocks/computer.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"functions": [
|
||||
{
|
||||
"function": "minecraft:copy_nbt",
|
||||
"source": "block_entity",
|
||||
"ops": [
|
||||
{
|
||||
"source": "items",
|
||||
"target": "BlockEntityTag.items",
|
||||
"op": "replace"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"function": "minecraft:set_contents",
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:dynamic",
|
||||
"name": "oc2:contents"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"name": "oc2:computer"
|
||||
}
|
||||
],
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "minecraft:survives_explosion"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "oc2:redstone_interface"
|
||||
}
|
||||
],
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "minecraft:survives_explosion"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
19
src/main/resources/data/oc2/loot_tables/blocks/screen.json
Normal file
19
src/main/resources/data/oc2/loot_tables/blocks/screen.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "oc2:screen"
|
||||
}
|
||||
],
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "minecraft:survives_explosion"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user