Skip to content

Commit

Permalink
feat: enchant table (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd authored Oct 1, 2024
1 parent 5c33e52 commit 6152c5e
Show file tree
Hide file tree
Showing 77 changed files with 1,191 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.allaymc.api.block.interfaces;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.BlockEntityHolderComponent;
import org.allaymc.api.blockentity.interfaces.BlockEntityEnchantTable;

public interface BlockEnchantingTableBehavior extends BlockBehavior {
public interface BlockEnchantingTableBehavior extends
BlockBehavior,
BlockEntityHolderComponent<BlockEntityEnchantTable> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.blockentity.interfaces;

import org.allaymc.api.blockentity.BlockEntity;

/**
* @author daoge_cmd
*/
public interface BlockEntityEnchantTable extends BlockEntity {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public final class BlockEntityTypes {
public static BlockEntityType<BlockEntitySmoker> SMOKER;
public static BlockEntityType<BlockEntitySign> SIGN;
public static BlockEntityType<BlockEntityHangingSign> HANGING_SIGN;
public static BlockEntityType<BlockEntityEnchantTable> ENCHANT_TABLE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public record FullContainerType<T extends Container>(
.mapSlotToType(FurnaceContainer.INGREDIENT_SLOT, ContainerSlotType.SMOKER_INGREDIENT)
.build();

public static final FullContainerType<EnchantTableContainer> ENCHANT_TABLE = builder()
.id(ContainerType.ENCHANTMENT)
.size(2)
.mapSlotToType(EnchantTableContainer.INPUT_SLOT, ContainerSlotType.ENCHANTING_INPUT)
.mapSlotToType(EnchantTableContainer.MATERIAL_SLOT, ContainerSlotType.ENCHANTING_MATERIAL)
.mapRangedNetworkSlotIndex(14, 15, 0)
.build();

public FullContainerType(int id, ContainerSlotType[] slotTypeTable, Set<ContainerSlotType> heldSlotTypes, BiMap<Integer, Integer> networkSlotIndexMapper) {
this.id = id;
this.slotTypeTable = slotTypeTable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.allaymc.api.container.impl;

import org.allaymc.api.container.FullContainerType;
import org.allaymc.api.item.ItemStack;

/**
* @author daoge_cmd
*/
public class EnchantTableContainer extends BlockContainer {
public static final int INPUT_SLOT = 0;
public static final int MATERIAL_SLOT = 1;

public EnchantTableContainer() {
super(FullContainerType.ENCHANT_TABLE);
}

public ItemStack getInput() {
return getItemStack(INPUT_SLOT);
}

public ItemStack getMaterial() {
return getItemStack(MATERIAL_SLOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.jetbrains.annotations.UnmodifiableView;
import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.joml.Vector3ic;

import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

public interface EntityPlayerBaseComponent extends EntityBaseComponent, ChunkLoader, ScoreboardViewer {

Expand Down Expand Up @@ -446,4 +446,25 @@ default double getMaxInteractDistance() {
* @param speed The movement speed to set.
*/
void setMovementSpeed(float speed);
}

/**
* Get the enchantment seed of the player.
*
* @return The enchantment seed of the player.
*/
int getEnchantmentSeed();

/**
* Set the enchantment seed of the player.
*
* @param seed The enchantment seed to set.
*/
void setEnchantmentSeed(int seed);

/**
* Regenerate the enchantment seed of the player.
*/
default void regenerateEnchantmentSeed() {
setEnchantmentSeed(ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ default NbtMap saveNBT() {
*/
boolean hasEnchantment(EnchantmentType enchantmentType);

/**
* Check if the item has any enchantments.
*
* @return {@code true} if the item has enchantments, {@code false} otherwise.
*/
default boolean hasEnchantment() {
return !getEnchantments().isEmpty();
}

/**
* Check if the item has any protection type enchantment.
*
Expand Down Expand Up @@ -430,6 +439,13 @@ default int getEnchantmentProtectionFactor(DamageContainer.DamageType damageType
*/
void addEnchantment(EnchantmentType enchantmentType, int level);

/**
* Add multiple enchantments to the item.
*
* @param enchantmentInstances The enchantment instances that will be added.
*/
void addEnchantments(Collection<EnchantmentInstance> enchantmentInstances);

/**
* Remove an enchantment from the item.
*
Expand Down Expand Up @@ -540,7 +556,7 @@ default boolean checkEnchantmentCompatibility(EnchantmentType type) {
default Set<EnchantmentType> getIncompatibleEnchantmentTypes(EnchantmentType type) {
return getEnchantments().stream()
.map(EnchantmentInstance::getType)
.filter(enchantmentType -> enchantmentType.checkIncompatible(type))
.filter(enchantmentType -> enchantmentType.isIncompatibleWith(type))
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ItemData {
@Builder.Default
protected int armorValue = 0;
@Builder.Default
protected int enchantValue = 0;
@Builder.Default
protected int attackDamage = 0;
@Builder.Default
protected boolean isDamageable = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.inventory.EnchantData;

/**
* Represents an instance of an enchantment.
Expand Down Expand Up @@ -52,4 +53,8 @@ public NbtMap saveNBT() {
.putShort("lvl", (short) getLevel())
.build();
}

public EnchantData toNetwork() {
return new EnchantData(getType().getId(), getLevel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ public int getMaxLevel() {
return maxLevel;
}

/**
* Get the minimum modified level of the enchantment.
*
* @param level The level of the enchantment.
*
* @return The minimum modified level of the enchantment.
* @see <a href="https://minecraft.wiki/w/Enchanting/Levels">Enchanting Levels<a/>.
*/
public int getMinModifiedLevel(int level) {
return 1 + level * 10;
}

/**
* Get the maximum modified level of the enchantment.
*
* @param level The level of the enchantment.
*
* @return The maximum modified level of the enchantment.
* @see <a href="https://minecraft.wiki/w/Enchanting/Levels">Enchanting Levels<a/>.
*/
public int getMaxModifiedLevel(int level) {
return getMinModifiedLevel(level) + 5;
}

/**
* Check if the enchantment is available in enchantment table.
*
* @return {@code true} if the enchantment is available in enchantment table, {@code false} otherwise.
*/
public boolean isAvailableInEnchantTable() {
return true;
}

/**
* Get the rarity of the enchantment.
*
Expand All @@ -78,13 +111,13 @@ public Rarity getRarity() {
}

/**
* Check if the enchantment is compatible with another enchantment.
* Check if the enchantment is incompatible with another enchantment.
*
* @param other The other enchantment.
*
* @return {@code true} if the enchantments are compatible, {@code false} otherwise.
* @return {@code true} if the enchantments are incompatible, {@code false} otherwise.
*/
public boolean checkIncompatible(EnchantmentType other) {
public boolean isIncompatibleWith(EnchantmentType other) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public enum Rarity {
/**
* Converts the weight to the closest rarity using floor semantic.
*
* @param weight The enchantment weight
* @param weight The enchantment weight.
*
* @return The closest rarity
* @return The closest rarity.
*/
public static Rarity fromWeight(int weight) {
if (weight < 2) return VERY_RARE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ public class EnchantmentAquaAffinityType extends EnchantmentType {
public EnchantmentAquaAffinityType() {
super(new Identifier("minecraft:aqua_affinity"), 8, 1, Rarity.RARE);
}
}

@Override
public int getMinModifiedLevel(int level) {
return 1;
}

@Override
public int getMaxModifiedLevel(int level) {
return 41;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ public EnchantmentBaneOfArthropodsType() {
}

@Override
public boolean checkIncompatible(EnchantmentType other) {
public boolean isIncompatibleWith(EnchantmentType other) {
return other instanceof EnchantmentSmiteType ||
other instanceof EnchantmentSharpnessType ||
other instanceof EnchantmentBreachType ||
other instanceof EnchantmentDensityType;
}

@Override
public int getMinModifiedLevel(int level) {
return level * 8 + 3;
}

@Override
public int getMaxModifiedLevel(int level) {
return getMinModifiedLevel(level) + 20;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ public class EnchantmentBindingType extends EnchantmentType {
public EnchantmentBindingType() {
super(new Identifier("minecraft:binding"), 27, 1, Rarity.VERY_RARE);
}

@Override
public boolean isAvailableInEnchantTable() {
return false;
}

@Override
public int getMinModifiedLevel(int level) {
return 25;
}

@Override
public int getMaxModifiedLevel(int level) {
return 50;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public EnchantmentBlastProtectionType() {
}

@Override
public boolean checkIncompatible(EnchantmentType other) {
public boolean isIncompatibleWith(EnchantmentType other) {
return other instanceof EnchantmentFireProtectionType ||
other instanceof EnchantmentProtectionType ||
other instanceof EnchantmentProjectileProtectionType;
Expand All @@ -30,4 +30,14 @@ public int getProtectionFactor(DamageType damageType, int level) {
}
return level * 2;
}

@Override
public int getMinModifiedLevel(int level) {
return level * 8 + 3;
}

@Override
public int getMaxModifiedLevel(int level) {
return getMinModifiedLevel(level) + 12;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ public EnchantmentBreachType() {
}

@Override
public boolean checkIncompatible(EnchantmentType other) {
public boolean isIncompatibleWith(EnchantmentType other) {
return other instanceof EnchantmentDensityType ||
other instanceof EnchantmentBaneOfArthropodsType ||
other instanceof EnchantmentSmiteType;
}

@Override
public int getMinModifiedLevel(int level) {
return level * 9 + 6;
}

@Override
public int getMaxModifiedLevel(int level) {
return getMinModifiedLevel(level) + 50;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ public EnchantmentChannelingType() {
}

@Override
public boolean checkIncompatible(EnchantmentType other) {
public boolean isIncompatibleWith(EnchantmentType other) {
return other instanceof EnchantmentRiptideType;
}

@Override
public int getMinModifiedLevel(int level) {
return 25;
}

@Override
public int getMaxModifiedLevel(int level) {
return 50;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ public EnchantmentDensityType() {
}

@Override
public boolean checkIncompatible(EnchantmentType other) {
public boolean isIncompatibleWith(EnchantmentType other) {
return other instanceof EnchantmentBreachType ||
other instanceof EnchantmentBaneOfArthropodsType ||
other instanceof EnchantmentSmiteType;
}

@Override
public int getMinModifiedLevel(int level) {
return level * 8 - 3;
}

@Override
public int getMaxModifiedLevel(int level) {
return getMinModifiedLevel(level) + 20;
}
}
Loading

0 comments on commit 6152c5e

Please sign in to comment.