Skip to content

Commit

Permalink
Add decorated pot sherd functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Oct 14, 2023
1 parent 86a6583 commit bc011de
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.laytonsmith.abstraction.blocks;

import java.util.Map;

public interface MCDecoratedPot extends MCBlockState {

Map<Side, MCMaterial> getSherds();

void setSherd(Side side, MCMaterial sherd);

enum Side {
BACK,
FRONT,
LEFT,
RIGHT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCChest;
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCCommandBlock;
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCContainer;
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCDecoratedPot;
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCDispenser;
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCDropper;
import com.laytonsmith.abstraction.bukkit.blocks.BukkitMCFurnace;
Expand Down Expand Up @@ -113,6 +114,7 @@
import org.bukkit.block.CommandBlock;
import org.bukkit.block.Container;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.DecoratedPot;
import org.bukkit.block.Dispenser;
import org.bukkit.block.Dropper;
import org.bukkit.block.Furnace;
Expand Down Expand Up @@ -586,6 +588,9 @@ public static MCBlockState BukkitGetCorrectBlockState(BlockState bs) {
if(bs instanceof CommandBlock) {
return new BukkitMCCommandBlock((CommandBlock) bs);
}
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20_1) && bs instanceof DecoratedPot) {
return new BukkitMCDecoratedPot((DecoratedPot) bs);
}
return new BukkitMCBlockState(bs);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.laytonsmith.abstraction.bukkit.blocks;

import com.laytonsmith.abstraction.blocks.MCDecoratedPot;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import org.bukkit.Material;
import org.bukkit.block.DecoratedPot;

import java.util.HashMap;
import java.util.Map;

public class BukkitMCDecoratedPot extends BukkitMCBlockState implements MCDecoratedPot {

DecoratedPot dp;

public BukkitMCDecoratedPot(DecoratedPot pot) {
super(pot);
this.dp = pot;
}

@Override
public DecoratedPot getHandle() {
return this.dp;
}

@Override
public Map<MCDecoratedPot.Side, MCMaterial> getSherds() {
Map<MCDecoratedPot.Side, MCMaterial> sherds = new HashMap<>();
for(Map.Entry<DecoratedPot.Side, Material> sherd : this.dp.getSherds().entrySet()) {
sherds.put(MCDecoratedPot.Side.valueOf(sherd.getKey().name()), BukkitMCMaterial.valueOfConcrete(sherd.getValue()));
}
return sherds;
}

@Override
public void setSherd(MCDecoratedPot.Side side, MCMaterial sherd) {
DecoratedPot.Side concreteSide = DecoratedPot.Side.valueOf(side.name());
this.dp.setSherd(concreteSide, (Material) sherd.getHandle());
}
}
134 changes: 134 additions & 0 deletions src/main/java/com/laytonsmith/core/functions/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.laytonsmith.abstraction.blocks.MCBlockData;
import com.laytonsmith.abstraction.blocks.MCBlockState;
import com.laytonsmith.abstraction.blocks.MCCommandBlock;
import com.laytonsmith.abstraction.blocks.MCDecoratedPot;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.blocks.MCSign;
import com.laytonsmith.abstraction.blocks.MCSign.Side;
Expand Down Expand Up @@ -75,6 +76,7 @@

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Environment {
Expand Down Expand Up @@ -3191,4 +3193,136 @@ public Boolean runAsync() {
return false;
}
}

@api(environments = {CommandHelperEnvironment.class})
public static class get_decorated_pot_sherds extends AbstractFunction {

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

@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 "array {locationArray} Returns an associative array of sherds for each side of a decorated pot."
+ " The keys for each side are 'front', 'back', 'left', and 'right'."
+ " The values are one of any sherd materials or a brick if there's no decoration on that side.";
}

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

@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 bs = loc.getBlock().getState();
if(bs instanceof MCDecoratedPot decoratedPot) {
CArray sherds = CArray.GetAssociativeArray(t);
Map<MCDecoratedPot.Side, MCMaterial> potSherds = decoratedPot.getSherds();
for(Map.Entry<MCDecoratedPot.Side, MCMaterial> side : potSherds.entrySet()) {
sherds.set(side.getKey().name().toLowerCase(), side.getValue().name());
}
return sherds;
} else {
throw new CREFormatException("The block at the specified location is not a decorated pot", t);
}
}
}

@api(environments = {CommandHelperEnvironment.class})
public static class set_decorated_pot_sherds extends AbstractFunction {

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

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

@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 "void {locationArray, array} Set the sherds on the sides of a decorated pot."
+ " Takes an associative array with a key for each side: 'front', 'back', 'left', and 'right'."
+ " Each side accepts a sherd material name, or if no decoration is desired on that side, a brick."
+ " Throws a FormatException if a side or material is not valid.";
}

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

@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 bs = loc.getBlock().getState();
if(!(bs instanceof MCDecoratedPot decoratedPot)) {
throw new CREFormatException("The block at the specified location is not a decorated pot", t);
}
Mixed sherds = args[1];
if(sherds.isInstanceOf(CArray.TYPE)) {
CArray sherdArray = (CArray) sherds;
if(sherdArray.isAssociative()) {
for(String key : sherdArray.stringKeySet()) {
MCDecoratedPot.Side side;
try {
side = MCDecoratedPot.Side.valueOf(key.toUpperCase());
} catch (IllegalArgumentException ex) {
throw new CREFormatException("Invalid decorated pot side: " + key, t);
}
try {
decoratedPot.setSherd(side, MCMaterial.valueOf(sherdArray.get(key, t).val()));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(ex.getMessage(), t);
}
}
bs.update();
return CVoid.VOID;
}
}
throw new CREFormatException("Expected associative array for decorated pots.", t);
}
}
}

0 comments on commit bc011de

Please sign in to comment.