Skip to content

Commit

Permalink
Merge branch 'dev' into conditional-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 authored Aug 10, 2024
2 parents e0fc588 + c6f6027 commit 9020ed9
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Player;
import tc.oc.pgm.platform.modern.material.ModernBlockMaterialData;
import tc.oc.pgm.util.bukkit.Effects;
import tc.oc.pgm.util.material.BlockMaterialData;
import tc.oc.pgm.util.platform.Supports;

@Supports(value = PAPER, minVersion = "1.20.6")
Expand Down Expand Up @@ -69,4 +72,11 @@ public void spawnFlame(World world, Location location) {
public void explosion(Player player, Location location) {
player.spawnParticle(Particle.EXPLOSION, location, 1, 0d, 0d, 0d, 0, null, true);
}

@Override
public void blockBreak(Location location, BlockMaterialData material) {
location
.getWorld()
.playEffect(location, Effect.STEP_SOUND, ((ModernBlockMaterialData) material).getBlock());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tc.oc.pgm.util.material.MaterialMatcher;

public interface ModernBlockMaterialData extends BlockMaterialData {

BlockData getBlock();

@Override
Expand All @@ -30,7 +31,7 @@ default void sendBlockChange(Player player, Location location) {

@Override
default int encoded() {
return getItemType().ordinal();
return ModernEncodeUtil.encode(getBlock());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tc.oc.pgm.platform.modern.material;

import net.minecraft.world.level.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.block.data.CraftBlockData;

class ModernEncodeUtil {
private static final int ENCODED_NULL_MATERIAL = -1;

static int encode(BlockData blockData) {
return Block.BLOCK_STATE_REGISTRY.getId(((CraftBlockData) blockData).getState());
}

static ModernBlockData decode(int encoded) {
if (encoded == ENCODED_NULL_MATERIAL) return null;
var vanillaBlockstate = Block.BLOCK_STATE_REGISTRY.byId(encoded);
if (vanillaBlockstate == null) return null;
return new ModernBlockData(vanillaBlockstate.createCraftBlockData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
@Supports(value = PAPER, minVersion = "1.20.6")
@SuppressWarnings("deprecation")
public class ModernMaterialUtils implements MaterialUtils {
private static final int ENCODED_NULL_MATERIAL = -1;

@Override
public BlockMaterialData createBlockData(Material material) {
Expand Down Expand Up @@ -83,8 +82,7 @@ public ItemMaterialData createItemData(Hanging hanging) {

@Override
public BlockMaterialData decode(int encoded) {
if (encoded == ENCODED_NULL_MATERIAL) return null;
return new ModernBlockData(Material.values()[encoded].createBlockData());
return ModernEncodeUtil.decode(encoded);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.World;
import org.bukkit.entity.Player;
import tc.oc.pgm.util.bukkit.Effects;
import tc.oc.pgm.util.material.BlockMaterialData;
import tc.oc.pgm.util.platform.Supports;

@Supports(SPORTPAPER)
Expand Down Expand Up @@ -98,6 +99,11 @@ public void explosion(Player player, Location location) {
player.spigot().playEffect(location, Effect.EXPLOSION_HUGE, 0, 0, 0f, 0f, 0f, 1f, 1, 256);
}

@Override
public void blockBreak(Location location, BlockMaterialData material) {
location.getWorld().playEffect(location, Effect.STEP_SOUND, material.encoded());
}

private float rgbToParticle(int rgb) {
return Math.max(0.001f, (rgb / 255.0f));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public BlockVector getBlockVector() {

@Override
public int encoded() {
throw new UnsupportedOperationException();
return SpEncodeUtil.encode(materialId, data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tc.oc.pgm.platform.sportpaper.material;

import org.bukkit.Material;

@SuppressWarnings("deprecation")
class SpEncodeUtil {
private static final int ENCODED_NULL_MATERIAL = -1;

static int encode(Material mat, int data) {
return encode(mat.getId(), data);
}

static int encode(int typeId, int data) {
return typeId + (data << 12);
}

static SpMaterialData decode(int encoded) {
if (encoded == ENCODED_NULL_MATERIAL) return null;
return new SpMaterialData(decodeMaterial(encoded), decodeData(encoded));
}

static Material decodeMaterial(int encoded) {
return Material.getMaterial(encoded & 0xfff);
}

static byte decodeData(int encoded) {
return (byte) (encoded >> 12);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void sendBlockChange(Player player, Location location) {

@Override
public int encoded() {
return material.getId() + (((int) damage) << 12);
return SpEncodeUtil.encode(material, damage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,7 @@ public ItemMaterialData createItemData(Hanging hanging) {

@Override
public BlockMaterialData decode(int encoded) {
if (encoded == ENCODED_NULL_MATERIAL) return null;
Material material = Material.getMaterial(decodeTypeId(encoded));
return new SpMaterialData(material, decodeMetadata(encoded));
}

static int decodeTypeId(int encoded) {
return encoded & 0xfff;
}

static byte decodeMetadata(int encoded) {
return (byte) (encoded >> 12);
return SpEncodeUtil.decode(encoded);
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions util/src/main/java/tc/oc/pgm/util/bukkit/Effects.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
Expand All @@ -26,7 +25,5 @@ public interface Effects {

void explosion(Player player, Location location);

default void blockBreak(Location location, BlockMaterialData material) {
location.getWorld().playEffect(location, Effect.STEP_SOUND, material.encoded());
}
void blockBreak(Location location, BlockMaterialData material);
}
22 changes: 5 additions & 17 deletions util/src/main/java/tc/oc/pgm/util/material/MaterialCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,24 @@
import java.util.Iterator;
import org.bukkit.block.BlockState;

/** Efficiently counts distinct {@link MaterialData}s */
/** Efficiently counts distinct {@link BlockMaterialData}s */
public class MaterialCounter {
private final Int2IntMap counts;

public MaterialCounter() {
this.counts = new Int2IntOpenHashMap();
}

public boolean contains(int encodedMaterial) {
return counts.containsKey(encodedMaterial);
}

public boolean contains(BlockMaterialData material) {
return contains(material.encoded());
}

public int get(int encodedMaterial) {
return counts.get(encodedMaterial);
return counts.containsKey(material.encoded());
}

public int get(BlockMaterialData material) {
return get(material.encoded());
}

public int increment(int encodedMaterial, int count) {
return counts.merge(encodedMaterial, count, (key, v) -> v + count);
return counts.get(material.encoded());
}

public int increment(BlockState block, int count) {
return increment(MaterialData.block(block).encoded(), count);
public void increment(BlockState block, int count) {
counts.merge(MaterialData.block(block).encoded(), count, (key, v) -> v + count);
}

public void clear() {
Expand Down

0 comments on commit 9020ed9

Please sign in to comment.