Skip to content

Commit

Permalink
Merge pull request #573 from SquidDev/generic-peripherals
Browse files Browse the repository at this point in the history
Generic peripherals
  • Loading branch information
SirEndii authored Apr 10, 2024
2 parents 048e4b9 + e7b1220 commit ca760a8
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 270 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kotlinx_coroutines_version=1.6.0-RC3
ttoolkit_version=0.1.3

# Mod dependencies
cc_version=1.108.4
cc_version=1.110.0
curios_version=5.2.0+1.20.1
minecolonies_version=1.20.1-1.1.472-BETA
appliedenergistics_version=15.0.9-beta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public static void commonSetup() {

@SubscribeEvent
public static void interModComms(InterModEnqueueEvent event) {
if (!curiosLoaded)
return;
/*
if (!curiosLoaded) {
}
// InterModComms.sendTo("curios", SlotTypeMessage.REGISTER_TYPE, () -> new SlotTypeMessage.Builder("glasses").size(1).build());
InterModComms.sendTo("curios", SlotTypeMessage.REGISTER_TYPE, () -> new SlotTypeMessage.Builder("glasses").size(1).build());
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public static long getTotalItemStorage(IGridNode node) {
total += disk.getBytes(null);
}
} else if (APAddons.aeAdditionsLoaded && (stack.getItem() instanceof SuperStorageCell superStorageCell)) {
total += superStorageCell.getKiloBytes() * 1024;
total += superStorageCell.getKiloBytes() * 1024L;
}
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ public static long getTotalFluidStorage(IGridNode node) {
total += cell.getBytes(null);
}
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof SuperStorageCell superStorageCell) {
total += superStorageCell.getKiloBytes() * 1024;
total += superStorageCell.getKiloBytes() * 1024L;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package de.srendi.advancedperipherals.common.addons.botania;

import de.srendi.advancedperipherals.common.addons.computercraft.integrations.IntegrationPeripheralProvider;
import vazkii.botania.api.block_entity.GeneratingFlowerBlockEntity;
import vazkii.botania.common.block.block_entity.mana.ManaPoolBlockEntity;
import vazkii.botania.common.block.block_entity.mana.ManaSpreaderBlockEntity;
import dan200.computercraft.api.ComputerCraftAPI;

public class Integration implements Runnable {

@Override
public void run() {
IntegrationPeripheralProvider.registerBlockEntityIntegration(ManaFlowerIntegration::new, GeneratingFlowerBlockEntity.class);
IntegrationPeripheralProvider.registerBlockEntityIntegration(ManaPoolIntegration::new, ManaPoolBlockEntity.class);
IntegrationPeripheralProvider.registerBlockEntityIntegration(SpreaderIntegration::new, ManaSpreaderBlockEntity.class);
ComputerCraftAPI.registerGenericSource(new ManaFlowerIntegration());
ComputerCraftAPI.registerGenericSource(new ManaPoolIntegration());
ComputerCraftAPI.registerGenericSource(new SpreaderIntegration());
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
package de.srendi.advancedperipherals.common.addons.botania;

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.lib.peripherals.BlockEntityIntegrationPeripheral;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import de.srendi.advancedperipherals.lib.peripherals.APGenericPeripheral;
import vazkii.botania.api.block_entity.GeneratingFlowerBlockEntity;

public class ManaFlowerIntegration extends BlockEntityIntegrationPeripheral<GeneratingFlowerBlockEntity> {
public class ManaFlowerIntegration implements APGenericPeripheral {

public ManaFlowerIntegration(BlockEntity entity) {
super(entity);
}

@NotNull
@Override
public String getType() {
public String getPeripheralType() {
return "manaFlower";
}

@LuaFunction(mainThread = true)
public final boolean isFloating() {
public final boolean isFloating(GeneratingFlowerBlockEntity blockEntity) {
return blockEntity.isFloating();
}

@LuaFunction(mainThread = true)
public final int getMaxMana() {
public final int getMaxMana(GeneratingFlowerBlockEntity blockEntity) {
return blockEntity.getMaxMana();
}

@LuaFunction(mainThread = true)
public final int getMana() {
public final int getMana(GeneratingFlowerBlockEntity blockEntity) {
return blockEntity.getMana();
}

@LuaFunction(mainThread = true)
public final boolean isOnEnchantedSoil() {
public final boolean isOnEnchantedSoil(GeneratingFlowerBlockEntity blockEntity) {
return blockEntity.overgrowth;
}

@LuaFunction(mainThread = true)
public final boolean isFull() {
public final boolean isFull(GeneratingFlowerBlockEntity blockEntity) {
return blockEntity.getMana() >= blockEntity.getMaxMana();
}

@LuaFunction(mainThread = true)
public final boolean isEmpty() {
public final boolean isEmpty(GeneratingFlowerBlockEntity blockEntity) {
return blockEntity.getMana() == 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,61 @@

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.lib.peripherals.BlockEntityIntegrationPeripheral;
import de.srendi.advancedperipherals.lib.peripherals.APGenericPeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;
import vazkii.botania.common.block.block_entity.mana.ManaPoolBlockEntity;

import java.util.List;
import java.util.stream.Collectors;

public class ManaPoolIntegration extends BlockEntityIntegrationPeripheral<ManaPoolBlockEntity> {
public class ManaPoolIntegration implements APGenericPeripheral {

public ManaPoolIntegration(BlockEntity entity) {
super(entity);
}

@NotNull
@Override
public String getType() {
public String getPeripheralType() {
return "manaPool";
}

@LuaFunction(mainThread = true)
public final int getMana() {
public final int getMana(ManaPoolBlockEntity blockEntity) {
return blockEntity.getCurrentMana();
}

@LuaFunction(mainThread = true)
public final int getMaxMana() {
public final int getMaxMana(ManaPoolBlockEntity blockEntity) {
return blockEntity.getMaxMana();
}

@LuaFunction(mainThread = true)
public final int getManaNeeded() {
public final int getManaNeeded(ManaPoolBlockEntity blockEntity) {
return blockEntity.getAvailableSpaceForMana();
}

@LuaFunction(mainThread = true)
public final boolean isFull() {
public final boolean isFull(ManaPoolBlockEntity blockEntity) {
return blockEntity.isFull();
}

@LuaFunction(mainThread = true)
public final boolean isEmpty() {
public final boolean isEmpty(ManaPoolBlockEntity blockEntity) {
return blockEntity.getCurrentMana() == 0;
}

@LuaFunction(mainThread = true)
public final boolean canChargeItem() {
public final boolean canChargeItem(ManaPoolBlockEntity blockEntity) {
return blockEntity.isOutputtingPower();
}

@LuaFunction(mainThread = true)
public final boolean hasItems() {
return !getPoolItems().isEmpty();
public final boolean hasItems(ManaPoolBlockEntity blockEntity) {
return !getPoolItems(blockEntity).isEmpty();
}

@LuaFunction(mainThread = true)
public final Object getItems() {
List<ItemStack> items = getPoolItems();
public final Object getItems(ManaPoolBlockEntity blockEntity) {
List<ItemStack> items = getPoolItems(blockEntity);
if(items.isEmpty())
return null;
Object[] luaStacks = new Object[items.size()];
Expand All @@ -75,7 +68,7 @@ public final Object getItems() {
return luaStacks;
}

private List<ItemStack> getPoolItems() {
private List<ItemStack> getPoolItems(ManaPoolBlockEntity blockEntity) {
BlockPos position = blockEntity.getBlockPos();
return blockEntity.getLevel().getEntitiesOfClass(ItemEntity.class, new AABB(position, position.offset(1, 1, 1)))
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,55 @@

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.lib.peripherals.BlockEntityIntegrationPeripheral;
import de.srendi.advancedperipherals.lib.peripherals.APGenericPeripheral;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import vazkii.botania.common.block.block_entity.mana.ManaSpreaderBlockEntity;

public class SpreaderIntegration extends BlockEntityIntegrationPeripheral<ManaSpreaderBlockEntity> {
public class SpreaderIntegration implements APGenericPeripheral {

public SpreaderIntegration(BlockEntity entity) {
super(entity);
}

@NotNull
@Override
public String getType() {
public String getPeripheralType() {
return "manaSpreader";
}

@LuaFunction(mainThread = true)
public final int getMana() {
public final int getMana(ManaSpreaderBlockEntity blockEntity) {
return blockEntity.getCurrentMana();
}

@LuaFunction(mainThread = true)
public final int getMaxMana() {
public final int getMaxMana(ManaSpreaderBlockEntity blockEntity) {
return blockEntity.getMaxMana();
}

@LuaFunction(mainThread = true)
public final Object getBounding() {
public final Object getBounding(ManaSpreaderBlockEntity blockEntity) {
if (blockEntity.getBinding() == null) return null;
return LuaConverter.posToObject(blockEntity.getBinding());
}

@LuaFunction(mainThread = true)
public final String getVariant() {
public final String getVariant(ManaSpreaderBlockEntity blockEntity) {
return blockEntity.getVariant().toString();
}

@LuaFunction(mainThread = true)
public final boolean isFull() {
public final boolean isFull(ManaSpreaderBlockEntity blockEntity) {
return blockEntity.isFull();
}

@LuaFunction(mainThread = true)
public final boolean isEmpty() {
public final boolean isEmpty(ManaSpreaderBlockEntity blockEntity) {
return blockEntity.getCurrentMana() == 0;
}

@LuaFunction(mainThread = true)
public final boolean hasLens() {
public final boolean hasLens(ManaSpreaderBlockEntity blockEntity) {
return blockEntity.getItem(0) != ItemStack.EMPTY;
}

@LuaFunction(mainThread = true)
public final Object getLens() {
public final Object getLens(ManaSpreaderBlockEntity blockEntity) {
if(blockEntity.getItem(0) == ItemStack.EMPTY)
return null;
return LuaConverter.stackToObject(blockEntity.getItem(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
package de.srendi.advancedperipherals.common.addons.computercraft.integrations;

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.lib.peripherals.BlockEntityIntegrationPeripheral;
import de.srendi.advancedperipherals.lib.peripherals.APGenericPeripheral;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BeaconBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;

public class BeaconIntegration extends BlockEntityIntegrationPeripheral<BeaconBlockEntity> {

public BeaconIntegration(BlockEntity entity) {
super(entity);
}

public class BeaconIntegration implements APGenericPeripheral {
@Override
public @NotNull String getType() {
public String getPeripheralType() {
return "beacon";
}

@LuaFunction(mainThread = true)
public final int getLevel() {
public final int getLevel(BeaconBlockEntity blockEntity) {
// because levels are now protected field .... why?
CompoundTag savedData = blockEntity.saveWithoutMetadata();
return savedData.getInt("Levels");
}

@LuaFunction(mainThread = true)
public final String getPrimaryEffect() {
public final String getPrimaryEffect(BeaconBlockEntity blockEntity) {
return blockEntity.primaryPower == null ? "none" : blockEntity.primaryPower.getDescriptionId();
}

@LuaFunction(mainThread = true)
public final String getSecondaryEffect() {
public final String getSecondaryEffect(BeaconBlockEntity blockEntity) {
return blockEntity.secondaryPower == null ? "none" : blockEntity.secondaryPower.getDescriptionId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.srendi.advancedperipherals.common.addons.computercraft.integrations;

import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralProvider;
import de.srendi.advancedperipherals.AdvancedPeripherals;
Expand All @@ -10,7 +11,6 @@
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.NoteBlock;
import net.minecraft.world.level.block.entity.BeaconBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
Expand All @@ -37,7 +37,10 @@ private static void registerIntegration(IPeripheralIntegration integration) {
* @param integration integration generator
* @param tileClass target integration class
* @param <T> target integration
*
* @deprecated will be removed in 1.21. Use generics instead, see existing integrations
*/
@Deprecated(forRemoval = true, since = "1.20.1-0.7.39")
public static <T extends BlockEntity> void registerBlockEntityIntegration(Function<BlockEntity, BlockEntityIntegrationPeripheral<T>> integration, Class<T> tileClass) {
registerIntegration(new BlockEntityIntegration(integration, tileClass::isInstance));
}
Expand All @@ -49,7 +52,10 @@ public static <T extends BlockEntity> void registerBlockEntityIntegration(Functi
* @param tileClass target integration class
* @param priority Integration priority, lower is better
* @param <T> target integration
*
* @deprecated will be removed in 1.21. Use generics instead, see existing integrations
*/
@Deprecated(forRemoval = true, since = "1.20.1-0.7.39")
public static <T extends BlockEntity> void registerBlockEntityIntegration(Function<BlockEntity, BlockEntityIntegrationPeripheral<T>> integration, Class<T> tileClass, int priority) {
registerIntegration(new BlockEntityIntegration(integration, tileClass::isInstance, priority));
}
Expand All @@ -63,13 +69,16 @@ public static <T extends BlockEntity> void registerBlockEntityIntegration(Functi
* @param predicate target block entity
* @param priority Integration priority, lower is better
* @param <T> target integration
*
* @deprecated will be removed in 1.21. Use generics instead, see existing integrations
*/
@Deprecated(forRemoval = true, since = "1.20.1-0.7.39")
public static <T extends BlockEntity> void registerBlockEntityIntegration(Function<BlockEntity, BlockEntityIntegrationPeripheral<T>> integration, Class<T> tileClass, Predicate<T> predicate, int priority) {
registerIntegration(new BlockEntityIntegration(integration, tile -> tileClass.isInstance(tile) && predicate.test((T) tile), priority));
}

public static void load() {
registerIntegration(new BlockEntityIntegration(BeaconIntegration::new, BeaconBlockEntity.class::isInstance));
ComputerCraftAPI.registerGenericSource(new BeaconIntegration());
registerIntegration(new BlockIntegration(NoteBlockIntegration::new, NoteBlock.class::isInstance));

for (String mod : SUPPORTED_MODS) {
Expand Down
Loading

0 comments on commit ca760a8

Please sign in to comment.