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
14 changes: 14 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,14 @@
package net.glowstone.block.state;

import java.util.HashMap;

public class BlockStateData extends HashMap<String, String> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Extending an implementation of Map<> seems like the wrong way to go here. In my experience, this makes it harder to refactor, especially if another implementation of map is needed, and can dilute the reason for having BlockStateData vs a regular map. It's convenient, yes, but often introduces a design smell. I usually prefer introducing the map as a field and exposing only the relevant delegate methods, making it clear what is going on.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good points, I will change this.

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (Entry<String, String> entry : 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