-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'mc-1.16.x' into mc-1.17.x
- Loading branch information
Showing
25 changed files
with
611 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/dan200/computercraft/api/lua/LuaTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* This file is part of the public ComputerCraft API - http://www.computercraft.info | ||
* Copyright Daniel Ratcliffe, 2011-2021. This API may be redistributed unmodified and in full only. | ||
* For help using the API, and posting your mods, visit the forums at computercraft.info. | ||
*/ | ||
package dan200.computercraft.api.lua; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Map; | ||
|
||
import static dan200.computercraft.api.lua.LuaValues.*; | ||
|
||
public interface LuaTable<K, V> extends Map<K, V> | ||
{ | ||
/** | ||
* Compute the length of the array part of this table. | ||
* | ||
* @return This table's length. | ||
*/ | ||
default int length() | ||
{ | ||
int size = 0; | ||
while( containsKey( (double) (size + 1) ) ) size++; | ||
return size; | ||
} | ||
|
||
/** | ||
* Get an array entry as an integer. | ||
* | ||
* @param index The index in the table, starting at 1. | ||
* @return The table's value. | ||
* @throws LuaException If the value is not an integer. | ||
*/ | ||
default long getLong( int index ) throws LuaException | ||
{ | ||
Object value = get( (double) index ); | ||
if( !(value instanceof Number) ) throw badTableItem( index, "number", getType( value ) ); | ||
|
||
Number number = (Number) value; | ||
double asDouble = number.doubleValue(); | ||
if( !Double.isFinite( asDouble ) ) throw badTableItem( index, "number", getNumericType( asDouble ) ); | ||
return number.longValue(); | ||
} | ||
|
||
/** | ||
* Get a table entry as an integer. | ||
* | ||
* @param key The name of the field in the table. | ||
* @return The table's value. | ||
* @throws LuaException If the value is not an integer. | ||
*/ | ||
default long getLong( String key ) throws LuaException | ||
{ | ||
Object value = get( key ); | ||
if( !(value instanceof Number) ) throw badField( key, "number", getType( value ) ); | ||
|
||
Number number = (Number) value; | ||
double asDouble = number.doubleValue(); | ||
if( !Double.isFinite( asDouble ) ) throw badField( key, "number", getNumericType( asDouble ) ); | ||
return number.longValue(); | ||
} | ||
|
||
/** | ||
* Get an array entry as an integer. | ||
* | ||
* @param index The index in the table, starting at 1. | ||
* @return The table's value. | ||
* @throws LuaException If the value is not an integer. | ||
*/ | ||
default int getInt( int index ) throws LuaException | ||
{ | ||
return (int) getLong( index ); | ||
} | ||
|
||
/** | ||
* Get a table entry as an integer. | ||
* | ||
* @param key The name of the field in the table. | ||
* @return The table's value. | ||
* @throws LuaException If the value is not an integer. | ||
*/ | ||
default int getInt( String key ) throws LuaException | ||
{ | ||
return (int) getLong( key ); | ||
} | ||
|
||
|
||
@Nullable | ||
@Override | ||
default V put( K o, V o2 ) | ||
{ | ||
throw new UnsupportedOperationException( "Cannot modify LuaTable" ); | ||
} | ||
|
||
@Override | ||
default V remove( Object o ) | ||
{ | ||
throw new UnsupportedOperationException( "Cannot modify LuaTable" ); | ||
} | ||
|
||
@Override | ||
default void putAll( @Nonnull Map<? extends K, ? extends V> map ) | ||
{ | ||
throw new UnsupportedOperationException( "Cannot modify LuaTable" ); | ||
} | ||
|
||
@Override | ||
default void clear() | ||
{ | ||
throw new UnsupportedOperationException( "Cannot modify LuaTable" ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/dan200/computercraft/api/lua/ObjectLuaTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* This file is part of the public ComputerCraft API - http://www.computercraft.info | ||
* Copyright Daniel Ratcliffe, 2011-2021. This API may be redistributed unmodified and in full only. | ||
* For help using the API, and posting your mods, visit the forums at computercraft.info. | ||
*/ | ||
package dan200.computercraft.api.lua; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class ObjectLuaTable implements LuaTable<Object, Object> | ||
{ | ||
private final Map<Object, Object> map; | ||
|
||
public ObjectLuaTable( Map<?, ?> map ) | ||
{ | ||
this.map = Collections.unmodifiableMap( map ); | ||
} | ||
|
||
@Override | ||
public int size() | ||
{ | ||
return map.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() | ||
{ | ||
return map.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey( Object o ) | ||
{ | ||
return map.containsKey( o ); | ||
} | ||
|
||
@Override | ||
public boolean containsValue( Object o ) | ||
{ | ||
return map.containsKey( o ); | ||
} | ||
|
||
@Override | ||
public Object get( Object o ) | ||
{ | ||
return map.get( o ); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Set<Object> keySet() | ||
{ | ||
return map.keySet(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Collection<Object> values() | ||
{ | ||
return map.values(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Set<Entry<Object, Object>> entrySet() | ||
{ | ||
return map.entrySet(); | ||
} | ||
} |
Oops, something went wrong.