2018-12-25 17:08:48 +00:00
|
|
|
package net.shadowfacts.simplemultipart.container;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
import net.fabricmc.fabric.api.util.NbtType;
|
|
|
|
import net.fabricmc.fabric.block.entity.ClientSerializable;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
|
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.block.entity.BlockEntityType;
|
2019-01-06 15:28:36 +00:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2018-12-25 17:08:48 +00:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.nbt.ListTag;
|
|
|
|
import net.minecraft.nbt.Tag;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.util.Tickable;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-12-29 23:33:16 +00:00
|
|
|
import net.minecraft.util.math.Direction;
|
2018-12-25 17:08:48 +00:00
|
|
|
import net.minecraft.util.shape.VoxelShape;
|
2019-01-01 23:40:38 +00:00
|
|
|
import net.minecraft.world.World;
|
2018-12-25 17:08:48 +00:00
|
|
|
import net.minecraft.world.loot.context.LootContext;
|
|
|
|
import net.minecraft.world.loot.context.Parameters;
|
|
|
|
import net.shadowfacts.simplemultipart.SimpleMultipart;
|
2019-01-05 15:04:24 +00:00
|
|
|
import net.shadowfacts.simplemultipart.multipart.Multipart;
|
2018-12-25 17:08:48 +00:00
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartState;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntityProvider;
|
|
|
|
import net.shadowfacts.simplemultipart.util.MultipartHelper;
|
2018-12-28 02:55:51 +00:00
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartView;
|
2018-12-25 17:08:48 +00:00
|
|
|
import net.shadowfacts.simplemultipart.util.ShapeUtils;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
public abstract class AbstractContainerBlockEntity extends BlockEntity implements MultipartContainer, ClientSerializable {
|
|
|
|
|
|
|
|
protected Set<Entry> parts = new HashSet<>();
|
2018-12-29 23:33:16 +00:00
|
|
|
protected Map<Direction, Entry> sidePartCache = new WeakHashMap<>();
|
2018-12-25 17:08:48 +00:00
|
|
|
|
|
|
|
public AbstractContainerBlockEntity(BlockEntityType<?> type) {
|
|
|
|
super(type);
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:40:38 +00:00
|
|
|
@Override
|
|
|
|
public World getContainerWorld() {
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockPos getContainerPos() {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2018-12-25 17:08:48 +00:00
|
|
|
@Override
|
|
|
|
public Set<MultipartView> getParts() {
|
|
|
|
return ImmutableSet.copyOf(parts);
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:13:51 +00:00
|
|
|
@Override
|
|
|
|
public boolean hasParts() {
|
|
|
|
return !parts.isEmpty();
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:04:24 +00:00
|
|
|
@Override
|
|
|
|
public Set<MultipartView> getParts(Multipart type) {
|
|
|
|
return parts.stream()
|
|
|
|
.filter(e -> e.getState().getMultipart() == type)
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
}
|
|
|
|
|
2018-12-29 23:33:16 +00:00
|
|
|
@Override
|
|
|
|
public MultipartView getPart(Direction side) {
|
|
|
|
Entry existing = sidePartCache.get(side);
|
|
|
|
if (existing != null) {
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<Entry> e = parts.stream()
|
|
|
|
.min((a, b) -> {
|
|
|
|
VoxelShape aShape = a.getState().getBoundingShape(a);
|
|
|
|
VoxelShape bShape = b.getState().getBoundingShape(b);
|
|
|
|
double aCoord = side.getDirection() == Direction.AxisDirection.POSITIVE ? aShape.getMaximum(side.getAxis()) : aShape.getMinimum(side.getAxis());
|
|
|
|
double bCoord = side.getDirection() == Direction.AxisDirection.POSITIVE ? bShape.getMaximum(side.getAxis()) : bShape.getMinimum(side.getAxis());
|
|
|
|
return Double.compare(bCoord, aCoord);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!e.isPresent()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
sidePartCache.put(side, e.get());
|
|
|
|
|
|
|
|
return e.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void invalidateSidePartCache() {
|
|
|
|
sidePartCache.clear();
|
|
|
|
}
|
|
|
|
|
2018-12-25 17:08:48 +00:00
|
|
|
@Override
|
|
|
|
public boolean canInsert(MultipartState partState) {
|
|
|
|
VoxelShape newShape = partState.getBoundingShape(null);
|
|
|
|
for (Entry e : parts) {
|
|
|
|
VoxelShape existingShape = e.state.getBoundingShape(e);
|
2019-01-05 19:04:46 +00:00
|
|
|
if (ShapeUtils.intersect(newShape, existingShape) && !(partState.canIntersectWith(e.state) && e.state.canIntersectWith(partState))) {
|
2018-12-25 17:08:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void insert(MultipartState partState) {
|
|
|
|
if (!canInsert(partState)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:25:31 +00:00
|
|
|
Entry e = new Entry(this, partState, null);
|
2018-12-25 17:08:48 +00:00
|
|
|
if (partState.getMultipart() instanceof MultipartEntityProvider) {
|
2018-12-28 18:25:31 +00:00
|
|
|
e.entity = ((MultipartEntityProvider)partState.getMultipart()).createMultipartEntity(partState, this);
|
|
|
|
e.entity.view = e;
|
2018-12-25 17:08:48 +00:00
|
|
|
}
|
2018-12-28 18:25:31 +00:00
|
|
|
parts.add(e);
|
2018-12-25 17:08:48 +00:00
|
|
|
|
2019-01-05 15:14:52 +00:00
|
|
|
partState.onPartAdded(e);
|
|
|
|
|
2018-12-29 23:33:16 +00:00
|
|
|
invalidateSidePartCache();
|
2018-12-25 17:08:48 +00:00
|
|
|
updateWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-25 18:29:54 +00:00
|
|
|
public void remove(MultipartView view) {
|
2019-01-05 15:14:52 +00:00
|
|
|
if (view.getContainer() != this || !(view instanceof Entry)) {
|
2018-12-25 18:29:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:14:52 +00:00
|
|
|
parts.remove(view);
|
|
|
|
view.getState().onPartRemoved(view);
|
2018-12-25 17:08:48 +00:00
|
|
|
|
|
|
|
if (parts.isEmpty()) {
|
|
|
|
world.setBlockState(pos, Blocks.AIR.getDefaultState());
|
|
|
|
} else {
|
2018-12-29 23:33:16 +00:00
|
|
|
invalidateSidePartCache();
|
2018-12-25 17:08:48 +00:00
|
|
|
updateWorld();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-06 15:28:36 +00:00
|
|
|
public boolean breakPart(MultipartView view, PlayerEntity player) {
|
2019-01-05 15:14:52 +00:00
|
|
|
if (view.getContainer() != this || !(view instanceof Entry)) {
|
2018-12-25 17:08:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:14:52 +00:00
|
|
|
Entry e = (Entry)view;
|
|
|
|
|
2019-01-06 15:28:36 +00:00
|
|
|
if (world instanceof ServerWorld && !player.isCreative()) {
|
2019-01-05 15:14:52 +00:00
|
|
|
List<ItemStack> drops = getDroppedStacks(e, (ServerWorld)world, pos);
|
2018-12-25 17:08:48 +00:00
|
|
|
drops.forEach(stack -> Block.dropStack(world, pos, stack));
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:14:52 +00:00
|
|
|
remove(e);
|
2018-12-25 17:08:48 +00:00
|
|
|
|
|
|
|
updateWorld();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void schedulePartSave() {
|
|
|
|
markDirty(); // see yarn #360
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateWorld() {
|
|
|
|
boolean hasTickableParts = parts.stream().anyMatch(e -> e.getEntity() != null && e.getEntity() instanceof Tickable);
|
|
|
|
boolean currentlyTickable = this instanceof Tickable;
|
|
|
|
if (hasTickableParts != currentlyTickable) {
|
2018-12-28 02:44:50 +00:00
|
|
|
Block newBlock = hasTickableParts ? SimpleMultipart.tickableContainerBlock : SimpleMultipart.containerBlock;
|
|
|
|
world.setBlockState(pos, newBlock.getDefaultState(), 3);
|
|
|
|
AbstractContainerBlockEntity newContainer = (AbstractContainerBlockEntity)world.getBlockEntity(pos);
|
2018-12-25 17:08:48 +00:00
|
|
|
newContainer.parts = parts.stream()
|
|
|
|
.map(e -> new Entry(newContainer, e.state, e.entity))
|
|
|
|
.collect(Collectors.toSet());
|
2018-12-28 18:25:31 +00:00
|
|
|
newContainer.parts.stream().filter(e -> e.entity != null).forEach(e -> e.entity.view = e);
|
2018-12-25 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
world.markDirty(pos, world.getBlockEntity(pos));
|
|
|
|
world.scheduleBlockRender(pos);
|
|
|
|
BlockState blockState = world.getBlockState(pos);
|
|
|
|
world.updateListeners(pos, blockState, blockState, 3);
|
2019-01-06 14:47:52 +00:00
|
|
|
blockState.updateNeighborStates(world, pos, 3);
|
2018-12-25 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<ItemStack> getDroppedStacks(Entry e, ServerWorld world, BlockPos pos) {
|
|
|
|
LootContext.Builder builder = new LootContext.Builder(world);
|
|
|
|
builder.setRandom(world.random);
|
|
|
|
builder.put(SimpleMultipart.MULTIPART_STATE_PARAMETER, e.state);
|
|
|
|
builder.put(Parameters.POSITION, pos);
|
|
|
|
return e.state.getDroppedStacks(e, builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ListTag partsToTag() {
|
|
|
|
ListTag list = new ListTag();
|
|
|
|
for (Entry e : parts) {
|
|
|
|
CompoundTag tag = new CompoundTag();
|
|
|
|
tag.put("part", MultipartHelper.serializeMultipartState(e.state));
|
|
|
|
if (e.entity != null) {
|
|
|
|
tag.put("entity", e.entity.toTag(new CompoundTag()));
|
|
|
|
}
|
|
|
|
list.add(tag);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void partsFromTag(ListTag list) {
|
|
|
|
parts.clear();
|
|
|
|
for (Tag tag : list) {
|
|
|
|
CompoundTag compound = (CompoundTag)tag;
|
|
|
|
MultipartState state = MultipartHelper.deserializeMultipartState(compound.getCompound("part"));
|
2018-12-28 18:25:31 +00:00
|
|
|
|
|
|
|
Entry e = new Entry(this, state, null);
|
2018-12-25 17:08:48 +00:00
|
|
|
if (state.getMultipart() instanceof MultipartEntityProvider && compound.containsKey("entity", NbtType.COMPOUND)) {
|
2018-12-28 18:25:31 +00:00
|
|
|
e.entity = ((MultipartEntityProvider)state.getMultipart()).createMultipartEntity(state, this);
|
|
|
|
e.entity.view = e;
|
|
|
|
e.entity.fromTag(compound.getCompound("entity"));
|
2018-12-25 17:08:48 +00:00
|
|
|
}
|
2018-12-28 18:25:31 +00:00
|
|
|
parts.add(e);
|
2018-12-25 17:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompoundTag toTag(CompoundTag tag) {
|
|
|
|
tag.put("parts", partsToTag());
|
|
|
|
return super.toTag(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromTag(CompoundTag tag) {
|
|
|
|
super.fromTag(tag);
|
|
|
|
ListTag list = tag.getList("parts", NbtType.COMPOUND);
|
|
|
|
partsFromTag(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompoundTag toClientTag(CompoundTag tag) {
|
|
|
|
tag.put("parts", partsToTag());
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromClientTag(CompoundTag tag) {
|
|
|
|
ListTag list = tag.getList("parts", NbtType.COMPOUND);
|
|
|
|
partsFromTag(list);
|
|
|
|
updateWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Entry implements MultipartView {
|
2018-12-29 23:33:16 +00:00
|
|
|
public final AbstractContainerBlockEntity container;
|
2018-12-28 02:55:51 +00:00
|
|
|
public MultipartState state;
|
|
|
|
public MultipartEntity entity;
|
2018-12-25 17:08:48 +00:00
|
|
|
|
2018-12-29 23:33:16 +00:00
|
|
|
private Entry(AbstractContainerBlockEntity container, MultipartState state, MultipartEntity entity) {
|
2018-12-25 17:08:48 +00:00
|
|
|
this.container = container;
|
|
|
|
this.state = state;
|
|
|
|
this.entity = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-29 23:33:16 +00:00
|
|
|
public AbstractContainerBlockEntity getContainer() {
|
2018-12-25 17:08:48 +00:00
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MultipartState getState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-12-28 02:55:51 +00:00
|
|
|
@Override
|
|
|
|
public void setState(MultipartState state) {
|
|
|
|
this.state = state;
|
2018-12-29 23:33:16 +00:00
|
|
|
container.invalidateSidePartCache();
|
2018-12-28 02:55:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-25 17:08:48 +00:00
|
|
|
@Override
|
|
|
|
public MultipartEntity getEntity() {
|
|
|
|
return entity;
|
|
|
|
}
|
2018-12-28 02:55:51 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEntity(MultipartEntity entity) {
|
|
|
|
this.entity = entity;
|
2018-12-29 23:33:16 +00:00
|
|
|
container.invalidateSidePartCache();
|
2018-12-28 02:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Entry{" + state + "}";
|
|
|
|
}
|
2018-12-25 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|