Extract MultipartContainer into its own interface

This commit is contained in:
Shadowfacts 2018-12-25 10:25:51 -05:00
parent d167b53931
commit b334e6987e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 38 additions and 8 deletions

View File

@ -0,0 +1,22 @@
package net.shadowfacts.simplemultipart.api;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
import java.util.Set;
/**
* @author shadowfacts
*/
public interface MultipartContainer {
Set<MultipartView> getParts();
boolean canInsert(MultipartState state);
void insert(MultipartState state);
void remove(MultipartState state);
boolean breakPart(MultipartState state);
}

View File

@ -1,6 +1,5 @@
package net.shadowfacts.simplemultipart.api; package net.shadowfacts.simplemultipart.api;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.multipart.Multipart; import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState; import net.shadowfacts.simplemultipart.multipart.MultipartState;
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity; import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity;
@ -11,7 +10,7 @@ import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity;
// TODO: better name for this // TODO: better name for this
public interface MultipartView { public interface MultipartView {
MultipartContainerBlockEntity getContainer(); MultipartContainer getContainer();
MultipartState getState(); MultipartState getState();

View File

@ -17,6 +17,7 @@ import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.loot.context.LootContext; import net.minecraft.world.loot.context.LootContext;
import net.minecraft.world.loot.context.Parameters; import net.minecraft.world.loot.context.Parameters;
import net.shadowfacts.simplemultipart.SimpleMultipart; import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
import net.shadowfacts.simplemultipart.multipart.MultipartState; import net.shadowfacts.simplemultipart.multipart.MultipartState;
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity; import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity;
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntityProvider; import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntityProvider;
@ -29,7 +30,7 @@ import java.util.*;
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
public class MultipartContainerBlockEntity extends BlockEntity implements ClientSerializable { public class MultipartContainerBlockEntity extends BlockEntity implements MultipartContainer, ClientSerializable {
private Set<Entry> parts = new HashSet<>(); private Set<Entry> parts = new HashSet<>();
@ -37,10 +38,12 @@ public class MultipartContainerBlockEntity extends BlockEntity implements Client
super(SimpleMultipart.containerBlockEntity); super(SimpleMultipart.containerBlockEntity);
} }
@Override
public Set<MultipartView> getParts() { public Set<MultipartView> getParts() {
return ImmutableSet.copyOf(parts); return ImmutableSet.copyOf(parts);
} }
@Override
public boolean canInsert(MultipartState partState) { public boolean canInsert(MultipartState partState) {
VoxelShape newShape = partState.getBoundingShape(null); VoxelShape newShape = partState.getBoundingShape(null);
for (Entry e : parts) { for (Entry e : parts) {
@ -53,7 +56,12 @@ public class MultipartContainerBlockEntity extends BlockEntity implements Client
return true; return true;
} }
@Override
public void insert(MultipartState partState) { public void insert(MultipartState partState) {
if (!canInsert(partState)) {
return;
}
MultipartEntity entity = null; MultipartEntity entity = null;
if (partState.getMultipart() instanceof MultipartEntityProvider) { if (partState.getMultipart() instanceof MultipartEntityProvider) {
entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this); entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this);
@ -63,6 +71,7 @@ public class MultipartContainerBlockEntity extends BlockEntity implements Client
world.scheduleBlockRender(pos); world.scheduleBlockRender(pos);
} }
@Override
public void remove(MultipartState partState) { public void remove(MultipartState partState) {
parts.removeIf(e -> e.state == partState); parts.removeIf(e -> e.state == partState);
@ -71,6 +80,7 @@ public class MultipartContainerBlockEntity extends BlockEntity implements Client
} }
} }
@Override
public boolean breakPart(MultipartState partState) { public boolean breakPart(MultipartState partState) {
Optional<Entry> entry = parts.stream().filter(e -> e.state == partState).findFirst(); Optional<Entry> entry = parts.stream().filter(e -> e.state == partState).findFirst();
if (!entry.isPresent()) { if (!entry.isPresent()) {

View File

@ -13,14 +13,13 @@ import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity;
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntityProvider; import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntityProvider;
import net.shadowfacts.simplemultipart.api.MultipartView; import net.shadowfacts.simplemultipart.api.MultipartView;
import java.util.function.Function;
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
public class EntityTestPart extends Multipart implements MultipartEntityProvider<EntityTestPart.Entity> { public class EntityTestPart extends Multipart implements MultipartEntityProvider {
@Override @Override
@Deprecated
public VoxelShape getBoundingShape(MultipartState state, MultipartView view) { public VoxelShape getBoundingShape(MultipartState state, MultipartView view) {
return VoxelShapes.cube(6/16f, 6/16f, 6/16f, 10/16f, 10/16f, 10/16f); return VoxelShapes.cube(6/16f, 6/16f, 6/16f, 10/16f, 10/16f, 10/16f);
} }
@ -34,8 +33,8 @@ public class EntityTestPart extends Multipart implements MultipartEntityProvider
} }
@Override @Override
public Function<MultipartContainerBlockEntity, Entity> getMultipartEntityFactory() { public MultipartEntity createMultipartEntity(MultipartState state, MultipartContainerBlockEntity container) {
return Entity::new; return new Entity(container);
} }
public static class Entity extends MultipartEntity { public static class Entity extends MultipartEntity {