use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.
the class ContainerChiselHitech method setSelection.
public void setSelection(@Nullable Slot slot) {
this.selection = slot;
if (slot == null || !slot.getHasStack()) {
currentGroup = null;
selectionDuplicates = ImmutableList.of();
setTarget(null);
} else {
ImmutableList.Builder<Slot> builder = ImmutableList.builder();
for (int i = getInventoryChisel().size + 1; i < inventorySlots.size(); i++) {
Slot s = getSlot(i);
if (slot != s && ItemStack.areItemsEqual(slot.getStack(), s.getStack())) {
builder.add(s);
}
}
selectionDuplicates = builder.build();
ICarvingGroup group = carving.getGroup(slot.getStack().getItem()).orElse(null);
if (currentGroup != null && group != currentGroup) {
setTarget(null);
}
currentGroup = group;
}
ItemStack stack = slot == null ? ItemStack.EMPTY : slot.getStack();
getInventoryChisel().setStackInSpecialSlot(stack);
getInventoryChisel().updateItems();
NBTUtil.setHitechSelection(chisel, Optional.fromNullable(getSelection()).transform(s -> s.slotNumber).or(-1));
}
use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.
the class ChiselController method onPlayerInteract.
@SubscribeEvent
public static void onPlayerInteract(PlayerInteractEvent.LeftClickBlock event) {
PlayerEntity player = event.getPlayer();
ItemStack held = event.getItemStack();
if (held.getItem() instanceof IChiselItem) {
ItemStack target = NBTUtil.getChiselTarget(held);
IChiselItem chisel = (IChiselItem) held.getItem();
IVariationRegistry registry = CarvingUtils.getChiselRegistry();
BlockState state = event.getWorld().getBlockState(event.getPos());
if (!chisel.canChiselBlock(event.getWorld(), player, event.getHand(), event.getPos(), state)) {
return;
}
ICarvingGroup blockGroup = state.getBlock() instanceof ICarvable ? ((ICarvable) state.getBlock()).getVariation().getGroup() : registry.getGroup(state.getBlock()).orElse(null);
if (blockGroup == null) {
return;
}
IChiselMode mode = NBTUtil.getChiselMode(held);
Iterable<? extends BlockPos> candidates = mode.getCandidates(player, event.getPos(), event.getFace());
if (!target.isEmpty()) {
ICarvingGroup sourceGroup = registry.getGroup(target.getItem()).orElse(null);
if (blockGroup == sourceGroup) {
ICarvingVariation variation = registry.getVariation(target.getItem()).orElse(null);
if (variation != null) {
if (variation.getBlock() != null) {
setAll(candidates, player, state, variation);
}
} else {
Chisel.logger.warn("Found itemstack {} in group {}, but it has no variation!", target, sourceGroup.getId());
}
}
} else {
List<Block> variations = new ArrayList<>(blockGroup.getBlockTag().map(ITag::getAllElements).orElse(Collections.emptyList()));
variations = variations.stream().filter(v -> v.getBlock() != null).collect(Collectors.toList());
int index = variations.indexOf(state.getBlock());
index = player.isSneaking() ? index - 1 : index + 1;
index = (index + variations.size()) % variations.size();
ICarvingVariation next = registry.getVariation(variations.get(index)).orElse(null);
setAll(candidates, player, state, next);
}
}
}
use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.
the class PacketChiselButton method chiselAll.
public static void chiselAll(PlayerEntity player, int[] slots) {
if (player.openContainer instanceof ContainerChiselHitech) {
ContainerChiselHitech container = (ContainerChiselHitech) player.openContainer;
ItemStack chisel = container.getChisel();
ItemStack originalChisel = chisel.copy();
ItemStack target = container.getTargetStack();
if (!(chisel.getItem() instanceof IChiselItem)) {
return;
}
@SuppressWarnings("null") @Nonnull IVariationRegistry carving = CarvingUtils.getChiselRegistry();
if (chisel.isEmpty() || target.isEmpty()) {
return;
}
boolean playSound = false;
for (int i : slots) {
ItemStack s = player.inventory.getStackInSlot(i);
if (!s.isEmpty()) {
if (!carving.getGroup(target.getItem()).map(ICarvingGroup::getId).flatMap(id -> carving.getGroup(s.getItem()).map(g -> g.getId().equals(id))).orElse(false)) {
return;
}
container.getInventoryChisel().setStackInSpecialSlot(s);
ItemStack res = SlotChiselSelection.craft(container, player, target.copy(), false);
if (!res.isEmpty()) {
player.inventory.setInventorySlotContents(i, res);
playSound = true;
}
}
if (chisel.isEmpty()) {
return;
}
}
container.getInventoryChisel().setStackInSpecialSlot(container.getSelectionStack());
container.getInventoryChisel().updateItems();
if (playSound) {
SoundUtil.playSound(player, originalChisel, target);
}
}
}
use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.
the class ChiselBlockBuilder method build.
/**
* Builds the block(s), performing the passed action on each.
*
* @param after
* The consumer to call after creating each block. Use this to easily set things like hardness/light/etc. This is called after block registration, but prior to model/variation
* registration.
* @return An array of blocks created. More blocks are automatically created if the unbaked variations will not fit into one block.
*/
@SuppressWarnings("null")
public Map<String, BlockEntry<T>> build(NonNullUnaryOperator<Block.Properties> after) {
if (variations.size() == 0) {
throw new IllegalArgumentException("Must have at least one variation!");
}
Variation[] data = new Variation[variations.size()];
for (int i = 0; i < variations.size(); i++) {
data[i] = variations.get(i).doBuild();
}
Map<String, BlockEntry<T>> ret = new HashMap<>(data.length);
ICarvingGroup group = CarvingUtils.itemGroup(this.group, this.groupName);
for (int i = 0; i < data.length; i++) {
if (Strings.emptyToNull(data[i].getName()) != null) {
final int index = i;
final Variation var = data[index];
final VariationBuilder<T> builder = variations.get(index);
ret.put(var.getName(), registrate.object(blockName + "/" + var.getName()).block(material, p -> provider.createBlock(p, new VariationDataImpl(ret.get(var.getName()), var.getName(), var.getDisplayName(), group))).initialProperties(initialProperties == null ? NonNullSupplier.of(Blocks.STONE.delegate) : initialProperties).addLayer(layer).transform(this::addTags).properties(color == null ? after : after.andThen(p -> {
p.blockColors = $ -> color;
return p;
})).blockstate((ctx, prov) -> builder.model.accept(prov, ctx.getEntry())).setData(ProviderType.LANG, NonNullBiConsumer.noop()).recipe((ctx, prov) -> builder.recipe.accept(prov, ctx.getEntry())).loot(loot).item(provider::createBlockItem).model((ctx, prov) -> prov.withExistingParent("item/" + prov.name(ctx::getEntry), new ResourceLocation(prov.modid(ctx::getEntry), "block/" + prov.name(ctx::getEntry)))).transform(this::addTags).build().register());
}
}
if (this.group != null) {
CarvingUtils.getChiselRegistry().addGroup(group);
if (!otherBlocks.isEmpty() || !otherTags.isEmpty()) {
addExtraTagEntries(ProviderType.BLOCK_TAGS, t -> t, ForgeRegistries.BLOCKS::getValue);
this.<Item>addExtraTagEntries(ProviderType.ITEM_TAGS, t -> parent.getItemTag(t.getName()), ForgeRegistries.ITEMS::getValue);
}
}
return ret;
}
use of team.chisel.api.carving.ICarvingGroup in project Chisel by Chisel-Team.
the class TileAutoChisel method tick.
@Override
public void tick() {
if (getWorld() == null || getWorld().isRemote) {
return;
}
if (energyStorage.getEnergyStored() == 0 && Configurations.autoChiselNeedsPower) {
return;
}
@Nonnull ItemStack target = getTarget();
@Nonnull ItemStack chisel = getChisel();
@Nonnull ItemStack source = sourceSlot < 0 ? ItemStack.EMPTY : getInputInv().getStackInSlot(sourceSlot);
chisel = chisel.isEmpty() ? ItemStack.EMPTY : chisel.copy();
ICarvingVariation v = target.isEmpty() || chisel.isEmpty() ? null : CarvingUtils.getChiselRegistry().getVariation(target.getItem()).orElse(null);
ICarvingGroup g = target.isEmpty() || chisel.isEmpty() ? null : CarvingUtils.getChiselRegistry().getGroup(target.getItem()).orElse(null);
if (chisel.isEmpty() || v == null) {
setSourceSlot(-1);
progress = 0;
updateClientSlot();
return;
}
// Force a source slot recalc if the stack has changed to something that cannot be converted to the target
if (!source.isEmpty() && !CarvingUtils.getChiselRegistry().getGroup(source.getItem()).equals(g)) {
source = ItemStack.EMPTY;
}
IChiselItem chiselitem = (IChiselItem) chisel.getItem();
// Make sure to run this block if the source stack is removed, so a new one can be found
if ((sourceSlot < 0 && getWorld().getGameTime() % 20 == 0) || sourceSlot >= 0) {
// Reset source slot if it's been removed
if (source.isEmpty()) {
setSourceSlot(-1);
}
// Make sure we can output this stack
ItemStack res = new ItemStack(v.getItem());
if (!source.isEmpty()) {
res.setCount(source.getCount());
}
if (source.isEmpty() || canOutput(res)) {
for (int i = 0; sourceSlot < 0 && i < getInputInv().getSlots(); i++) {
ItemStack stack = getInputInv().getStackInSlot(i);
if (!stack.isEmpty() && g.equals(CarvingUtils.getChiselRegistry().getGroup(stack.getItem()).orElse(null))) {
res.setCount(stack.getCount());
if (canOutput(res) && chiselitem.canChisel(getWorld(), FakePlayerFactory.getMinecraft((ServerWorld) getWorld()), chisel, v)) {
setSourceSlot(i);
source = res.copy();
}
}
}
} else {
setSourceSlot(-1);
}
}
if (sourceSlot >= 0) {
source = getInputInv().getStackInSlot(sourceSlot);
Validate.notNull(source);
ICarvingVariation sourceVar = CarvingUtils.getChiselRegistry().getVariation(source.getItem()).orElse(null);
if (sourceVar != v) {
if (progress < MAX_PROGRESS) {
if (!Configurations.autoChiselNeedsPower) {
// Add constant progress
progress = Math.min(MAX_PROGRESS, progress + BASE_PROGRESS);
}
// Compute progress added by FE
int toUse = Math.min(MAX_PROGRESS - progress, getPowerProgressPerTick());
// Compute FE usage
int powerToUse = getUsagePerTick();
// Avoid NaN
if (toUse > 0 && powerToUse > 0) {
if (Configurations.autoChiselPowered) {
int used = energyStorage.extractEnergy(powerToUse, false);
progress += toUse * ((float) used / powerToUse);
} else {
progress += toUse;
}
}
} else {
ItemStack res = new ItemStack(v.getItem());
source = source.copy();
chisel = chisel.copy();
ServerPlayerEntity player = FakePlayerFactory.getMinecraft((ServerWorld) getWorld());
player.inventory.mainInventory.set(player.inventory.currentItem, chisel);
// TODO should this send an explicit packet for item break? currently just checks for empty stack on the client
res = chiselitem.craftItem(chisel, source, res, player, $ -> {
});
player.inventory.mainInventory.set(player.inventory.currentItem, ItemStack.EMPTY);
chiselitem.onChisel(getWorld(), player, chisel, v);
inputInv.setStackInSlot(sourceSlot, source);
Chisel.network.send(PacketDistributor.NEAR.with(targetNearby()), new MessageAutochiselFX(getPos(), chisel, sourceVar.getBlock().getDefaultState()));
otherInv.setStackInSlot(0, chisel);
mergeOutput(res);
// Try the next slot, if this is invalid it will be fixed next update
setSourceSlot((sourceSlot + 1) % getInputInv().getSlots());
progress = 0;
}
} else {
// This is the same variation, so just move it to the output
inputInv.setStackInSlot(sourceSlot, ItemStack.EMPTY);
mergeOutput(source);
}
} else {
progress = 0;
}
updateClientSlot();
}
Aggregations