Add Multipart add/remove hooks

This commit is contained in:
Shadowfacts 2019-01-05 10:14:52 -05:00
parent 835d75f06e
commit 32d645776e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 44 additions and 7 deletions

View File

@ -120,7 +120,6 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
return; return;
} }
Entry e = new Entry(this, partState, null); Entry e = new Entry(this, partState, null);
if (partState.getMultipart() instanceof MultipartEntityProvider) { if (partState.getMultipart() instanceof MultipartEntityProvider) {
e.entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this); e.entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this);
@ -128,17 +127,20 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
} }
parts.add(e); parts.add(e);
partState.onPartAdded(e);
invalidateSidePartCache(); invalidateSidePartCache();
updateWorld(); updateWorld();
} }
@Override @Override
public void remove(MultipartView view) { public void remove(MultipartView view) {
if (view.getContainer() != this) { if (view.getContainer() != this || !(view instanceof Entry)) {
return; return;
} }
parts.removeIf(e -> e.state == view.getState() && e.entity == view.getEntity()); parts.remove(view);
view.getState().onPartRemoved(view);
if (parts.isEmpty()) { if (parts.isEmpty()) {
world.setBlockState(pos, Blocks.AIR.getDefaultState()); world.setBlockState(pos, Blocks.AIR.getDefaultState());
@ -150,18 +152,19 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
@Override @Override
public boolean breakPart(MultipartView view) { public boolean breakPart(MultipartView view) {
Optional<Entry> entry = parts.stream().filter(e -> e.state == view.getState() && e.entity == view.getEntity()).findFirst(); if (view.getContainer() != this || !(view instanceof Entry)) {
if (!entry.isPresent()) {
return false; return false;
} }
Entry e = (Entry)view;
if (world instanceof ServerWorld) { 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)); drops.forEach(stack -> Block.dropStack(world, pos, stack));
// TODO: don't drop if player is creative // TODO: don't drop if player is creative
} }
remove(view); remove(e);
updateWorld(); updateWorld();

View File

@ -13,6 +13,7 @@ import net.minecraft.world.loot.LootSupplier;
import net.minecraft.world.loot.LootTables; import net.minecraft.world.loot.LootTables;
import net.minecraft.world.loot.context.LootContext; import net.minecraft.world.loot.context.LootContext;
import net.shadowfacts.simplemultipart.SimpleMultipart; import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.container.MultipartContainer;
import net.shadowfacts.simplemultipart.util.MultipartPlacementContext; import net.shadowfacts.simplemultipart.util.MultipartPlacementContext;
import java.util.List; import java.util.List;
@ -143,4 +144,21 @@ public abstract class Multipart {
return false; 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) {
}
} }

View File

@ -57,4 +57,20 @@ public class MultipartState extends AbstractPropertyContainer<Multipart, Multipa
return owner.activate(view, side, player, hand); 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);
}
} }