Change MultipartEntity container to a view

This commit is contained in:
Shadowfacts 2018-12-28 13:25:31 -05:00
parent 271bb0165e
commit 53b0db1cc4
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 19 additions and 27 deletions

View File

@ -69,11 +69,13 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
return; return;
} }
MultipartEntity entity = null;
Entry e = new Entry(this, partState, null);
if (partState.getMultipart() instanceof MultipartEntityProvider) { if (partState.getMultipart() instanceof MultipartEntityProvider) {
entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this); e.entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this);
e.entity.view = e;
} }
parts.add(new Entry(this, partState, entity)); parts.add(e);
updateWorld(); updateWorld();
} }
@ -128,7 +130,7 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
newContainer.parts = parts.stream() newContainer.parts = parts.stream()
.map(e -> new Entry(newContainer, e.state, e.entity)) .map(e -> new Entry(newContainer, e.state, e.entity))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
newContainer.parts.stream().filter(e -> e.entity != null).forEach(e -> e.entity.container = newContainer); newContainer.parts.stream().filter(e -> e.entity != null).forEach(e -> e.entity.view = e);
} }
world.markDirty(pos, world.getBlockEntity(pos)); world.markDirty(pos, world.getBlockEntity(pos));
@ -163,12 +165,14 @@ public abstract class AbstractContainerBlockEntity extends BlockEntity implement
for (Tag tag : list) { for (Tag tag : list) {
CompoundTag compound = (CompoundTag)tag; CompoundTag compound = (CompoundTag)tag;
MultipartState state = MultipartHelper.deserializeMultipartState(compound.getCompound("part")); MultipartState state = MultipartHelper.deserializeMultipartState(compound.getCompound("part"));
MultipartEntity entity = null;
Entry e = new Entry(this, state, null);
if (state.getMultipart() instanceof MultipartEntityProvider && compound.containsKey("entity", NbtType.COMPOUND)) { if (state.getMultipart() instanceof MultipartEntityProvider && compound.containsKey("entity", NbtType.COMPOUND)) {
entity = ((MultipartEntityProvider)state.getMultipart()).createMultipartEntity(state, this); e.entity = ((MultipartEntityProvider)state.getMultipart()).createMultipartEntity(state, this);
entity.fromTag(compound.getCompound("entity")); e.entity.view = e;
e.entity.fromTag(compound.getCompound("entity"));
} }
parts.add(new Entry(this, state, entity)); parts.add(e);
} }
} }

View File

@ -2,6 +2,7 @@ package net.shadowfacts.simplemultipart.multipart.entity;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.shadowfacts.simplemultipart.container.MultipartContainer; import net.shadowfacts.simplemultipart.container.MultipartContainer;
import net.shadowfacts.simplemultipart.multipart.MultipartView;
/** /**
* An entity associated with multiparts placed in the world. An instance of the part's entity exists for every in-world * An entity associated with multiparts placed in the world. An instance of the part's entity exists for every in-world
@ -17,20 +18,15 @@ import net.shadowfacts.simplemultipart.container.MultipartContainer;
public abstract class MultipartEntity { public abstract class MultipartEntity {
/** /**
* The container holding this multipart entity. * The view of this multipart.
*/ */
// TODO: change this to a view? public MultipartView view;
public MultipartContainer container;
public MultipartEntity(MultipartContainer container) {
this.container = container;
}
/** /**
* Calling this indicates that something about this entity has changed that necessitates saving it to disk. * Calling this indicates that something about this entity has changed that necessitates saving it to disk.
*/ */
protected void scheduleSave() { protected void scheduleSave() {
container.schedulePartSave(); view.getContainer().schedulePartSave();
} }
/** /**

View File

@ -35,16 +35,12 @@ public class EntityTestPart extends Multipart implements MultipartEntityProvider
@Override @Override
public MultipartEntity createMultipartEntity(MultipartState state, MultipartContainer container) { public MultipartEntity createMultipartEntity(MultipartState state, MultipartContainer container) {
return new Entity(container); return new Entity();
} }
public static class Entity extends MultipartEntity { public static class Entity extends MultipartEntity {
public Entity(MultipartContainer container) {
super(container);
}
public BlockPos getPos() { public BlockPos getPos() {
return ((AbstractContainerBlockEntity)container).getPos(); return ((AbstractContainerBlockEntity)view.getContainer()).getPos();
} }
} }

View File

@ -34,16 +34,12 @@ public class TickableEntityTestPart extends Multipart implements MultipartEntity
@Override @Override
public MultipartEntity createMultipartEntity(MultipartState state, MultipartContainer container) { public MultipartEntity createMultipartEntity(MultipartState state, MultipartContainer container) {
return new Entity(container); return new Entity();
} }
public static class Entity extends MultipartEntity implements Tickable { public static class Entity extends MultipartEntity implements Tickable {
public int timer = 0; public int timer = 0;
public Entity(MultipartContainer container) {
super(container);
}
@Override @Override
public void tick() { public void tick() {
timer++; timer++;