Skip to content

Commit

Permalink
Add lockable block functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Dec 27, 2019
1 parent f1f9cd9 commit ea07e0f
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ public interface MCBlockState extends MCMetadatable {
MCLocation getLocation();

void update();

boolean isLockable();

boolean isLocked();

String getLock();

void setLock(String key);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.laytonsmith.abstraction.bukkit.BukkitMCLocation;
import com.laytonsmith.abstraction.bukkit.BukkitMCMetadatable;
import org.bukkit.block.BlockState;
import org.bukkit.block.Lockable;

public class BukkitMCBlockState extends BukkitMCMetadatable implements MCBlockState {

Expand Down Expand Up @@ -41,4 +42,37 @@ public MCLocation getLocation() {
public void update() {
bs.update();
}

@Override
public boolean isLockable() {
return bs instanceof Lockable;
}

@Override
public boolean isLocked() {
if(bs instanceof Lockable) {
return ((Lockable) bs).isLocked();
} else {
throw new ClassCastException("Block is not a Lockable.");
}
}

@Override
public String getLock() {
if(bs instanceof Lockable) {
return ((Lockable) bs).getLock();
} else {
throw new ClassCastException("Block is not a Lockable.");
}
}

@Override
public void setLock(String key) {
if(bs instanceof Lockable) {
((Lockable) bs).setLock(key);
bs.update();
} else {
throw new ClassCastException("Block is not a Lockable.");
}
}
}
214 changes: 214 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.laytonsmith.abstraction.StaticLayer;
import com.laytonsmith.abstraction.blocks.MCBlock;
import com.laytonsmith.abstraction.blocks.MCBlockData;
import com.laytonsmith.abstraction.blocks.MCBlockState;
import com.laytonsmith.abstraction.blocks.MCCommandBlock;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.blocks.MCSign;
Expand Down Expand Up @@ -38,6 +39,7 @@
import com.laytonsmith.core.constructs.CNull;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.CVoid;
import com.laytonsmith.core.constructs.Construct;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.environments.CommandHelperEnvironment;
import com.laytonsmith.core.exceptions.CRE.CREBadEntityException;
Expand Down Expand Up @@ -2229,6 +2231,218 @@ public Mixed exec(Target t, com.laytonsmith.core.environments.Environment enviro
}
}

@api
public static class is_block_lockable extends AbstractFunction {

@Override
public String getName() {
return "is_block_lockable";
}

@Override
public Integer[] numArgs() {
return new Integer[]{1};
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRECastException.class, CREFormatException.class, CREInvalidWorldException.class};
}

@Override
public boolean isRestricted() {
return true;
}

@Override
public Boolean runAsync() {
return false;
}

@Override
public String docs() {
return "boolean {locationArray} Returns whether or not the block is lockable."
+ " ---- Locks prevent players from accessing the block's interface unless they are holding an item"
+ " key with the same display name as the lock.";
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}

@Override
public Mixed exec(Target t, com.laytonsmith.core.environments.Environment environment, Mixed... args)
throws ConfigRuntimeException {
MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], null, t);
return CBoolean.get(loc.getBlock().getState().isLockable());
}
}

@api
public static class is_block_locked extends AbstractFunction {

@Override
public String getName() {
return "is_block_locked";
}

@Override
public Integer[] numArgs() {
return new Integer[]{1};
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRECastException.class, CREFormatException.class, CREInvalidWorldException.class,
CREIllegalArgumentException.class};
}

@Override
public boolean isRestricted() {
return true;
}

@Override
public Boolean runAsync() {
return false;
}

@Override
public String docs() {
return "boolean {locationArray} Returns whether or not the block is locked if its lockable."
+ " ---- Locks prevent players from accessing the block's interface unless they are holding an item"
+ " key with the same display name as the lock."
+ " Throws IllegalArgumentException if used on a block that isn't lockable.";
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}

@Override
public Mixed exec(Target t, com.laytonsmith.core.environments.Environment environment, Mixed... args)
throws ConfigRuntimeException {
MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], null, t);
MCBlockState block = loc.getBlock().getState();
if(!block.isLockable()) {
throw new CREIllegalArgumentException("Block is not lockable.", t);
}
return CBoolean.get(block.isLocked());
}
}

@api
public static class get_block_lock extends AbstractFunction {

@Override
public String getName() {
return "get_block_lock";
}

@Override
public Integer[] numArgs() {
return new Integer[]{1};
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRECastException.class, CREFormatException.class, CREInvalidWorldException.class,
CREIllegalArgumentException.class};
}

@Override
public boolean isRestricted() {
return true;
}

@Override
public Boolean runAsync() {
return false;
}

@Override
public String docs() {
return "string {locationArray} Returns the string lock for this block if its lockable."
+ " ---- Locks prevent players from accessing the block's interface unless they are holding an item"
+ " key with the same display name as the lock."
+ " Throws IllegalArgumentException if used on a block that isn't lockable.";
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}

@Override
public Mixed exec(Target t, com.laytonsmith.core.environments.Environment environment, Mixed... args)
throws ConfigRuntimeException {
MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], null, t);
MCBlockState block = loc.getBlock().getState();
if(!block.isLockable()) {
throw new CREIllegalArgumentException("Block is not lockable.", t);
}
return new CString(block.getLock(), t);
}
}

@api
public static class set_block_lock extends AbstractFunction {

@Override
public String getName() {
return "set_block_lock";
}

@Override
public Integer[] numArgs() {
return new Integer[]{2};
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CRECastException.class, CREFormatException.class, CREInvalidWorldException.class,
CREIllegalArgumentException.class};
}

@Override
public boolean isRestricted() {
return true;
}

@Override
public Boolean runAsync() {
return false;
}

@Override
public String docs() {
return "void {locationArray, string} Sets the string lock for this block if its lockable."
+ " Set an empty string or null to remove lock."
+ " ---- Locks prevent players from accessing the block's interface unless they are holding an item"
+ " key with the same display name as the lock."
+ " Throws IllegalArgumentException if used on a block that isn't lockable.";
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}

@Override
public Mixed exec(Target t, com.laytonsmith.core.environments.Environment environment, Mixed... args)
throws ConfigRuntimeException {
MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], null, t);
MCBlockState block = loc.getBlock().getState();
if(!block.isLockable()) {
throw new CREIllegalArgumentException("Block is not lockable.", t);
}
block.setLock(Construct.nval(args[1]));
return CVoid.VOID;
}
}

@api
public static class get_block_command extends AbstractFunction {

Expand Down

0 comments on commit ea07e0f

Please sign in to comment.