Improve storage block breaking
This commit is contained in:
@@ -28,6 +28,7 @@ import com.raoulvdberge.refinedstorage.item.*;
|
||||
import com.raoulvdberge.refinedstorage.item.blockitem.ControllerBlockItem;
|
||||
import com.raoulvdberge.refinedstorage.item.blockitem.StorageBlockItem;
|
||||
import com.raoulvdberge.refinedstorage.item.group.MainItemGroup;
|
||||
import com.raoulvdberge.refinedstorage.loottable.StorageBlockLootFunctionSerializer;
|
||||
import com.raoulvdberge.refinedstorage.network.NetworkHandler;
|
||||
import com.raoulvdberge.refinedstorage.recipe.UpgradeWithEnchantedBookRecipeSerializer;
|
||||
import com.raoulvdberge.refinedstorage.tile.*;
|
||||
@@ -42,6 +43,7 @@ import net.minecraft.item.crafting.IRecipeSerializer;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityType;
|
||||
import net.minecraft.world.storage.loot.functions.LootFunctionManager;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.extensions.IForgeContainerType;
|
||||
@@ -105,6 +107,8 @@ public final class RS {
|
||||
API.instance().getNetworkNodeRegistry().add(StorageNetworkNode.CREATIVE_STORAGE_BLOCK_ID, (tag, world, pos) -> readAndReturn(tag, new StorageNetworkNode(world, pos, ItemStorageType.CREATIVE)));
|
||||
|
||||
API.instance().getGridManager().add(GridBlockGridFactory.ID, new GridBlockGridFactory());
|
||||
|
||||
LootFunctionManager.registerFunction(new StorageBlockLootFunctionSerializer());
|
||||
}
|
||||
|
||||
private INetworkNode readAndReturn(CompoundNBT tag, NetworkNode node) {
|
||||
|
@@ -9,6 +9,10 @@ import javax.annotation.Nonnull;
|
||||
*/
|
||||
public interface INetworkNodeProxy<T extends INetworkNode> {
|
||||
/**
|
||||
* Returns the node.
|
||||
* Needs to work on the client and the server.
|
||||
* If there is no node present, don't silently return null but throw an exception since the game is in a bad state if that happens.
|
||||
*
|
||||
* @return the node
|
||||
*/
|
||||
@Nonnull
|
||||
|
@@ -1,9 +1,5 @@
|
||||
package com.raoulvdberge.refinedstorage.block;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode;
|
||||
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNodeManager;
|
||||
import com.raoulvdberge.refinedstorage.api.util.Action;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.API;
|
||||
import com.raoulvdberge.refinedstorage.tile.NetworkNodeTile;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
@@ -14,9 +10,7 @@ import net.minecraft.state.StateContainer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IWorld;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
public abstract class NodeBlock extends BaseBlock {
|
||||
@@ -30,24 +24,6 @@ public abstract class NodeBlock extends BaseBlock {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerDestroy(IWorld world, BlockPos pos, BlockState state) {
|
||||
super.onPlayerDestroy(world, pos, state);
|
||||
|
||||
if (!world.isRemote()) {
|
||||
INetworkNodeManager manager = API.instance().getNetworkNodeManager((ServerWorld) world);
|
||||
|
||||
INetworkNode node = manager.getNode(pos);
|
||||
|
||||
manager.removeNode(pos);
|
||||
manager.markForSaving();
|
||||
|
||||
if (node != null && node.getNetwork() != null) {
|
||||
node.getNetwork().getNodeGraph().invalidate(Action.PERFORM, node.getNetwork().world(), node.getNetwork().getPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
|
||||
|
@@ -12,13 +12,9 @@ import net.minecraft.block.BlockState;
|
||||
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.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.BlockRayTraceResult;
|
||||
import net.minecraft.world.IBlockReader;
|
||||
@@ -58,26 +54,6 @@ public class StorageBlock extends NodeBlock {
|
||||
super.onBlockPlacedBy(world, pos, state, entity, stack);
|
||||
}
|
||||
|
||||
// TODO Improve this dropping mechanism
|
||||
@Override
|
||||
public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
|
||||
if (state.getBlock() != newState.getBlock()) {
|
||||
TileEntity tile = world.getTileEntity(pos);
|
||||
|
||||
if (tile instanceof StorageTile) {
|
||||
@SuppressWarnings("deprecation")
|
||||
ItemStack drop = new ItemStack(Item.getItemFromBlock(this));
|
||||
|
||||
drop.setTag(new CompoundNBT());
|
||||
drop.getTag().putUniqueId(StorageNetworkNode.NBT_ID, ((StorageTile) tile).getNode().getStorageId());
|
||||
|
||||
InventoryHelper.dropItems(world, pos, NonNullList.from(ItemStack.EMPTY, drop));
|
||||
}
|
||||
|
||||
super.onReplaced(state, world, pos, newState, isMoving);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTileEntity(BlockState state) {
|
||||
return true;
|
||||
|
@@ -0,0 +1,24 @@
|
||||
package com.raoulvdberge.refinedstorage.loottable;
|
||||
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.storage.StorageNetworkNode;
|
||||
import com.raoulvdberge.refinedstorage.tile.StorageTile;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.storage.loot.LootContext;
|
||||
import net.minecraft.world.storage.loot.LootParameters;
|
||||
import net.minecraft.world.storage.loot.functions.ILootFunction;
|
||||
|
||||
public class StorageBlockLootFunction implements ILootFunction {
|
||||
@Override
|
||||
public ItemStack apply(ItemStack stack, LootContext lootContext) {
|
||||
TileEntity tile = lootContext.get(LootParameters.BLOCK_ENTITY);
|
||||
|
||||
if (tile != null) {
|
||||
stack.setTag(new CompoundNBT());
|
||||
stack.getTag().putUniqueId(StorageNetworkNode.NBT_ID, ((StorageTile) tile).getRemovedNode().getStorageId());
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package com.raoulvdberge.refinedstorage.loottable;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.raoulvdberge.refinedstorage.RS;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.storage.loot.functions.ILootFunction;
|
||||
|
||||
public class StorageBlockLootFunctionSerializer extends ILootFunction.Serializer<StorageBlockLootFunction> {
|
||||
public StorageBlockLootFunctionSerializer() {
|
||||
super(new ResourceLocation(RS.ID, "storage_block"), StorageBlockLootFunction.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(JsonObject jsonObject, StorageBlockLootFunction storageBlockLootFunction, JsonSerializationContext jsonSerializationContext) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageBlockLootFunction deserialize(JsonObject jsonObject, JsonDeserializationContext jsonDeserializationContext) {
|
||||
return new StorageBlockLootFunction();
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package com.raoulvdberge.refinedstorage.tile;
|
||||
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode;
|
||||
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNodeManager;
|
||||
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNodeProxy;
|
||||
import com.raoulvdberge.refinedstorage.api.util.Action;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.API;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.ICoverable;
|
||||
import com.raoulvdberge.refinedstorage.apiimpl.network.node.NetworkNode;
|
||||
@@ -32,6 +33,8 @@ public abstract class NetworkNodeTile<N extends NetworkNode> extends BaseTile im
|
||||
|
||||
private LazyOptional<INetworkNodeProxy<N>> networkNodeProxy = LazyOptional.of(() -> this);
|
||||
|
||||
private N removedNode;
|
||||
|
||||
public NetworkNodeTile(TileEntityType<?> tileType) {
|
||||
super(tileType);
|
||||
|
||||
@@ -83,13 +86,52 @@ public abstract class NetworkNodeTile<N extends NetworkNode> extends BaseTile im
|
||||
INetworkNode node = manager.getNode(pos);
|
||||
|
||||
if (node == null) {
|
||||
manager.setNode(pos, node = createNode(world, pos));
|
||||
manager.markForSaving();
|
||||
throw new RuntimeException("No network node present at " + pos.toString());
|
||||
}
|
||||
|
||||
return (N) node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate() {
|
||||
super.validate();
|
||||
|
||||
if (!world.isRemote) {
|
||||
INetworkNodeManager manager = API.instance().getNetworkNodeManager((ServerWorld) world);
|
||||
|
||||
if (manager.getNode(pos) == null) {
|
||||
manager.setNode(pos, createNode(world, pos));
|
||||
manager.markForSaving();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
super.remove();
|
||||
|
||||
if (!world.isRemote) {
|
||||
INetworkNodeManager manager = API.instance().getNetworkNodeManager((ServerWorld) world);
|
||||
|
||||
INetworkNode node = manager.getNode(pos);
|
||||
|
||||
if (node != null) {
|
||||
removedNode = (N) node;
|
||||
}
|
||||
|
||||
manager.removeNode(pos);
|
||||
manager.markForSaving();
|
||||
|
||||
if (node != null && node.getNetwork() != null) {
|
||||
node.getNetwork().getNodeGraph().invalidate(Action.PERFORM, node.getNetwork().world(), node.getNetwork().getPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public N getRemovedNode() {
|
||||
return removedNode;
|
||||
}
|
||||
|
||||
public abstract N createNode(World world, BlockPos pos);
|
||||
|
||||
@Nonnull
|
||||
|
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "refinedstorage:16k_storage_block",
|
||||
"functions": [
|
||||
{
|
||||
"function": "refinedstorage:storage_block"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "refinedstorage:1k_storage_block",
|
||||
"functions": [
|
||||
{
|
||||
"function": "refinedstorage:storage_block"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "refinedstorage:4k_storage_block",
|
||||
"functions": [
|
||||
{
|
||||
"function": "refinedstorage:storage_block"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "refinedstorage:64k_storage_block",
|
||||
"functions": [
|
||||
{
|
||||
"function": "refinedstorage:storage_block"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"type": "minecraft:block",
|
||||
"pools": [
|
||||
{
|
||||
"rolls": 1,
|
||||
"entries": [
|
||||
{
|
||||
"type": "minecraft:item",
|
||||
"name": "refinedstorage:creative_storage_block",
|
||||
"functions": [
|
||||
{
|
||||
"function": "refinedstorage:storage_block"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user