Add Multipart add/remove hooks
This commit is contained in:
parent
835d75f06e
commit
32d645776e
|
@ -120,7 +120,6 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
Entry e = new Entry(this, partState, null);
|
||||
if (partState.getMultipart() instanceof MultipartEntityProvider) {
|
||||
e.entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this);
|
||||
|
@ -128,17 +127,20 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
|
|||
}
|
||||
parts.add(e);
|
||||
|
||||
partState.onPartAdded(e);
|
||||
|
||||
invalidateSidePartCache();
|
||||
updateWorld();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(MultipartView view) {
|
||||
if (view.getContainer() != this) {
|
||||
if (view.getContainer() != this || !(view instanceof Entry)) {
|
||||
return;
|
||||
}
|
||||
|
||||
parts.removeIf(e -> e.state == view.getState() && e.entity == view.getEntity());
|
||||
parts.remove(view);
|
||||
view.getState().onPartRemoved(view);
|
||||
|
||||
if (parts.isEmpty()) {
|
||||
world.setBlockState(pos, Blocks.AIR.getDefaultState());
|
||||
|
@ -150,18 +152,19 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
|
|||
|
||||
@Override
|
||||
public boolean breakPart(MultipartView view) {
|
||||
Optional<Entry> entry = parts.stream().filter(e -> e.state == view.getState() && e.entity == view.getEntity()).findFirst();
|
||||
if (!entry.isPresent()) {
|
||||
if (view.getContainer() != this || !(view instanceof Entry)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Entry e = (Entry)view;
|
||||
|
||||
if (world instanceof ServerWorld) {
|
||||
List<ItemStack> drops = getDroppedStacks(entry.get(), (ServerWorld)world, pos);
|
||||
List<ItemStack> drops = getDroppedStacks(e, (ServerWorld)world, pos);
|
||||
drops.forEach(stack -> Block.dropStack(world, pos, stack));
|
||||
// TODO: don't drop if player is creative
|
||||
}
|
||||
|
||||
remove(view);
|
||||
remove(e);
|
||||
|
||||
updateWorld();
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import net.minecraft.world.loot.LootSupplier;
|
|||
import net.minecraft.world.loot.LootTables;
|
||||
import net.minecraft.world.loot.context.LootContext;
|
||||
import net.shadowfacts.simplemultipart.SimpleMultipart;
|
||||
import net.shadowfacts.simplemultipart.container.MultipartContainer;
|
||||
import net.shadowfacts.simplemultipart.util.MultipartPlacementContext;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -143,4 +144,21 @@ public abstract class Multipart {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after this multipart (and it's entity, if there is one) has been added to the container.
|
||||
* @param view The view of this part.
|
||||
*/
|
||||
@Deprecated
|
||||
public void onPartAdded(MultipartView view) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called <b>after</b> this part has been removed from its container.
|
||||
* @param view The view of this part.
|
||||
* The multipart entity and container in this view are still present, but the part is no longer in the container.
|
||||
*/
|
||||
@Deprecated
|
||||
public void onPartRemoved(MultipartView view) {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,4 +57,20 @@ public class MultipartState extends AbstractPropertyContainer<Multipart, Multipa
|
|||
return owner.activate(view, side, player, hand);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Multipart#onPartAdded(MultipartView)
|
||||
*/
|
||||
public void onPartAdded(MultipartView view) {
|
||||
//noinspection deprecated
|
||||
owner.onPartAdded(view);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Multipart#onPartRemoved(MultipartView)
|
||||
*/
|
||||
public void onPartRemoved(MultipartView view) {
|
||||
//noinspection deprecated
|
||||
owner.onPartRemoved(view);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue