Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement block states for commands and /testforblock command #579

Closed
wants to merge 9 commits into from
3 changes: 2 additions & 1 deletion src/main/java/net/glowstone/GlowServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.glowstone.advancement.GlowAdvancementDisplay;
import net.glowstone.block.BuiltinMaterialValueManager;
import net.glowstone.block.MaterialValueManager;
import net.glowstone.block.state.GlowDispenser;
import net.glowstone.block.entity.state.GlowDispenser;
import net.glowstone.boss.BossBarManager;
import net.glowstone.boss.GlowBossBar;
import net.glowstone.client.GlowClient;
Expand Down Expand Up @@ -861,6 +861,7 @@ private void loadPlugins() {
commandMap.register("minecraft", new EffectCommand());
commandMap.register("minecraft", new EnchantCommand());
commandMap.register("minecraft", new TestForCommand());
commandMap.register("minecraft", new TestForBlockCommand());

File folder = new File(config.getString(Key.PLUGIN_FOLDER));
if (!folder.isDirectory() && !folder.mkdirs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.entity.BannerEntity;
import net.glowstone.block.state.GlowBanner;
import net.glowstone.block.entity.state.GlowBanner;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.util.nbt.CompoundTag;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/blocktype/BlockBed.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.entity.BedEntity;
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.state.GlowBed;
import net.glowstone.block.entity.state.GlowBed;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.DyeColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.entity.DispenserEntity;
import net.glowstone.block.state.GlowDispenser;
import net.glowstone.block.entity.state.GlowDispenser;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.inventory.MaterialMatcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.entity.FlowerPotEntity;
import net.glowstone.block.state.GlowFlowerPot;
import net.glowstone.block.entity.state.GlowFlowerPot;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.GrassSpecies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.entity.ContainerEntity;
import net.glowstone.block.entity.HopperEntity;
import net.glowstone.block.state.GlowHopper;
import net.glowstone.block.entity.state.GlowHopper;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.entity.objects.GlowItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.glowstone.block.GlowBlock;
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.entity.JukeboxEntity;
import net.glowstone.block.state.GlowJukebox;
import net.glowstone.block.entity.state.GlowJukebox;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.GameMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.entity.SkullEntity;
import net.glowstone.block.entity.BlockEntity;
import net.glowstone.block.state.GlowSkull;
import net.glowstone.block.entity.state.GlowSkull;
import net.glowstone.chunk.GlowChunk;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.util.pattern.BlockPattern;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/entity/BannerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.blocktype.BlockBanner;
import net.glowstone.block.state.GlowBanner;
import net.glowstone.block.entity.state.GlowBanner;
import net.glowstone.constants.GlowBlockEntity;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.util.nbt.CompoundTag;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/entity/BedEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Setter;
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowBed;
import net.glowstone.block.entity.state.GlowBed;
import net.glowstone.constants.GlowBlockEntity;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.util.nbt.CompoundTag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowBrewingStand;
import net.glowstone.block.entity.state.GlowBrewingStand;
import net.glowstone.inventory.GlowBrewerInventory;
import net.glowstone.util.nbt.CompoundTag;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/entity/ChestEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowChest;
import net.glowstone.block.entity.state.GlowChest;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.inventory.GlowChestInventory;
import net.glowstone.net.message.play.game.BlockActionMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowDispenser;
import net.glowstone.block.entity.state.GlowDispenser;
import net.glowstone.inventory.GlowInventory;
import org.bukkit.event.inventory.InventoryType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowDropper;
import net.glowstone.block.entity.state.GlowDropper;

public class DropperEntity extends DispenserEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowFlowerPot;
import net.glowstone.block.entity.state.GlowFlowerPot;
import net.glowstone.constants.GlowBlockEntity;
import net.glowstone.constants.ItemIds;
import net.glowstone.entity.GlowPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.glowstone.GlowServer;
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowFurnace;
import net.glowstone.block.entity.state.GlowFurnace;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.inventory.GlowFurnaceInventory;
import net.glowstone.inventory.crafting.CraftingManager;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/entity/HopperEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowHopper;
import net.glowstone.block.entity.state.GlowHopper;
import net.glowstone.inventory.GlowInventory;
import org.bukkit.event.inventory.InventoryType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowJukebox;
import net.glowstone.block.entity.state.GlowJukebox;
import net.glowstone.io.nbt.NbtSerialization;
import net.glowstone.util.nbt.CompoundTag;
import org.bukkit.inventory.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowCreatureSpawner;
import net.glowstone.block.entity.state.GlowCreatureSpawner;
import net.glowstone.util.nbt.CompoundTag;
import org.bukkit.entity.EntityType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowNoteBlock;
import net.glowstone.block.entity.state.GlowNoteBlock;
import net.glowstone.util.nbt.CompoundTag;
import org.bukkit.Note;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/entity/SignEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowSign;
import net.glowstone.block.entity.state.GlowSign;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.util.TextMessage;
import net.glowstone.util.nbt.CompoundTag;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/glowstone/block/entity/SkullEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.blocktype.BlockSkull;
import net.glowstone.block.state.GlowSkull;
import net.glowstone.block.entity.state.GlowSkull;
import net.glowstone.constants.GlowBlockEntity;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.entity.meta.profile.PlayerProfile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.entity.BeaconEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.entity.BrewingStandEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.GlowServer;
import net.glowstone.block.GlowBlock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import com.destroystokyo.paper.loottable.LootableBlockInventory;
import net.glowstone.block.GlowBlock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.entity.DispenserEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import org.bukkit.block.Dropper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.entity.FurnaceEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.entity.HopperEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.EventFactory;
import net.glowstone.block.GlowBlock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.glowstone.block.state;
package net.glowstone.block.entity.state;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/net/glowstone/block/state/BlockStateData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.glowstone.block.state;

import com.google.common.collect.Maps;

import java.util.Map;

public class BlockStateData {

private final Map<String, String> map = Maps.newHashMap();
private final int numericValue;

public BlockStateData(int numericValue) {
this.numericValue = numericValue;
}

public BlockStateData() {
this.numericValue = -1;
}

public boolean isEmpty() {
return map.isEmpty();
}

public void put(String key, String value) {
map.put(key, value);
}

public boolean contains(String key) {
return map.containsKey(key);
}

public String get(String key) {
return map.get(key);
}

public boolean isNumeric() {
return numericValue != -1;
}

public int getNumericValue() {
return numericValue;
}

@Override
public String toString() {
if (isNumeric()) {
return String.valueOf(numericValue);
}
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.append(",").append(entry.getKey()).append("=").append(entry.getValue());
}
return builder.length() == 0 ? builder.toString() : builder.substring(1);
}
}
15 changes: 15 additions & 0 deletions src/main/java/net/glowstone/block/state/BlockStateReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.glowstone.block.state;

import org.bukkit.Material;
import org.bukkit.material.MaterialData;

import java.util.Set;

public abstract class BlockStateReader<T extends MaterialData> {

public abstract Set<String> getValidStates();

public abstract T read(Material material, BlockStateData data) throws InvalidBlockStateException;

public abstract boolean matches(BlockStateData state, T data) throws InvalidBlockStateException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like sticking this on the reader is a bit disjoint with how I implemented the Entity's tag matching. I'm not opposed to sticking it on the reader, but and if you want to keep it like this you should think about changing how the CompoundTag's matching works. Otherwise, I'd say you should implement the match method on the BlockStateData, similar to how CompoundTag implements its match method.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.glowstone.block.state;

import net.glowstone.constants.ItemIds;
import org.bukkit.Material;

public class InvalidBlockStateException extends Exception {
public InvalidBlockStateException(Material material, String state) {
super("'" + state + "' is not a state for block " + ItemIds.getName(material));
}

public InvalidBlockStateException(Material material, BlockStateData state) {
super("'" + state.toString() + "' is not a state for block " + ItemIds.getName(material));
}
}
Loading