Skip to content

Commit

Permalink
Enchantment table persists lapis (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
benkerllenevich authored Jun 25, 2022
1 parent 8f539f9 commit a361032
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions patches/server/0279-Enchantment-Table-Persists-Lapis.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ben Kerllenevich <ben@omega24.dev>
Date: Sat, 25 Jun 2022 08:04:06 -0400
Subject: [PATCH] Enchantment Table Persists Lapis


diff --git a/src/main/java/net/minecraft/world/inventory/EnchantmentMenu.java b/src/main/java/net/minecraft/world/inventory/EnchantmentMenu.java
index f7404f41c2df202a428711e7e09a9a6eb11bf527..d0baf170d0b535b4b1a8c48944a91de333127dc1 100644
--- a/src/main/java/net/minecraft/world/inventory/EnchantmentMenu.java
+++ b/src/main/java/net/minecraft/world/inventory/EnchantmentMenu.java
@@ -38,6 +38,12 @@ import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
import org.bukkit.entity.Player;
// CraftBukkit end

+// Purpur start
+import net.minecraft.world.level.block.entity.BlockEntity;
+import net.minecraft.world.level.block.entity.EnchantmentTableBlockEntity;
+import org.bukkit.craftbukkit.entity.CraftHumanEntity;
+// Purpur end
+
public class EnchantmentMenu extends AbstractContainerMenu {

private final Container enchantSlots;
@@ -71,6 +77,22 @@ public class EnchantmentMenu extends AbstractContainerMenu {
return context.getLocation();
}
// CraftBukkit end
+
+ // Purpur start
+ @Override
+ public void onClose(CraftHumanEntity who) {
+ super.onClose(who);
+
+ if (who.getHandle().getLevel().purpurConfig.enchantmentTableLapisPersists) {
+ access.execute((level, pos) -> {
+ BlockEntity blockEntity = level.getBlockEntity(pos);
+ if (blockEntity instanceof EnchantmentTableBlockEntity enchantmentTable) {
+ enchantmentTable.setLapis(this.getItem(1).getCount());
+ }
+ });
+ }
+ }
+ // Purpur end
};
this.random = RandomSource.create();
this.enchantmentSeed = DataSlot.standalone();
@@ -96,6 +118,17 @@ public class EnchantmentMenu extends AbstractContainerMenu {
}
});

+ // Purpur start
+ access.execute((level, pos) -> {
+ if (level.purpurConfig.enchantmentTableLapisPersists) {
+ BlockEntity blockEntity = level.getBlockEntity(pos);
+ if (blockEntity instanceof EnchantmentTableBlockEntity enchantmentTable) {
+ this.getSlot(1).set(new ItemStack(Items.LAPIS_LAZULI, enchantmentTable.getLapis()));
+ }
+ }
+ });
+ // Purpur end
+
int j;

for (j = 0; j < 3; ++j) {
@@ -338,6 +371,7 @@ public class EnchantmentMenu extends AbstractContainerMenu {
public void removed(net.minecraft.world.entity.player.Player player) {
super.removed(player);
this.access.execute((world, blockposition) -> {
+ if (world.purpurConfig.enchantmentTableLapisPersists) this.getSlot(1).set(ItemStack.EMPTY); // Purpur
this.clearContainer(player, this.enchantSlots);
});
}
diff --git a/src/main/java/net/minecraft/world/level/block/EnchantmentTableBlock.java b/src/main/java/net/minecraft/world/level/block/EnchantmentTableBlock.java
index f4ee3ce287528337a0f9a3b612c157254f895a58..37a888e5db65b927094b43775ae9d4098244f809 100644
--- a/src/main/java/net/minecraft/world/level/block/EnchantmentTableBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/EnchantmentTableBlock.java
@@ -28,6 +28,8 @@ import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
+import net.minecraft.world.Containers; // Purpur
+import net.minecraft.world.item.Items; // Purpur

public class EnchantmentTableBlock extends BaseEntityBlock {
protected static final VoxelShape SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 12.0D, 16.0D);
@@ -120,4 +122,18 @@ public class EnchantmentTableBlock extends BaseEntityBlock {
public boolean isPathfindable(BlockState state, BlockGetter world, BlockPos pos, PathComputationType type) {
return false;
}
+
+ // Purpur start
+ @Override
+ public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean moved) {
+ BlockEntity blockEntity = level.getBlockEntity(pos);
+
+ if (level.purpurConfig.enchantmentTableLapisPersists && blockEntity instanceof EnchantmentTableBlockEntity enchantmentTable) {
+ Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(Items.LAPIS_LAZULI, enchantmentTable.getLapis()));
+ level.updateNeighbourForOutputSignal(pos, this);
+ }
+
+ super.onRemove(state, level, pos, newState, moved);
+ }
+ // Purpur end
}
diff --git a/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
index 2341a5a249d455628165fc6ba508fc6d70c3dbfb..4ccb8a7dc4201a7cffa59e419576500189042ac6 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
@@ -24,6 +24,7 @@ public class EnchantmentTableBlockEntity extends BlockEntity implements Nameable
public float tRot;
private static final RandomSource RANDOM = RandomSource.create();
private Component name;
+ private int lapis = 0; // Purpur

public EnchantmentTableBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntityType.ENCHANTING_TABLE, pos, state);
@@ -35,6 +36,7 @@ public class EnchantmentTableBlockEntity extends BlockEntity implements Nameable
if (this.hasCustomName()) {
nbt.putString("CustomName", Component.Serializer.toJson(this.name));
}
+ nbt.putInt("Purpur.Lapis", this.lapis); // Purpur

}

@@ -44,6 +46,7 @@ public class EnchantmentTableBlockEntity extends BlockEntity implements Nameable
if (nbt.contains("CustomName", 8)) {
this.name = net.minecraft.server.MCUtil.getBaseComponentFromNbt("CustomName", nbt); // Paper - Catch ParseException
}
+ this.lapis = nbt.getInt("Purpur.Lapis"); // Purpur

}

@@ -117,4 +120,14 @@ public class EnchantmentTableBlockEntity extends BlockEntity implements Nameable
public Component getCustomName() {
return this.name;
}
+
+ // Purpur start
+ public int getLapis() {
+ return this.lapis;
+ }
+
+ public void setLapis(int lapis) {
+ this.lapis = lapis;
+ }
+ // Purpur
}
diff --git a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java
index e8cc15345734ee1f0d7e452f2a90ddd9e57951ba..6a5572714916937eff21c1b984a2a2f00c561cbe 100644
--- a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java
+++ b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java
@@ -1423,6 +1423,11 @@ public class PurpurWorldConfig {
elderGuardianAlwaysDropExp = getBoolean("mobs.elder_guardian.always-drop-exp", elderGuardianAlwaysDropExp);
}

+ public boolean enchantmentTableLapisPersists = false;
+ private void enchantmentTableSettings() {
+ enchantmentTableLapisPersists = getBoolean("blocks.enchantment-table.lapis-persists", enchantmentTableLapisPersists);
+ }
+
public boolean enderDragonRidable = false;
public boolean enderDragonRidableInWater = false;
public boolean enderDragonControllable = true;

0 comments on commit a361032

Please sign in to comment.