mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 13:42:59 +00:00 
			
		
		
		
	Merge pull request #167 from SquidDev-CC/hotfix/limit-label
Impose additional limitations on disk and computer labels
This commit is contained in:
		| @@ -8,6 +8,7 @@ package dan200.computercraft.core.apis; | |||||||
|  |  | ||||||
| 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 class OSAPI implements ILuaAPI | |||||||
|                     { |                     { | ||||||
|                         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.media.IMedia; | |||||||
| 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 class DiskDrivePeripheral implements IPeripheral | |||||||
|                 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,26 @@ | |||||||
|  | package dan200.computercraft.shared.util; | ||||||
|  |  | ||||||
|  | public class StringUtil | ||||||
|  | { | ||||||
|  |     public static String normaliseLabel( String label ) | ||||||
|  |     { | ||||||
|  |         if( label == null ) return null; | ||||||
|  |  | ||||||
|  |         int length = Math.min( 32, label.length() ); | ||||||
|  |         StringBuilder builder = new StringBuilder( length ); | ||||||
|  |         for (int i = 0; i < length; i++) | ||||||
|  |         { | ||||||
|  |             char c = label.charAt( i ); | ||||||
|  |             if( (c >= ' ' && c <= '~') || (c >= 161 && c <= 172) || (c >= 174 && c <= 255) ) | ||||||
|  |             { | ||||||
|  |                 builder.append( c ); | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 builder.append( '?' ); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return builder.toString(); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 Daniel Ratcliffe
					Daniel Ratcliffe