Fix CC peripherals

This commit is contained in:
Andrew-71 2023-08-03 18:07:34 +03:00
parent 4c6939e69f
commit b264703521
17 changed files with 189 additions and 138 deletions

View file

@ -16,6 +16,7 @@ dependencies {
implementation project(":common")
modImplementation("curse.maven:tardim-531315:4668945")
// modCompileOnly("com.simibubi.create:create-fabric-1.20.1:0.5.1-d-build.1118+mc1.20.1")
modCompileOnly("cc.tweaked:cc-tweaked-$minecraft_version-fabric-api:$cc_version")
}

View file

@ -38,7 +38,6 @@ public class FoodMachineBlock extends HorizontalDirectionalBlock implements Enti
public FoodMachineBlock() {
super(Properties.of().strength(2, 4).noOcclusion().mapColor(MapColor.METAL)); // No occlusion?
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH));
//this.registerDefaultState(this.stateDefinition.any().setValue(HORIZONTAL_FACING, Direction.NORTH));
}
public BlockState getStateForPlacement(BlockPlaceContext $$0) {
@ -63,14 +62,13 @@ public class FoodMachineBlock extends HorizontalDirectionalBlock implements Enti
if (be instanceof FoodMachineBlockEntity && w.dimension() == TRDDimensions.TARDIS) {
TardimData data = TardimManager.getFromPos(blockPos);
if (data != null && data.hasPermission(player)) {
if (data.getFuel() >= 0.05) {
data.setFuel(data.getFuel() - 0.05); // Remove some fuel in exchange for food
if (data.getFuel() >= 0.2) {
data.setFuel(data.getFuel() - 0.2); // Remove some fuel in exchange for food
ItemEntity food = new ItemEntity(EntityType.ITEM, w);
// Select type of food here
food.setItem(new ItemStack(Items.BREAD, 1));
food.setPos(Vec3.atCenterOf(blockPos).add(new Vec3(0, 0.2, 0)));
w.addFreshEntity(food);
} else {

View file

@ -9,6 +9,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import su.a71.tardim_ic.tardim_ic.registration.Registration;
/*
@ -24,15 +25,14 @@ public class CommandCloisterBell implements ICommand {
try {
Level lvl = player.level();
if (!lvl.isClientSide) {
// lvl.playSound(
// null,
// pos,
// Registration.CLOISTER_SOUND_EVENT,
// SoundSource.BLOCKS,
// 1.5f,
// 1f
// );
// TODO: Re-add
lvl.playSound(
null,
pos,
Registration.CLOISTER_BELL,
SoundSource.BLOCKS,
1.5f,
1f
);
}
} catch (Exception var9) {
CommandTardimBase.sendResponse(player, "There was an error", CommandTardimBase.ResponseType.FAIL, source);

View file

@ -37,6 +37,7 @@ import net.minecraft.world.phys.Vec3;
import su.a71.tardim_ic.computercraft_compat.digital_interface.DigitalInterfaceBlock;
import su.a71.tardim_ic.computercraft_compat.entity.FakeTardimPeripheralTileEntity;
import su.a71.tardim_ic.tardim_ic.registration.Registration;
import su.a71.tardim_ic.utils.FakePlayer;
import java.util.*;
@ -150,7 +151,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final ObjectLuaTable getCurrentLocation() throws LuaException {
Location loc = getTardimData().getCurrentLocation();
Location loc = getTardimData().getCurrentLocation();
return new ObjectLuaTable(Map.of(
"dimension", loc.getLevel().location().toString(),
"pos", new ObjectLuaTable(Map.of(
@ -175,7 +176,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final ObjectLuaTable getTravelLocation() throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(data.getCurrentLocation());
}
@ -197,12 +198,12 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final ObjectLuaTable getCompanions() throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
Map<Integer, String> companions = new HashMap<>();
for (int i = 0; i < data.getCompanions().size(); i++) {
companions.put(i + 1, data.getCompanions().get(i).getUsername());
}
return new ObjectLuaTable(companions);
for (int i = 0; i < data.getCompanions().size(); i++) {
companions.put(i + 1, data.getCompanions().get(i).getUsername());
}
return new ObjectLuaTable(companions);
}
/**
@ -255,7 +256,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final void setDimension(String dimension) throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
String key = dimension;
dimension = toTitleCase(dimension);
@ -302,7 +303,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
if (this.getTileEntity().getLevel().isClientSide()) {
return;
}
TardimData data = getTardimData();
TardimData data = getTardimData();
UUID uuid = data.getOwner();
String username = data.getOwnerName();
@ -339,9 +340,9 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
PlayerList playerList = this.getTileEntity().getLevel().getServer().getPlayerList();
ServerPlayer player = playerList.getPlayerByName(username);
if (player == null) {
throw new LuaException("Player not found");
}
if (player == null) {
throw new LuaException("Player not found");
}
for (ItemStack armour : player.getArmorSlots()) {
// if (armour.is(PERSONAL_JAMMER)) {
@ -350,11 +351,11 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
// TODO: Re-add
}
ResourceKey<Level> dim = player.getCommandSenderWorld().dimension();
BlockPos pos = player.blockPosition();
ResourceKey<Level> dim = player.getCommandSenderWorld().dimension();
BlockPos pos = player.blockPosition();
setDimension(dim.location().toString());
setTravelLocation(pos.getX(), pos.getY(), pos.getZ());
setDimension(dim.location().toString());
setTravelLocation(pos.getX(), pos.getY(), pos.getZ());
}
/**
@ -368,13 +369,13 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
return null;
}
PlayerList playerList = this.getTileEntity().getLevel().getServer().getPlayerList();
PlayerList playerList = this.getTileEntity().getLevel().getServer().getPlayerList();
Map<Integer, String> players = new HashMap<>();
for (int i = 0; i < playerList.getPlayers().size(); i++) {
players.put(i + 1, playerList.getPlayers().get(i).getGameProfile().getName());
}
return new ObjectLuaTable(players);
return new ObjectLuaTable(players);
}
/**
@ -383,8 +384,8 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final String getDoorRotation() throws LuaException {
TardimData data = getTardimData();
Direction rotation = data.getTravelLocation().getFacing();
TardimData data = getTardimData();
Direction rotation = data.getTravelLocation().getFacing();
switch (rotation) {
case NORTH -> {
return "north";
@ -410,7 +411,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final void setDoorRotation(String rotation) throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
switch (rotation) {
case "north" -> data.getTravelLocation().setFacing(Direction.NORTH);
case "east" -> data.getTravelLocation().setFacing(Direction.EAST);
@ -427,7 +428,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final void toggleDoorRotation() throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
@ -454,18 +455,18 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
*/
@LuaFunction(mainThread = true)
public final void coordAdd(String axis, int amount) throws LuaException {
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
Location location = data.getTravelLocation();
switch (axis) {
case "x" -> location.addPosition(amount, 0, 0);
Location location = data.getTravelLocation();
switch (axis) {
case "x" -> location.addPosition(amount, 0, 0);
case "y" -> location.addPosition(0, amount, 0);
case "z" -> location.addPosition(0, 0, amount);
default -> throw new LuaException("Invalid axis");
}
}
}
/**
@ -477,7 +478,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
return;
}
TardimData data = getTardimData();
TardimData data = getTardimData();
if (data.isInFlight()) {
throw new LuaException("TARDIM is already in flight");
@ -486,7 +487,7 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
ServerLevel level = this.tileEntity.getLevel().getServer().getLevel(loc.getLevel());
ItemTardim.destroyTardim(level, loc.getPos(), Direction.NORTH);
data.setInFlight(true);
if (data.getTravelLocation() == null) {
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
@ -776,15 +777,14 @@ public class DigitalInterfacePeripheral extends TardimPeripheral<DigitalInterfac
try {
Level lvl = this.tileEntity.getLevel();
if (!lvl.isClientSide) {
// lvl.playSound(
// null,
// this.tileEntity.getPos(),
// Registration.CLOISTER_SOUND_EVENT,
// SoundSource.BLOCKS,
// 1.5f,
// 1f
// );
// TODO: Re-add
lvl.playSound(
null,
this.tileEntity.getPos(),
Registration.CLOISTER_BELL,
SoundSource.BLOCKS,
1.5f,
1f
);
}
} catch (Exception var9) {
throw new LuaException("There was an error trying to play the sound");

View file

@ -0,0 +1,32 @@
package su.a71.tardim_ic.mixin;
import com.swdteam.tardim.tardim.TardimManager;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import static com.swdteam.tardim.tardim.TardimManager.FUEL_MAP;
// This mixin aims to make TARDIM fuel system less awful by allowing users to put standard furnace fuel into it.
@Mixin(value = TardimManager.class, remap = true)
public class BetterFuelMapMixin {
@Overwrite
public static boolean isFuel(Item i) {
return FUEL_MAP.containsKey(i) || AbstractFurnaceBlockEntity.getFuel().containsKey(i);
}
@Overwrite
public static double getFuel(Item i) {
if (!isFuel(i)) {
return 0.0;
}
if (!AbstractFurnaceBlockEntity.getFuel().containsKey(i)) {
return (Double)FUEL_MAP.get(i);
}
else
return AbstractFurnaceBlockEntity.getFuel().get(i) / 8000.0; // Adapt with coal's 1600 ticks -> 0.2 fuel
}
}

View file

@ -0,0 +1,50 @@
package su.a71.tardim_ic.mixin;
import com.swdteam.tardim.common.block.BlockFuelStorage;
import com.swdteam.tardim.common.init.TRDDimensions;
import com.swdteam.tardim.tardim.TardimData;
import com.swdteam.tardim.tardim.TardimManager;
import com.swdteam.tardim.tileentity.TileEntityFuelStorage;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HopperBlock;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import static su.a71.tardim_ic.Constants.LOG;
@Mixin(value = TileEntityFuelStorage.class, remap = true)
public class BetterFuelStorageMixin {
// This is rather inefficient as we iterate 2 times
// However, the hoppers are so small and this method is called so rarely that it should be fine.
@Inject(method = "serverTick(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lcom/swdteam/tardim/tileentity/TileEntityFuelStorage;)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/HopperBlockEntity;removeItem(II)Lnet/minecraft/world/item/ItemStack;"),
locals = LocalCapture.CAPTURE_FAILHARD)
private static void saveLavaBuckets(Level world, BlockPos pos, BlockState state, TileEntityFuelStorage blockEntity, CallbackInfo ci) {
HopperBlockEntity mixin_hopper = (HopperBlockEntity)world.getBlockEntity(blockEntity.getBlockPos().above());
for(int j = 0; j < mixin_hopper.getContainerSize(); ++j) {
ItemStack stack = mixin_hopper.getItem(j);
double fuel = TardimManager.getFuel(stack.getItem());
if (fuel > 0.0) {
if (stack.getItem() instanceof BucketItem) {
LOG.info("THIS IS A BUCKET");
mixin_hopper.setItem(j, new ItemStack(stack.getItem().getCraftingRemainingItem(), 2));
} else {
mixin_hopper.removeItem(j, 1);
}
}
}
}
}

View file

@ -1,24 +1,27 @@
package su.a71.tardim_ic.tardim_ic.registration;
import com.swdteam.tardim.common.init.CommandManager;
import dan200.computercraft.api.ComputerCraftAPI;
import com.swdteam.tardim.common.block.BlockFuelStorage;
import com.swdteam.tardim.common.block.BlockRotor;
import com.swdteam.tardim.common.block.BlockTardimScanner;
import dan200.computercraft.api.peripheral.PeripheralLookup;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import su.a71.tardim_ic.Constants;
import su.a71.tardim_ic.command.CommandModemTransmit;
import su.a71.tardim_ic.computercraft_compat.digital_interface.DigitalInterfaceBlock;
import su.a71.tardim_ic.computercraft_compat.digital_interface.DigitalInterfaceTileEntity;
import su.a71.tardim_ic.computercraft_compat.entity.FakeTardimPeripheralTileEntity;
import su.a71.tardim_ic.computercraft_compat.peripherals.DigitalInterfacePeripheral;
import su.a71.tardim_ic.computercraft_compat.peripherals.FuelStoragePeripheral;
import su.a71.tardim_ic.computercraft_compat.peripherals.TardimScannerPeripheral;
import su.a71.tardim_ic.computercraft_compat.peripherals.TimeRotorPeripheral;
import static com.swdteam.tardim.common.init.TRDBlocks.*;
import static su.a71.tardim_ic.tardim_ic.registration.Registration.registerBlock;
//import su.a71.tardim_ic.computercraft_compat.blocks.digital_interface.DigitalInterfaceTileEntity;
public class ComputerCraftCompat {
public static final Block DIGITAL_TARDIM_INTERFACE = new DigitalInterfaceBlock();
@ -32,15 +35,22 @@ public class ComputerCraftCompat {
public static void register() {
Constants.LOG.info("Loaded ComputerCraft compatibility!");
registerBlock("digital_tardim_interface", () -> DIGITAL_TARDIM_INTERFACE, null);
//
// Registry.register(Registry.BLOCK, new ResourceLocation(Constants.MOD_ID, "digital_tardim_interface"), DIGITAL_TARDIM_INTERFACE);
// Registry.register(Registry.ITEM, new ResourceLocation(Constants.MOD_ID, "digital_tardim_interface"), new BlockItem(DIGITAL_TARDIM_INTERFACE, new FabricItemSettings().tab(Registration.TARDIM_IC_TAB)));
// CommandManager.register(new CommandModemTransmit());
// PeripheralLookup.get().registerSelf();
PeripheralLookup.get().registerForBlockEntity((entity, direction) -> new DigitalInterfacePeripheral(new FakeTardimPeripheralTileEntity(entity.getBlockPos(), entity.getLevel())), DIGITAL_INTERFACE_BE);
PeripheralLookup.get().registerForBlocks((world, pos, state, blockEntity, direction) -> {
if (state.getBlock() instanceof BlockFuelStorage) {
return new FuelStoragePeripheral(new FakeTardimPeripheralTileEntity(pos, world));
} else if (state.getBlock() instanceof BlockRotor) {
return new TimeRotorPeripheral(new FakeTardimPeripheralTileEntity(pos, world));
} else if (state.getBlock() instanceof BlockTardimScanner) {
return new TardimScannerPeripheral(new FakeTardimPeripheralTileEntity(pos, world));
}
return null;
}, FUEL_STORAGE, SCANNER, ROTOR);
}
// ComputerCraftAPI.registerPeripheralProvider(new TardimPeripheralProvider());
public static void addToTab(CreativeModeTab.ItemDisplayParameters itemDisplayParameters, CreativeModeTab.Output output) {
output.accept(DIGITAL_TARDIM_INTERFACE);
}
}

View file

@ -17,4 +17,6 @@ public class CreateCompat {
// AllDisplayBehaviours.assignBlockEntity(AllDisplayBehaviours.register(new ResourceLocation(Constants.MOD_ID, "fuel_storage_display_source"), new FuelLevelDisplaySource()), TILE_FUEL_STORAGE);
// AllDisplayBehaviours.assignBlockEntity(AllDisplayBehaviours.register(new ResourceLocation(Constants.MOD_ID, "fuel_required_display_source"), new RequiredFuelDisplaySource()), TILE_FUEL_STORAGE);
}
}

View file

@ -1,13 +1,19 @@
package su.a71.tardim_ic.tardim_ic.registration;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import su.a71.tardim_ic.Constants;
@ -25,34 +31,46 @@ public class Registration {
public static final Block REDSTONE_INPUT = new RedstoneInputBlock();
public static final BlockEntityType<RedstoneInputBlockEntity> REDSTONE_INPUT_BE = Registry.register(
BuiltInRegistries.BLOCK_ENTITY_TYPE,
new ResourceLocation("tardim_ic", "redstone_tardim_input"),
new ResourceLocation(Constants.MOD_ID, "redstone_tardim_input"),
FabricBlockEntityTypeBuilder.create(RedstoneInputBlockEntity::new, REDSTONE_INPUT).build()
);
public static final Block FOOD_MACHINE = new FoodMachineBlock();
public static final BlockEntityType<FoodMachineBlockEntity> FOOD_MACHINE_BE = Registry.register(
BuiltInRegistries.BLOCK_ENTITY_TYPE,
new ResourceLocation("tardim_ic", "food_machine"),
new ResourceLocation(Constants.MOD_ID, "food_machine"),
FabricBlockEntityTypeBuilder.create(FoodMachineBlockEntity::new, FOOD_MACHINE).build()
);
public static final SoundEvent CLOISTER_BELL = SoundEvent.createVariableRangeEvent(new ResourceLocation(Constants.MOD_ID, "cloister"));
public static final CreativeModeTab TAB = FabricItemGroup.builder()
.icon(() -> new ItemStack(REDSTONE_INPUT))
.title(Component.translatable("itemGroup.tardim_ic"))
.displayItems(((itemDisplayParameters, output) -> {
output.accept(REDSTONE_INPUT);
output.accept(FOOD_MACHINE);
if (FabricLoader.getInstance().isModLoaded("computercraft")) {
ComputerCraftCompat.addToTab(itemDisplayParameters, output);
}
}))
.build();
public static void registerBlock(String name, Supplier<? extends Block> supplier, CreativeModeTab tab)
{
Registry.register(BuiltInRegistries.BLOCK, new ResourceLocation(Constants.MOD_ID, name), supplier.get());
BlockItem blockItem = new BlockItem(supplier.get(), new FabricItemSettings());
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation(Constants.MOD_ID, name), blockItem);
// Optional<ResourceKey<CreativeModeTab>> key = Optional.ofNullable(BuiltInRegistries.CREATIVE_MODE_TAB.getKey(tab));
// key.ifPresent(itemGroupRegistryKey -> ItemGroupEvents.modifyEntriesEvent(itemGroupRegistryKey).register(content -> {
// content.add(blockItem);
// }));
}
public static void register() {
Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, new ResourceLocation("tardim_ic", "item_group"), TAB);
registerBlock("redstone_tardim_input", () -> REDSTONE_INPUT, null);
registerBlock("food_machine", () -> FOOD_MACHINE, null);
Registry.register(BuiltInRegistries.SOUND_EVENT, new ResourceLocation(Constants.MOD_ID, "cloister"), CLOISTER_BELL);
Exteriors.register();
if (FabricLoader.getInstance().isModLoaded("computercraft")) {
ComputerCraftCompat.register();

View file

@ -5,6 +5,9 @@
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_17",
"mixins": [
"CommandsMixin",
"BetterFuelStorageMixin",
"BetterFuelMapMixin"
],
"client": [
],