Skip to content

Commit

Permalink
Add dropping of minecart contents if it's not a shulker box
Browse files Browse the repository at this point in the history
  • Loading branch information
P3pp3rF1y committed Dec 27, 2024
1 parent 46bb4c1 commit b7cc220
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
Expand All @@ -15,6 +16,7 @@
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.level.gameevent.GameEvent;
Expand All @@ -34,6 +36,7 @@
import net.p3pp3rf1y.sophisticatedstorage.block.*;
import net.p3pp3rf1y.sophisticatedstorage.init.ModBlocks;
import net.p3pp3rf1y.sophisticatedstorage.item.BarrelBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.ShulkerBoxItem;
import net.p3pp3rf1y.sophisticatedstorage.item.StorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorage.item.WoodStorageBlockItem;
import net.p3pp3rf1y.sophisticatedstorageinmotion.common.gui.MovingLimitedBarrelContainerMenu;
Expand Down Expand Up @@ -139,15 +142,9 @@ private void onContentsChanged() {
MovingStorageData.get(storageId).setDirty();
}

public void dropAllItems() {
//TODO implement
}

public void startOpen(Player player) {
entity.gameEvent(GameEvent.CONTAINER_OPEN, player);
PiglinAi.angerNearbyPiglins(player, true);

//TODO implement openers tracking? Would require stopOpen override in that case as well
}

public void stopOpen(Player player) {
Expand Down Expand Up @@ -319,4 +316,21 @@ public MovingStorageContainerMenu<? extends Entity> createMenu(int id, Player pl
public static boolean isLimitedBarrel(ItemStack storageItem) {
return storageItem.getItem() instanceof BlockItem blockItem && blockItem.getBlock() instanceof LimitedBarrelBlock;
}

public void onDestroy() {
if (entity.level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
ItemStack drop = new ItemStack(entity.getDropItem());
drop.set(ModDataComponents.STORAGE_ITEM, SimpleItemContent.copyOf(entity.getStorageItem()));
drop.set(DataComponents.CUSTOM_NAME, entity.getCustomName());
entity.spawnAtLocation(drop);
if (!(entity.getStorageItem().getItem() instanceof ShulkerBoxItem)) {
dropAllItems();
}
}
}

private void dropAllItems() {
InventoryHelper.dropItems(getStorageWrapper().getInventoryHandler(), entity.level(), entity.position().x(), entity.position().y(), entity.position().z());
InventoryHelper.dropItems(getStorageWrapper().getUpgradeHandler(), entity.level(), entity.position().x(), entity.position().y(), entity.position().z());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package net.p3pp3rf1y.sophisticatedstorageinmotion.entity;

import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;

public interface IMovingStorageEntity {
Vec3 position();

int getId();

ItemStack getStorageItem();

void setStorageItem(ItemStack storageItem);

EntityStorageHolder getStorageHolder();
EntityStorageHolder<?> getStorageHolder();

Item getDropItem();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -23,7 +22,6 @@
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.p3pp3rf1y.sophisticatedcore.init.ModCoreDataComponents;
import net.p3pp3rf1y.sophisticatedcore.inventory.InventoryHandler;
Expand Down Expand Up @@ -84,17 +82,11 @@ public StorageMinecart(Level level, double x, double y, double z) {
@Override
public void destroy(DamageSource source) {
this.kill();
if (level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
ItemStack drop = new ItemStack(ModItems.STORAGE_MINECART.get());
drop.set(ModDataComponents.STORAGE_ITEM, SimpleItemContent.copyOf(getStorageItem()));
drop.set(DataComponents.CUSTOM_NAME, getCustomName());
spawnAtLocation(drop);
entityStorageHolder.dropAllItems();
}
getStorageHolder().onDestroy();
}

@Override
protected Item getDropItem() {
public Item getDropItem() {
return ModItems.STORAGE_MINECART.get();
}

Expand Down Expand Up @@ -184,7 +176,7 @@ public void readChestVehicleSaveData(CompoundTag tag, HolderLookup.Provider leve

@Override
public void chestVehicleDestroyed(DamageSource damageSource, Level level, Entity p_entity) {
//TODO implement
//noop
}

@Override
Expand Down

0 comments on commit b7cc220

Please sign in to comment.