Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

Commit

Permalink
Use IExtendedLuaMachine to inject the debug API
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed May 3, 2017
1 parent ff2d641 commit 6b60738
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/main/java/org/squiddev/cctweaks/items/ItemDebugger.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.squiddev.cctweaks.items;

import com.google.common.base.Joiner;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.lua.LuaJLuaMachine;
import dan200.computercraft.shared.computer.blocks.TileComputerBase;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.util.PeripheralUtil;
Expand All @@ -13,8 +13,8 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.world.World;
import org.apache.commons.lang3.StringUtils;
import org.squiddev.cctweaks.api.IWorldPosition;
import org.squiddev.cctweaks.api.lua.IExtendedLuaMachine;
import org.squiddev.cctweaks.api.network.INetworkController;
import org.squiddev.cctweaks.api.network.INetworkNode;
import org.squiddev.cctweaks.api.network.IWorldNetworkNode;
Expand All @@ -26,7 +26,6 @@
import org.squiddev.cctweaks.core.utils.WorldPosition;
import org.squiddev.cctweaks.core.visualiser.NetworkPlayerWatcher;

import java.lang.reflect.Method;
import java.util.Set;

public class ItemDebugger extends ItemComputerAction {
Expand Down Expand Up @@ -61,18 +60,11 @@ protected boolean useComputer(ItemStack stack, EntityPlayer player, TileComputer
Object computer = ComputerAccessor.serverComputerComputer.get(serverComputer);
Object luaMachine = ComputerAccessor.computerMachine.get(computer);

if (luaMachine instanceof LuaJLuaMachine) {
org.luaj.vm2.LuaValue globals = (org.luaj.vm2.LuaValue) ComputerAccessor.luaMachineGlobals.get(luaMachine);
globals.load(new org.luaj.vm2.lib.DebugLib());
if (luaMachine instanceof IExtendedLuaMachine) {
((IExtendedLuaMachine) luaMachine).enableDebug();
} else {
try {
Method method = luaMachine.getClass().getMethod("injectDebug");
method.setAccessible(true);
method.invoke(luaMachine);
} catch (NoSuchMethodException e) {
DebugLogger.warn("Computer machine (" + luaMachine.getClass().getName() + ") has no .injectDebug() method. Cannot inject debug library.");
return false;
}
DebugLogger.warn("Do not know how to inject debug library into " + luaMachine.getClass().getName());
return false;
}
} catch (NullPointerException e) {
DebugLogger.warn("Could not add DebugLib", e);
Expand Down Expand Up @@ -168,7 +160,7 @@ private static IChatComponent withColor(String message, EnumChatFormatting color
}

private static IChatComponent info(Iterable<String> message) {
return info(StringUtils.join(message, ", "));
return info(Joiner.on(", ").join(message));
}

private static IChatComponent info(String message) {
Expand Down

0 comments on commit 6b60738

Please sign in to comment.