Skip to content

Commit

Permalink
commit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m1919810 committed Dec 29, 2024
1 parent 6664da3 commit 5cf0268
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ public SlimefunBlockData getBlockData(Location l) {
return getBlockDataFromCache(l);
}

var chunk = l.getChunk();
var chunkData = getChunkDataCache(chunk, false);
//var chunk = l.getChunk();
//fix issue #935
var chunkData = getChunkDataCache(l, false);
var lKey = LocationUtils.getLocKey(l);
if (chunkData != null) {
var re = chunkData.getBlockCacheInternal(lKey);
Expand All @@ -238,7 +239,8 @@ public SlimefunBlockData getBlockData(Location l) {
var re =
result.isEmpty() ? null : new SlimefunBlockData(l, result.get(0).get(FieldKey.SLIMEFUN_ID));
if (re != null) {
chunkData = getChunkDataCache(chunk, true);
//fix issue #935
chunkData = getChunkDataCache(l, true);
chunkData.addBlockCacheInternal(re, false);
re = chunkData.getBlockCacheInternal(lKey);
}
Expand All @@ -262,7 +264,7 @@ public void getBlockDataAsync(Location l, IAsyncReadCallback<SlimefunBlockData>
* @return {@link SlimefunBlockData}
*/
public SlimefunBlockData getBlockDataFromCache(Location l) {
return getBlockDataFromCache(LocationUtils.getChunkKey(l.getChunk()), LocationUtils.getLocKey(l));
return getBlockDataFromCache(LocationUtils.getChunkKey(l), LocationUtils.getLocKey(l));
}

/**
Expand Down Expand Up @@ -745,6 +747,16 @@ private SlimefunChunkData getChunkDataCache(Chunk chunk, boolean createOnNotExis
})
: loadedChunk.get(LocationUtils.getChunkKey(chunk));
}
//fix issue 935: auto chunk load when using loc.getChunk(),if chunk data is already loaded into cache, we generate keyString using location,instead of loc.getChunk
private SlimefunChunkData getChunkDataCache(Location loc, boolean createOnNotExists) {
var re= loadedChunk.get(LocationUtils.getChunkKey(loc));
if(re!=null){
return re;
}else{
//这里不要检测createOnNotExist 容易出事
return getChunkDataCache(loc.getChunk(),createOnNotExists);
}
}

private void deleteChunkAndBlockDataDirectly(String cKey) {
var req = new RecordKey(DataScope.BLOCK_RECORD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static String getLocKey(Location l) {
public static String getChunkKey(Chunk chunk) {
return chunk.getWorld().getName() + ";" + chunk.getX() + ":" + chunk.getZ();
}
public static String getChunkKey(Location loc) {
return loc.getWorld().getName() + ";" + (loc.getBlockX()>>4) + ":" + (loc.getBlockZ()>>4);
}

public static Location toLocation(String lKey) {
var strArr = lKey.split(";");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ public class SlimefunItemStack extends ItemStack {
private boolean locked = false;
private String texture = null;

public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item) {
public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item, @Nonnull Consumer<ItemMeta> consumer) {
super(item.getType(), item.getAmount());

if (item.hasItemMeta()) {
setItemMeta(item.getItemMeta());
}

Validate.notNull(id, "The Item id must never be null!");
Validate.isTrue(
id.equals(id.toUpperCase(Locale.ROOT)), "Slimefun Item Ids must be uppercase! (e.g. 'MY_ITEM_ID')");
Expand All @@ -64,20 +60,17 @@ public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item) {

this.id = id;

ItemMeta meta = getItemMeta();
ItemMeta meta = item.hasItemMeta()?item.getItemMeta(): getItemMeta();

Slimefun.getItemDataService().setItemData(meta, id);
Slimefun.getItemTextureService().setTexture(meta, id);
consumer.accept(meta);

setItemMeta(meta);
}

public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item, @Nonnull Consumer<ItemMeta> consumer) {
this(id, item);

ItemMeta im = getItemMeta();
consumer.accept(im);
setItemMeta(im);
private static final Consumer<ItemMeta> DEFAULT_CONSUMER=(itemMeta -> {});
public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item) {
this(id, item,DEFAULT_CONSUMER);
}

public SlimefunItemStack(@Nonnull String id, @Nonnull Material type, @Nonnull Consumer<ItemMeta> consumer) {
Expand Down Expand Up @@ -316,10 +309,15 @@ public void lock() {
"The provided texture for Item \"" + id + "\" does not seem to be a valid texture String!");
}
}

//fix issue 988 : addon menu can't open propertly due to cast error in 1.21+paper
@Override
public ItemStack clone() {
return new SlimefunItemStack(id, this);
//return new SlimefunItemStack(id, this);
ItemStack stack=super.clone();
if(stack instanceof SlimefunItemStack sfitem){
sfitem.locked=false;
}
return stack;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.attributes.DistinctiveItem;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.EntityType;
Expand All @@ -29,7 +32,7 @@
* @see RepairedSpawner
*
*/
public abstract class AbstractMonsterSpawner extends SlimefunItem {
public abstract class AbstractMonsterSpawner extends SlimefunItem implements DistinctiveItem {

@ParametersAreNonnullByDefault
AbstractMonsterSpawner(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
Expand Down Expand Up @@ -65,6 +68,13 @@ public Optional<EntityType> getEntityType(@Nonnull ItemStack item) {
return Optional.of(type);
}
}
if(meta instanceof BlockStateMeta blockStateMeta){
if(blockStateMeta.hasBlockState()&& blockStateMeta.getBlockState() instanceof CreatureSpawner spawner){
EntityType type=spawner.getSpawnedType();
if(type!=null)
return Optional.of(type);
}
}

return Optional.empty();
}
Expand All @@ -80,38 +90,53 @@ public Optional<EntityType> getEntityType(@Nonnull ItemStack item) {
* @return An {@link ItemStack} for this {@link SlimefunItem} holding that {@link EntityType}
*/
@Nonnull
public ItemStack getItemForEntityType(@Nonnull EntityType type) {
Validate.notNull(type, "The EntityType cannot be null");
public ItemStack getItemForEntityType(@Nullable EntityType type) {
//Validate.notNull(type, "The EntityType cannot be null");

ItemStack item = getItem().clone();
ItemMeta meta = item.getItemMeta();
//fix: you can't set null type or a not-spawnable type, for example ,player
if(type!=null&&type.isSpawnable()) {

// Fixes #2583 - Proper NBT handling of Spawners
if (meta instanceof BlockStateMeta stateMeta) {
BlockState state = stateMeta.getBlockState();
// Fixes #2583 - Proper NBT handling of Spawners
if (meta instanceof BlockStateMeta stateMeta) {
BlockState state = stateMeta.getBlockState();

if (state instanceof CreatureSpawner spawner) {
spawner.setSpawnedType(type);
}
if (state instanceof CreatureSpawner spawner) {
spawner.setSpawnedType(type);
}

stateMeta.setBlockState(state);
stateMeta.setBlockState(state);
}
}

// Setting the lore to indicate the Type visually
List<String> lore = meta.getLore();

for (int i = 0; i < lore.size(); i++) {
String currentLine = lore.get(i);
if (currentLine.contains("<Type>") || currentLine.contains("<类型>")) {
String typeName = ChatUtils.humanize(type.name());
String typeName = type==null?"空" :ChatUtils.humanize(type.name());
lore.set(i, currentLine.replace("<Type>", typeName).replace("<类型>", typeName));
break;
}
}

meta.setLore(lore);
item.setItemMeta(meta);

return item;
}
//to fix the bug of stacking two BROKEN_SPAWNER/REINFORCED_SPAWNER containing different EntityType using cargo or machine
public boolean canStack(@Nonnull ItemMeta itemMetaOne, @Nonnull ItemMeta itemMetaTwo){
if(itemMetaOne instanceof BlockStateMeta blockStateMeta1&& itemMetaTwo instanceof BlockStateMeta blockStateMeta2){
if(blockStateMeta1.hasBlockState()&&blockStateMeta2.hasBlockState()){
//BlockState.equals do not compare these data
if(blockStateMeta1.getBlockState() instanceof CreatureSpawner spawner1&& blockStateMeta2.getBlockState() instanceof CreatureSpawner spawner2){
return spawner1.getSpawnedType()==spawner2.getSpawnedType();
}
}else{
return blockStateMeta1.hasBlockState()==blockStateMeta2.hasBlockState();
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.util.StorageCacheUtils;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.handlers.ToolUseHandler;
Expand All @@ -14,6 +15,7 @@
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.papermc.lib.PaperLib;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -48,26 +50,32 @@ public PickaxeOfContainment(

if (b.getType() == Material.SPAWNER) {
ItemStack spawner = breakSpawner(b);
SlimefunUtils.spawnItem(
b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true, e.getPlayer());
if(spawner!=null){
SlimefunUtils.spawnItem(
b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true, e.getPlayer());

e.setExpToDrop(0);
e.setDropItems(false);
e.setExpToDrop(0);
e.setDropItems(false);
}
}
};
}

private @Nonnull ItemStack breakSpawner(@Nonnull Block b) {
private @Nullable ItemStack breakSpawner(@Nonnull Block b) {
AbstractMonsterSpawner spawner;

/*
If the spawner's BlockStorage has BlockInfo, then it's not a vanilla spawner
and should not give a broken spawner but a repaired one instead.
*/
if (StorageCacheUtils.hasBlock(b.getLocation())) {
SlimefunItem item=StorageCacheUtils.getSfItem(b.getLocation());
if (item instanceof RepairedSpawner) {
spawner = (AbstractMonsterSpawner) SlimefunItems.REPAIRED_SPAWNER.getItem();
} else {
} else if (item ==null) {
spawner = (AbstractMonsterSpawner) SlimefunItems.BROKEN_SPAWNER.getItem();
}else{
//do not drop anything when mining other addon's spawner-material machine
return null;
}

BlockState state = PaperLib.getBlockState(b, false).getState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public SmeltersPickaxe(ItemGroup itemGroup, SlimefunItemStack item, RecipeType r
drops.add(drop);
}
}

//fix issue #967
e.setDropItems(false);
damageItem(e.getPlayer(), tool);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import io.github.thebusybiscuit.slimefun4.implementation.items.backpacks.Cooler;
import io.github.thebusybiscuit.slimefun4.implementation.items.backpacks.SlimefunBackpack;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -183,18 +186,19 @@ private void openBackpack(Player p, ItemStack item, PlayerProfile profile, int s
// Check if someone else is currently viewing this backpack
if (!backpacks.containsValue(item)) {
SoundEffect.BACKPACK_OPEN_SOUND.playAt(p.getLocation(), SoundCategory.PLAYERS);

PlayerBackpack.getAsync(
item,
backpack -> {
//fix the issue #978 dupe with fast-click backpack
backpack.open(p);
backpacks.put(p.getUniqueId(), item);
invSnapshot.put(
backpack.getUniqueId(),
InvStorageUtils.getInvSnapshot(
backpack.getInventory().getContents()));
backpack.open(p);
},
true);

} else {
Slimefun.getLocalization().sendMessage(p, "backpack.already-open", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public void onDamage(PlayerDeathEvent e) {
}

// There shouldn't even be any items in there, but let's be extra safe!
Map<Integer, ItemStack> existingItems = soulbound.get(p.getUniqueId());
// Map<Integer, ItemStack> existingItems = soulbound.get(p.getUniqueId());
//fix issue #964 dupe using soulbound
//
// if (existingItems == null) {
// } else {
// existingItems.putAll(items);
// }
soulbound.put(p.getUniqueId(), items);

if (existingItems == null) {
soulbound.put(p.getUniqueId(), items);
} else {
existingItems.putAll(items);
}

// Remove soulbound items from our drops
e.getDrops().removeIf(itemStack -> SlimefunUtils.isSoulbound(itemStack, p.getWorld()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.ItemStack;

/**
Expand Down Expand Up @@ -38,4 +39,15 @@ public void onAnvil(InventoryClickEvent e) {
}
}
}
@EventHandler(ignoreCancelled = true)
public void onAnvilCraft(PrepareAnvilEvent e){
//fix issue #958
if(e.getInventory().getType() == InventoryType.ANVIL&&e.getInventory().getSize()>=2){
ItemStack item1 = e.getInventory().getContents()[0];
ItemStack item2 = e.getInventory().getContents()[1];
if (hasUnallowedItems(item1, item2)) {
e.setResult(null);
}
}
}
}
Loading

0 comments on commit 5cf0268

Please sign in to comment.