parent
1b562ae837
commit
b1efbdad95
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
import dan200.computercraft.api.lua.ILuaContext;
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
import dan200.computercraft.api.lua.LuaException;
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
|
import dan200.computercraft.shared.util.StringUtil;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -266,11 +267,7 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
|
||||||
{
|
{
|
||||||
throw new LuaException( "Expected string or nil" );
|
throw new LuaException( "Expected string or nil" );
|
||||||
}
|
}
|
||||||
label = (String)args[0];
|
label = StringUtil.normaliseLabel( (String) args[0] );
|
||||||
if( label.length() > 32 )
|
|
||||||
{
|
|
||||||
label = label.substring( 0, 32 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
m_apiEnvironment.setLabel( label );
|
m_apiEnvironment.setLabel( label );
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
|
||||||
|
import dan200.computercraft.shared.util.StringUtil;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
@ -87,6 +88,7 @@ public Object[] callMethod( IComputerAccess computer, ILuaContext context, int m
|
||||||
if( media != null )
|
if( media != null )
|
||||||
{
|
{
|
||||||
ItemStack disk = m_diskDrive.getDiskStack();
|
ItemStack disk = m_diskDrive.getDiskStack();
|
||||||
|
label = StringUtil.normaliseLabel( label );
|
||||||
if( media.setLabel( disk, label ) )
|
if( media.setLabel( disk, label ) )
|
||||||
{
|
{
|
||||||
m_diskDrive.setDiskStack( disk );
|
m_diskDrive.setDiskStack( disk );
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package dan200.computercraft.shared.util;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class StringUtil
|
||||||
|
{
|
||||||
|
private static final Pattern INVALID_PATTERN = Pattern.compile( "[^ -~]" );
|
||||||
|
|
||||||
|
public static String normaliseLabel( String label )
|
||||||
|
{
|
||||||
|
label = INVALID_PATTERN.matcher( label ).replaceAll( "" );
|
||||||
|
|
||||||
|
if( label.length() > 32 )
|
||||||
|
{
|
||||||
|
label = label.substring( 0, 32 );
|
||||||
|
}
|
||||||
|
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue