2018-12-20 00:39:10 +00:00
|
|
|
package net.shadowfacts.simplemultipart.multipart;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
2018-12-20 01:44:53 +00:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2018-12-20 00:39:10 +00:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.state.StateFactory;
|
2018-12-20 01:44:53 +00:00
|
|
|
import net.minecraft.util.Hand;
|
2018-12-20 00:39:10 +00:00
|
|
|
import net.minecraft.util.Identifier;
|
2018-12-28 18:10:59 +00:00
|
|
|
import net.minecraft.util.math.Direction;
|
2018-12-20 00:39:10 +00:00
|
|
|
import net.minecraft.util.shape.VoxelShape;
|
|
|
|
import net.minecraft.world.loot.LootSupplier;
|
|
|
|
import net.minecraft.world.loot.LootTables;
|
|
|
|
import net.minecraft.world.loot.context.LootContext;
|
|
|
|
import net.shadowfacts.simplemultipart.SimpleMultipart;
|
2018-12-24 22:16:38 +00:00
|
|
|
import net.shadowfacts.simplemultipart.util.MultipartPlacementContext;
|
2018-12-20 00:39:10 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
2018-12-28 18:10:59 +00:00
|
|
|
* The base class for a multipart object.
|
|
|
|
*
|
|
|
|
* Analogous to {@link net.minecraft.block.Block}.
|
|
|
|
*
|
2018-12-20 00:39:10 +00:00
|
|
|
* @author shadowfacts
|
2018-12-28 18:10:59 +00:00
|
|
|
* @since 0.1.0
|
2018-12-20 00:39:10 +00:00
|
|
|
*/
|
|
|
|
public abstract class Multipart {
|
|
|
|
|
|
|
|
private StateFactory<Multipart, MultipartState> stateFactory;
|
|
|
|
private MultipartState defaultState;
|
|
|
|
|
|
|
|
private Identifier dropTableId;
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
// TODO: Settings object?
|
2018-12-20 00:39:10 +00:00
|
|
|
public Multipart() {
|
|
|
|
StateFactory.Builder<Multipart, MultipartState> builder = new StateFactory.Builder<>(this);
|
|
|
|
appendProperties(builder);
|
|
|
|
stateFactory = builder.build(MultipartState::new);
|
|
|
|
defaultState = stateFactory.getDefaultState();
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* Used to add additional properties to the state factory.
|
|
|
|
* @param builder The state factory builder.
|
|
|
|
*/
|
2018-12-20 00:39:10 +00:00
|
|
|
protected void appendProperties(StateFactory.Builder<Multipart, MultipartState> builder) {}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return The default state for this multipart.
|
|
|
|
*/
|
2018-12-20 00:39:10 +00:00
|
|
|
public MultipartState getDefaultState() {
|
|
|
|
return defaultState;
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* Sets a new default state for this multipart.
|
|
|
|
* @param defaultState The new default state.
|
|
|
|
*/
|
2018-12-20 00:39:10 +00:00
|
|
|
public void setDefaultState(MultipartState defaultState) {
|
|
|
|
this.defaultState = defaultState;
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return The state factory for this multipart.
|
|
|
|
*/
|
2018-12-20 00:39:10 +00:00
|
|
|
public StateFactory<Multipart, MultipartState> getStateFactory() {
|
|
|
|
return stateFactory;
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
|
|
|
* Used to determine the state that should be used when this multipart is placed in the world from the given context.
|
|
|
|
*
|
|
|
|
* For example: determine which half a slab should be or which side a facade should be on.
|
|
|
|
*
|
|
|
|
* @param context The context in which this part is being placed.
|
|
|
|
* @return The state to place.
|
|
|
|
*/
|
2018-12-24 22:16:38 +00:00
|
|
|
public MultipartState getPlacementState(MultipartPlacementContext context) {
|
2018-12-20 00:39:10 +00:00
|
|
|
return getDefaultState();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-28 18:10:59 +00:00
|
|
|
* Retrieves the bounding shape this multipart should have.
|
|
|
|
*
|
2018-12-20 00:39:10 +00:00
|
|
|
* Can be overridden, should only be called via {@link MultipartState#getBoundingShape}
|
2018-12-28 18:10:59 +00:00
|
|
|
*
|
|
|
|
* @param state The specific state for which to return the bounding shape.
|
|
|
|
* @param view The current view of the multipart.
|
|
|
|
* Will be {@code null} when the default bounding shape is determined prior to the part being inserted
|
|
|
|
* into the container.
|
|
|
|
* @return The bounding shape of this multipart in the given state.
|
2018-12-20 00:39:10 +00:00
|
|
|
*/
|
|
|
|
@Deprecated
|
2018-12-28 18:10:59 +00:00
|
|
|
public abstract VoxelShape getBoundingShape(MultipartState state, /*@Nullable*/ MultipartView view);
|
2018-12-20 00:39:10 +00:00
|
|
|
|
2018-12-28 18:10:59 +00:00
|
|
|
/**
|
2018-12-28 18:16:58 +00:00
|
|
|
* @return The loot table ID used for to determine the drops by the default {@link Multipart#getDroppedStacks(MultipartView, LootContext.Builder)} implementation.
|
2018-12-28 18:10:59 +00:00
|
|
|
*/
|
2018-12-20 00:39:10 +00:00
|
|
|
public Identifier getDropTableId() {
|
|
|
|
if (dropTableId == null) {
|
|
|
|
Identifier id = SimpleMultipart.MULTIPART.getId(this);
|
|
|
|
dropTableId = new Identifier(id.getNamespace(), "multiparts/" + id.getPath());
|
|
|
|
}
|
|
|
|
return dropTableId;
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:44:53 +00:00
|
|
|
/**
|
2018-12-28 18:10:59 +00:00
|
|
|
* Retrieves a list of stacks that should be dropped in the world when this part is broken via {@link net.shadowfacts.simplemultipart.container.MultipartContainer#breakPart(MultipartView)}.
|
|
|
|
*
|
2018-12-20 01:44:53 +00:00
|
|
|
* Can be overridden, should only be called via {@link MultipartState#getDroppedStacks)}
|
2018-12-28 18:10:59 +00:00
|
|
|
*
|
|
|
|
* @param view The view of this part.
|
|
|
|
* @param builder The {@link LootContext} builder, used by the default loot table-based implementation.
|
|
|
|
* @return The list of stacks to drop.
|
2018-12-20 01:44:53 +00:00
|
|
|
*/
|
2018-12-20 00:39:10 +00:00
|
|
|
@Deprecated
|
2018-12-28 18:16:58 +00:00
|
|
|
public List<ItemStack> getDroppedStacks(MultipartView view, LootContext.Builder builder) {
|
2018-12-20 00:39:10 +00:00
|
|
|
Identifier dropTableId = getDropTableId();
|
|
|
|
if (dropTableId == LootTables.EMPTY) {
|
|
|
|
return ImmutableList.of();
|
|
|
|
} else {
|
2018-12-28 18:16:58 +00:00
|
|
|
LootContext context = builder.put(SimpleMultipart.MULTIPART_STATE_PARAMETER, view.getState()).build(SimpleMultipart.MULTIPART_LOOT_CONTEXT);
|
2018-12-20 00:39:10 +00:00
|
|
|
ServerWorld world = context.getWorld();
|
|
|
|
LootSupplier supplier = world.getServer().getLootManager().getSupplier(dropTableId);
|
|
|
|
return supplier.getDrops(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:44:53 +00:00
|
|
|
/**
|
2018-12-28 18:10:59 +00:00
|
|
|
* Called when this part is activated (i.e. right-clicked) in the world.
|
|
|
|
*
|
2018-12-20 01:44:53 +00:00
|
|
|
* Can be overridden, should only be called via {@link MultipartState#activate}
|
2018-12-28 18:10:59 +00:00
|
|
|
*
|
|
|
|
* @param view The view of this part.
|
2018-12-29 23:45:36 +00:00
|
|
|
* @param side The side of the part that was hit.
|
2018-12-28 18:10:59 +00:00
|
|
|
* @param player The player that activated this part.
|
|
|
|
* @param hand The hand with which they performed the action.
|
|
|
|
* @return If the activation was successful. {@code true} will trigger the hand-swinging animation.
|
2018-12-20 01:44:53 +00:00
|
|
|
*/
|
|
|
|
@Deprecated
|
2018-12-29 23:45:36 +00:00
|
|
|
public boolean activate(MultipartView view, Direction side, PlayerEntity player, Hand hand) {
|
2018-12-20 01:44:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:39:10 +00:00
|
|
|
}
|