Skip to content

Commit

Permalink
fix: computer craft compat no longer crashes when CC is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
techno-sam committed Aug 8, 2024
1 parent 09d74f6 commit 50d817f
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 131 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
------------------------------------------------------
Numismatics 1.0.7
Numismatics 1.1.0
------------------------------------------------------
Additions
- Computer Craft: Tweaked compat for vendors and brass depositors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.ithundxr.createnumismatics.compat.Mods;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ActualComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.Details;
import dev.ithundxr.createnumismatics.multiloader.fluid.MultiloaderFluidStack;
import net.minecraft.world.item.ItemStack;
Expand All @@ -33,12 +34,7 @@
public class ComputerCraftProxy {
public static void register() {
fallbackFactory = FallbackComputerBehaviour::new;
Mods.COMPUTERCRAFT.executeIfInstalled(() -> ComputerCraftProxy::registerWithDependency);
}

@ExpectPlatform
static void registerWithDependency() {
throw new AssertionError();
Mods.COMPUTERCRAFT.executeIfInstalled(() -> ActualComputerCraftProxy::registerWithDependency);
}

public static Function<SmartBlockEntity, ? extends AbstractComputerBehaviour> fallbackFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.compat.computercraft.implementation;

import dev.architectury.injectables.annotations.ExpectPlatform;

public class ActualComputerCraftProxy {
@ExpectPlatform
public static void registerWithDependency() {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,11 @@

package dev.ithundxr.createnumismatics.compat.computercraft.fabric;

import com.google.common.collect.ImmutableMap;
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dan200.computercraft.api.peripheral.PeripheralLookup;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BankTerminalPeripheral;
import dev.ithundxr.createnumismatics.content.bank.AuthorizedCardItem;
import dev.ithundxr.createnumismatics.content.bank.CardItem;
import dev.ithundxr.createnumismatics.content.bank.IDCardItem;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.core.registries.Registries;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.UUID;

public class ComputerCraftProxyImpl {
public static void registerWithDependency() {
/* Comment if computercraft.implementation is not in the source set */
ComputerCraftProxy.computerFactory = ComputerBehaviour::new;

PeripheralLookup.get().registerFallback((level, blockPos, blockState, blockEntity, direction) -> ComputerBehaviour.peripheralProvider(level, blockPos));

Utils.runOnceRegistered(Registries.BLOCK, () -> {
PeripheralLookup.get().registerForBlocks(
(world, pos, state, blockEntity, context) -> BankTerminalPeripheral.INSTANCE,
NumismaticsBlocks.BANK_TERMINAL.get()
);
});

VanillaDetailRegistries.ITEM_STACK.addProvider((detailMap, stack) -> {
Map<Object, @Nullable Object> cardDetails = null;
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
UUID accountID = CardItem.get(stack);
if (accountID != null) {
cardDetails = Map.of(
"AccountID", accountID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.AUTHORIZED_CARDS.matches(stack)) {
AuthorizedCardItem.AuthorizationPair authorizationPair = AuthorizedCardItem.get(stack);
if (authorizationPair != null) {
UUID accountID = authorizationPair.accountID();
UUID authorizationID = authorizationPair.authorizationID();
cardDetails = Map.of(
"AccountID", accountID.toString(),
"AuthorizationID", authorizationID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.ID_CARDS.matches(stack)) {
UUID id = IDCardItem.get(stack);
if (id != null) {
cardDetails = Map.of(
"ID", id.toString()
);
}
}

if (cardDetails != null)
detailMap.put("numismatics", ImmutableMap.of("card", cardDetails));
});
}

public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) {
if (ComputerCraftProxy.computerFactory == null)
return ComputerCraftProxy.fallbackFactory.apply(sbe);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.compat.computercraft.implementation.fabric;

import com.google.common.collect.ImmutableMap;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dan200.computercraft.api.peripheral.PeripheralLookup;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BankTerminalPeripheral;
import dev.ithundxr.createnumismatics.content.bank.AuthorizedCardItem;
import dev.ithundxr.createnumismatics.content.bank.CardItem;
import dev.ithundxr.createnumismatics.content.bank.IDCardItem;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.core.registries.Registries;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.UUID;

public class ActualComputerCraftProxyImpl {
public static void registerWithDependency() {
/* Comment if computercraft.implementation is not in the source set */
ComputerCraftProxy.computerFactory = ComputerBehaviour::new;

PeripheralLookup.get().registerFallback((level, blockPos, blockState, blockEntity, direction) -> ComputerBehaviour.peripheralProvider(level, blockPos));

Utils.runOnceRegistered(Registries.BLOCK, () -> {
PeripheralLookup.get().registerForBlocks(
(world, pos, state, blockEntity, context) -> BankTerminalPeripheral.INSTANCE,
NumismaticsBlocks.BANK_TERMINAL.get()
);
});

VanillaDetailRegistries.ITEM_STACK.addProvider((detailMap, stack) -> {
Map<Object, @Nullable Object> cardDetails = null;
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
UUID accountID = CardItem.get(stack);
if (accountID != null) {
cardDetails = Map.of(
"AccountID", accountID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.AUTHORIZED_CARDS.matches(stack)) {
AuthorizedCardItem.AuthorizationPair authorizationPair = AuthorizedCardItem.get(stack);
if (authorizationPair != null) {
UUID accountID = authorizationPair.accountID();
UUID authorizationID = authorizationPair.authorizationID();
cardDetails = Map.of(
"AccountID", accountID.toString(),
"AuthorizationID", authorizationID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.ID_CARDS.matches(stack)) {
UUID id = IDCardItem.get(stack);
if (id != null) {
cardDetails = Map.of(
"ID", id.toString()
);
}
}

if (cardDetails != null)
detailMap.put("numismatics", ImmutableMap.of("card", cardDetails));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,11 @@

package dev.ithundxr.createnumismatics.compat.computercraft.forge;

import com.google.common.collect.ImmutableMap;
import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import dan200.computercraft.api.ForgeComputerCraftAPI;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BankTerminalPeripheral;
import dev.ithundxr.createnumismatics.content.bank.AuthorizedCardItem;
import dev.ithundxr.createnumismatics.content.bank.CardItem;
import dev.ithundxr.createnumismatics.content.bank.IDCardItem;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.UUID;

public class ComputerCraftProxyImpl {
public static void registerWithDependency() {
/* Comment if computercraft.implementation is not in the source set */
ComputerCraftProxy.computerFactory = ComputerBehaviour::new;

ForgeComputerCraftAPI.registerPeripheralProvider((level, pos, direction) -> {
BlockState state = level.getBlockState(pos);

if (NumismaticsBlocks.BANK_TERMINAL.has(state)) {
return LazyOptional.of(() -> BankTerminalPeripheral.INSTANCE);
}

return LazyOptional.empty();
});

VanillaDetailRegistries.ITEM_STACK.addProvider((detailMap, stack) -> {
Map<Object, @Nullable Object> cardDetails = null;
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
UUID accountID = CardItem.get(stack);
if (accountID != null) {
cardDetails = Map.of(
"AccountID", accountID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.AUTHORIZED_CARDS.matches(stack)) {
AuthorizedCardItem.AuthorizationPair authorizationPair = AuthorizedCardItem.get(stack);
if (authorizationPair != null) {
UUID accountID = authorizationPair.accountID();
UUID authorizationID = authorizationPair.authorizationID();
cardDetails = Map.of(
"AccountID", accountID.toString(),
"AuthorizationID", authorizationID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.ID_CARDS.matches(stack)) {
UUID id = IDCardItem.get(stack);
if (id != null) {
cardDetails = Map.of(
"ID", id.toString()
);
}
}

if (cardDetails != null)
detailMap.put("numismatics", ImmutableMap.of("card", cardDetails));
});
}
public static AbstractComputerBehaviour behaviour(SmartBlockEntity sbe) {
if (ComputerCraftProxy.computerFactory == null)
return ComputerCraftProxy.fallbackFactory.apply(sbe);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.compat.computercraft.implementation.forge;

import com.google.common.collect.ImmutableMap;
import dan200.computercraft.api.ForgeComputerCraftAPI;
import dan200.computercraft.api.detail.VanillaDetailRegistries;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.ComputerBehaviour;
import dev.ithundxr.createnumismatics.compat.computercraft.implementation.peripherals.BankTerminalPeripheral;
import dev.ithundxr.createnumismatics.content.bank.AuthorizedCardItem;
import dev.ithundxr.createnumismatics.content.bank.CardItem;
import dev.ithundxr.createnumismatics.content.bank.IDCardItem;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.UUID;

public class ActualComputerCraftProxyImpl {
public static void registerWithDependency() {
/* Comment if computercraft.implementation is not in the source set */
ComputerCraftProxy.computerFactory = ComputerBehaviour::new;

ForgeComputerCraftAPI.registerPeripheralProvider((level, pos, direction) -> {
BlockState state = level.getBlockState(pos);

if (NumismaticsBlocks.BANK_TERMINAL.has(state)) {
return LazyOptional.of(() -> BankTerminalPeripheral.INSTANCE);
}

return LazyOptional.empty();
});

VanillaDetailRegistries.ITEM_STACK.addProvider((detailMap, stack) -> {
Map<Object, @Nullable Object> cardDetails = null;
if (NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
UUID accountID = CardItem.get(stack);
if (accountID != null) {
cardDetails = Map.of(
"AccountID", accountID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.AUTHORIZED_CARDS.matches(stack)) {
AuthorizedCardItem.AuthorizationPair authorizationPair = AuthorizedCardItem.get(stack);
if (authorizationPair != null) {
UUID accountID = authorizationPair.accountID();
UUID authorizationID = authorizationPair.authorizationID();
cardDetails = Map.of(
"AccountID", accountID.toString(),
"AuthorizationID", authorizationID.toString()
);
}
} else if (NumismaticsTags.AllItemTags.ID_CARDS.matches(stack)) {
UUID id = IDCardItem.get(stack);
if (id != null) {
cardDetails = Map.of(
"ID", id.toString()
);
}
}

if (cardDetails != null)
detailMap.put("numismatics", ImmutableMap.of("card", cardDetails));
});
}
}

0 comments on commit 50d817f

Please sign in to comment.