This commit is contained in:
Shadowfacts 2018-12-25 10:30:46 -05:00
parent b334e6987e
commit 17a5a8d6ee
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
13 changed files with 66 additions and 60 deletions

View File

@ -9,9 +9,9 @@ import net.minecraft.world.loot.context.LootContextType;
import net.minecraft.world.loot.context.LootContextTypes;
import net.minecraft.world.loot.context.Parameter;
import net.minecraft.world.loot.context.Parameters;
import net.shadowfacts.simplemultipart.container.MultipartContainerEventHandler;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlock;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.container.ContainerBlockEntity;
import net.shadowfacts.simplemultipart.container.ContainerEventHandler;
import net.shadowfacts.simplemultipart.container.ContainerBlock;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
@ -30,14 +30,14 @@ public class SimpleMultipart implements ModInitializer {
public static final Parameter<MultipartState> MULTIPART_STATE_PARAMETER = new Parameter<>(new Identifier(MODID, "multipart_state"));
public static final LootContextType MULTIPART_LOOT_CONTEXT = createMultipartLootContextType();
public static final MultipartContainerBlock containerBlock = new MultipartContainerBlock();
public static final BlockEntityType<MultipartContainerBlockEntity> containerBlockEntity = createBlockEntityType();
public static final ContainerBlock containerBlock = new ContainerBlock();
public static final BlockEntityType<ContainerBlockEntity> containerBlockEntity = createBlockEntityType();
@Override
public void onInitialize() {
Registry.register(Registry.BLOCK, new Identifier(MODID, "container"), containerBlock);
MultipartContainerEventHandler.register();
ContainerEventHandler.register();
}
private static Registry<Multipart> createMultipartRegistry() {
@ -46,8 +46,8 @@ public class SimpleMultipart implements ModInitializer {
return registry;
}
private static BlockEntityType<MultipartContainerBlockEntity> createBlockEntityType() {
BlockEntityType.Builder<MultipartContainerBlockEntity> builder = BlockEntityType.Builder.create(MultipartContainerBlockEntity::new);
private static BlockEntityType<ContainerBlockEntity> createBlockEntityType() {
BlockEntityType.Builder<ContainerBlockEntity> builder = BlockEntityType.Builder.create(ContainerBlockEntity::new);
return Registry.register(Registry.BLOCK_ENTITY, new Identifier(MODID, "container"), builder.method_11034(null));
}

View File

@ -19,4 +19,6 @@ public interface MultipartContainer {
boolean breakPart(MultipartState state);
void schedulePartSave();
}

View File

@ -12,7 +12,7 @@ import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockState;
import net.shadowfacts.simplemultipart.container.ContainerBlockState;
import java.util.List;
import java.util.Random;
@ -25,10 +25,10 @@ public class MultipartContainerBakedModel implements BakedModel {
@Override
public List<BakedQuad> getQuads(BlockState state, Direction side, Random random) {
if (!(state instanceof MultipartContainerBlockState)) {
if (!(state instanceof ContainerBlockState)) {
return null;
}
MultipartContainerBlockState containerState = (MultipartContainerBlockState)state;
ContainerBlockState containerState = (ContainerBlockState)state;
// TODO: would manually building the list be more efficient?
return containerState.getParts().stream()
.flatMap(view -> {

View File

@ -11,6 +11,7 @@ import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.ExtendedBlockView;
import net.minecraft.world.World;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
import net.shadowfacts.simplemultipart.client.util.RenderStateProvider;
import net.shadowfacts.simplemultipart.util.MultipartHelper;
import net.shadowfacts.simplemultipart.util.MultipartHitResult;
@ -21,15 +22,15 @@ import java.util.Set;
/**
* @author shadowfacts
*/
public class MultipartContainerBlock extends Block implements BlockEntityProvider, RenderStateProvider {
public class ContainerBlock extends Block implements BlockEntityProvider, RenderStateProvider {
public MultipartContainerBlock() {
public ContainerBlock() {
super(FabricBlockSettings.of(Material.STONE).build());
}
@Override
public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, Direction side, float hitX, float hitY, float hitZ) {
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
if (container == null) {
return false;
}
@ -44,18 +45,19 @@ public class MultipartContainerBlock extends Block implements BlockEntityProvide
@Override
public BlockState getStateForRendering(BlockState state, BlockPos pos, ExtendedBlockView world) {
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
if (container == null) {
return state;
}
Set<MultipartView> parts = container.getParts();
return new MultipartContainerBlockState(state, parts);
return new ContainerBlockState(state, parts);
}
@Override
@Deprecated
public VoxelShape getBoundingShape(BlockState state, BlockView world, BlockPos pos) {
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
if (container == null) {
return VoxelShapes.empty();
}
@ -69,7 +71,7 @@ public class MultipartContainerBlock extends Block implements BlockEntityProvide
}
@Override
public MultipartContainerBlockEntity createBlockEntity(BlockView world) {
return new MultipartContainerBlockEntity();
public ContainerBlockEntity createBlockEntity(BlockView world) {
return new ContainerBlockEntity();
}
}

View File

@ -30,11 +30,11 @@ import java.util.*;
/**
* @author shadowfacts
*/
public class MultipartContainerBlockEntity extends BlockEntity implements MultipartContainer, ClientSerializable {
public class ContainerBlockEntity extends BlockEntity implements MultipartContainer, ClientSerializable {
private Set<Entry> parts = new HashSet<>();
public MultipartContainerBlockEntity() {
public ContainerBlockEntity() {
super(SimpleMultipart.containerBlockEntity);
}
@ -103,7 +103,12 @@ public class MultipartContainerBlockEntity extends BlockEntity implements Multip
return true;
}
private List<ItemStack> getDroppedStacks(MultipartContainerBlockEntity.Entry e, ServerWorld world, BlockPos pos) {
@Override
public void schedulePartSave() {
markDirty(); // see yarn #360
}
private List<ItemStack> getDroppedStacks(ContainerBlockEntity.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);
@ -174,8 +179,8 @@ public class MultipartContainerBlockEntity extends BlockEntity implements Multip
}
@Override
public MultipartContainerBlockEntity getContainer() {
return MultipartContainerBlockEntity.this; // TODO: is this bad?
public ContainerBlockEntity getContainer() {
return ContainerBlockEntity.this; // TODO: is this bad?
}
@Override

View File

@ -8,11 +8,11 @@ import java.util.Set;
/**
* @author shadowfacts
*/
public class MultipartContainerBlockState extends BlockState {
public class ContainerBlockState extends BlockState {
private Set<MultipartView> parts;
public MultipartContainerBlockState(BlockState delegate, Set<MultipartView> parts) {
public ContainerBlockState(BlockState delegate, Set<MultipartView> parts) {
super(delegate.getBlock(), delegate.getEntries());
this.parts = parts;
}

View File

@ -7,6 +7,7 @@ import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
import net.shadowfacts.simplemultipart.util.MultipartHitResult;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.util.MultipartHelper;
@ -14,10 +15,10 @@ import net.shadowfacts.simplemultipart.util.MultipartHelper;
/**
* @author shadowfacts
*/
public class MultipartContainerEventHandler {
public class ContainerEventHandler {
public static void register() {
PlayerInteractionEvent.ATTACK_BLOCK.register(MultipartContainerEventHandler::handleBlockAttack);
PlayerInteractionEvent.ATTACK_BLOCK.register(ContainerEventHandler::handleBlockAttack);
}
private static ActionResult handleBlockAttack(PlayerEntity player, World world, Hand hand, BlockPos pos, Direction direction) {
@ -25,7 +26,7 @@ public class MultipartContainerEventHandler {
return ActionResult.PASS;
}
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
if (container == null) {
return ActionResult.FAILURE;
}

View File

@ -4,10 +4,9 @@ import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
import net.shadowfacts.simplemultipart.container.ContainerBlockEntity;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
import net.shadowfacts.simplemultipart.util.MultipartPlacementContext;
@ -31,14 +30,14 @@ public class ItemMultipart extends Item {
protected ActionResult tryPlace(ItemUsageContext context) {
// If a multipart inside an existing container was clicked, try inserting into that
MultipartContainerBlockEntity hitContainer = getContainer(context);
MultipartContainer hitContainer = getContainer(context);
if (hitContainer != null && tryPlace(new MultipartPlacementContext(hitContainer, context))) {
return ActionResult.SUCCESS;
}
// Otherwise, get or create a new container and try inserting into that
ItemUsageContext offsetContext = new ItemUsageContext(context.getPlayer(), context.getItemStack(), context.getPos().offset(context.getFacing()), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
MultipartContainerBlockEntity offsetContainer = getOrCreateContainer(offsetContext);
MultipartContainer offsetContainer = getOrCreateContainer(offsetContext);
if (offsetContainer != null && tryPlace(new MultipartPlacementContext(offsetContainer, offsetContext))) {
return ActionResult.SUCCESS;
}
@ -46,22 +45,22 @@ public class ItemMultipart extends Item {
return ActionResult.FAILURE;
}
protected MultipartContainerBlockEntity getContainer(ItemUsageContext context) {
protected MultipartContainer getContainer(ItemUsageContext context) {
BlockState state = context.getWorld().getBlockState(context.getPos());
if (state.getBlock() == SimpleMultipart.containerBlock) {
return (MultipartContainerBlockEntity)context.getWorld().getBlockEntity(context.getPos());
return (MultipartContainer)context.getWorld().getBlockEntity(context.getPos());
} else {
return null;
}
}
protected MultipartContainerBlockEntity getOrCreateContainer(ItemUsageContext context) {
MultipartContainerBlockEntity container = getContainer(context);
protected MultipartContainer getOrCreateContainer(ItemUsageContext context) {
MultipartContainer container = getContainer(context);
if (container == null) {
BlockState existing = context.getWorld().getBlockState(context.getPos());
if (existing.isAir()) { // TODO: should check is replaceable (might not be mapped?)
context.getWorld().setBlockState(context.getPos(), SimpleMultipart.containerBlock.getDefaultState());
container = (MultipartContainerBlockEntity)context.getWorld().getBlockEntity(context.getPos());
container = (MultipartContainer)context.getWorld().getBlockEntity(context.getPos());
}
}
return container;

View File

@ -1,21 +1,21 @@
package net.shadowfacts.simplemultipart.multipart.entity;
import net.minecraft.nbt.CompoundTag;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
/**
* @author shadowfacts
*/
public abstract class MultipartEntity {
public MultipartContainerBlockEntity container;
public MultipartContainer container;
public MultipartEntity(MultipartContainerBlockEntity container) {
public MultipartEntity(MultipartContainer container) {
this.container = container;
}
protected void scheduleSave() {
container.markDirty(); // see yarn #360
container.schedulePartSave();
}
public CompoundTag toTag(CompoundTag tag) {

View File

@ -1,6 +1,6 @@
package net.shadowfacts.simplemultipart.multipart.entity;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
/**
@ -9,6 +9,6 @@ import net.shadowfacts.simplemultipart.multipart.MultipartState;
public interface MultipartEntityProvider {
/*@Nullable*/
MultipartEntity createMultipartEntity(MultipartState state, MultipartContainerBlockEntity container);
MultipartEntity createMultipartEntity(MultipartState state, MultipartContainer container);
}

View File

@ -2,9 +2,7 @@ package net.shadowfacts.simplemultipart.util;
import com.google.common.collect.ImmutableMap;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.PropertyContainer;
import net.minecraft.state.StateFactory;
import net.minecraft.state.property.Property;
@ -15,10 +13,9 @@ import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.World;
import net.minecraft.world.loot.context.LootContext;
import net.minecraft.world.loot.context.Parameters;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
import net.shadowfacts.simplemultipart.container.ContainerBlockEntity;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
@ -29,7 +26,7 @@ import java.util.*;
*/
public class MultipartHelper {
public static MultipartHitResult rayTrace(MultipartContainerBlockEntity container, World world, BlockPos pos, PlayerEntity player) {
public static MultipartHitResult rayTrace(MultipartContainer container, World world, BlockPos pos, PlayerEntity player) {
// copied from BoatItem::use
float var6 = MathHelper.lerp(1.0F, player.prevPitch, player.pitch);
float var7 = MathHelper.lerp(1.0F, player.prevYaw, player.yaw);
@ -49,7 +46,7 @@ public class MultipartHelper {
return rayTrace(container, world, pos, start, end);
}
public static MultipartHitResult rayTrace(MultipartContainerBlockEntity container, World world, BlockPos pos, Vec3d start, Vec3d end) {
public static MultipartHitResult rayTrace(MultipartContainer container, World world, BlockPos pos, Vec3d start, Vec3d end) {
return container.getParts().stream()
.map(view -> {
VoxelShape shape = view.getState().getBoundingShape(view);

View File

@ -5,25 +5,25 @@ import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.api.MultipartContainer;
/**
* @author shadowfacts
*/
public class MultipartPlacementContext extends ItemUsageContext {
private final MultipartContainerBlockEntity container;
private final MultipartContainer container;
public MultipartPlacementContext(MultipartContainerBlockEntity container, PlayerEntity player, ItemStack stack, BlockPos pos, Direction side, float hitX, float hitY, float hitZ) {
public MultipartPlacementContext(MultipartContainer container, PlayerEntity player, ItemStack stack, BlockPos pos, Direction side, float hitX, float hitY, float hitZ) {
super(player, stack, pos, side, hitX, hitY, hitZ);
this.container = container;
}
public MultipartPlacementContext(MultipartContainerBlockEntity container, ItemUsageContext context) {
public MultipartPlacementContext(MultipartContainer container, ItemUsageContext context) {
this(container, context.getPlayer(), context.getItemStack(), context.getPos(), context.getFacing(), context.getHitX(), context.getHitY(), context.getHitZ());
}
public MultipartContainerBlockEntity getContainer() {
public MultipartContainer getContainer() {
return container;
}

View File

@ -6,7 +6,7 @@ import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.shadowfacts.simplemultipart.container.MultipartContainerBlockEntity;
import net.shadowfacts.simplemultipart.container.ContainerBlockEntity;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
import net.shadowfacts.simplemultipart.multipart.entity.MultipartEntity;
@ -33,12 +33,12 @@ public class EntityTestPart extends Multipart implements MultipartEntityProvider
}
@Override
public MultipartEntity createMultipartEntity(MultipartState state, MultipartContainerBlockEntity container) {
public MultipartEntity createMultipartEntity(MultipartState state, ContainerBlockEntity container) {
return new Entity(container);
}
public static class Entity extends MultipartEntity {
public Entity(MultipartContainerBlockEntity container) {
public Entity(ContainerBlockEntity container) {
super(container);
}