Skip to content

Commit

Permalink
Implements #65
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Dec 11, 2022
1 parent a874f15 commit ae4ed14
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.potion.PotionUtil;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.samo_lego.golfiv.casts.ItemStackChecker;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand Down Expand Up @@ -45,6 +45,8 @@ public abstract class ItemStackMixinCast_ItemStackChecker implements ItemStackCh
@Shadow
public abstract Item getItem();

@Shadow
private @Nullable NbtCompound nbt;
private final ItemStack itemStack = (ItemStack) (Object) this;

/**
Expand All @@ -58,55 +60,49 @@ public void makeLegal(boolean survival) {
Map<Enchantment, Integer> enchantments = EnchantmentHelper.fromNbt(this.getEnchantments());

// Checks item enchantments
if((survival && golfConfig.items.survival.checkEnchants) || (!survival && golfConfig.items.creative.checkEnchants))
for(Map.Entry<Enchantment, Integer> ench : enchantments.entrySet()) {
if ((survival && golfConfig.items.survival.checkEnchants) || (!survival && golfConfig.items.creative.checkEnchants))
for (Map.Entry<Enchantment, Integer> ench : enchantments.entrySet()) {
Enchantment enchantment = ench.getKey();
int level = ench.getValue();

Set<Enchantment> otherEnchants = EnchantmentHelper.get(this.itemStack).keySet();
otherEnchants.remove(enchantment);

if(!enchantment.isAcceptableItem(this.itemStack) || !EnchantmentHelper.isCompatible(otherEnchants, enchantment) || level > enchantment.getMaxLevel()) {
if (!enchantment.isAcceptableItem(this.itemStack) ||
!EnchantmentHelper.isCompatible(otherEnchants, enchantment) ||
level > enchantment.getMaxLevel()) {
this.removeSubNbt("Enchantments");
break;
}
}

// Checks potion
if(
(
(survival && golfConfig.items.survival.checkPotionLevels) ||
(!survival && golfConfig.items.creative.checkPotionLevels)
) &&
(
this.itemStack.getItem() == Items.POTION ||
this.itemStack.getItem() == Items.SPLASH_POTION ||
this.itemStack.getItem() == Items.LINGERING_POTION
)
) {
if (((survival && golfConfig.items.survival.checkPotionLevels) || (!survival && golfConfig.items.creative.checkPotionLevels)) &&
(this.itemStack.getItem() == Items.POTION ||
this.itemStack.getItem() == Items.SPLASH_POTION ||
this.itemStack.getItem() == Items.LINGERING_POTION)) {
List<StatusEffectInstance> effects = PotionUtil.getCustomPotionEffects(this.itemStack);
for(StatusEffectInstance effect : effects) {
if(effect.getAmplifier() > 1) {

for (StatusEffectInstance effect : effects) {
if (effect.getAmplifier() > 1) {
this.removeSubNbt("CustomPotionEffects");
this.removeSubNbt("Potion");
break;
}
}
}

Identifier id = Registries.ITEM.getId(this.itemStack.getItem());
if(survival && (golfConfig.items.survival.bannedItems.contains(id.toString()) || (golfConfig.items.survival.bannedItems.contains("minecraft:spawn_egg") && this.itemStack.getItem() instanceof SpawnEggItem))) {
String nbt = golfConfig.items.survival.bannedItems.get(this.itemStack.getItem());
NbtCompound tag = this.itemStack.getNbt();

if (survival && nbt != null && (nbt.isEmpty() || tag != null && tag.toString().equals(nbt)) ||
(golfConfig.items.survival.banSpawnEggs && this.itemStack.getItem() instanceof SpawnEggItem)) {
this.setCount(0);
}
} else if (((survival && golfConfig.items.survival.checkItemCount) ||
(!survival && golfConfig.items.creative.checkItemCount)) &&
this.count > this.getMaxCount()) {

if(
(
(survival && golfConfig.items.survival.checkItemCount) ||
(!survival && golfConfig.items.creative.checkItemCount)
) &&
this.count > this.getMaxCount()
) {
this.count = 1;
this.count = this.getMaxCount();
}
}
}
58 changes: 50 additions & 8 deletions src/main/java/org/samo_lego/golfiv/storage/GolfConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityType;
import net.minecraft.item.ItemConvertible;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import org.samo_lego.golfiv.utils.GolfLogger;
Expand Down Expand Up @@ -87,14 +89,22 @@ public static class Survival {

@SerializedName("// Which items should be cleared when clicked in survival inventory")
public final String _comment_bannedSurvivalItems = "";
public ArrayList<String> bannedItems = new ArrayList<>(Arrays.asList(
"minecraft:barrier",
"minecraft:spawner",
"minecraft:structure_void",
"minecraft:bedrock",
"minecraft:command_block",
"minecraft:spawn_egg"
));
@JsonAdapter(BlockMapAdapter.class)
public Map<ItemConvertible, String> bannedItems = Map.ofEntries(
Map.entry(Blocks.BEDROCK, ""),
Map.entry(Blocks.BARRIER, ""),
Map.entry(Blocks.COMMAND_BLOCK, ""),
Map.entry(Blocks.CHAIN_COMMAND_BLOCK, ""),
Map.entry(Blocks.REPEATING_COMMAND_BLOCK, ""),
Map.entry(Blocks.STRUCTURE_BLOCK, ""),
Map.entry(Blocks.STRUCTURE_VOID, ""),
Map.entry(Blocks.JIGSAW, ""),
Map.entry(Blocks.SPAWNER, ""),
Map.entry(Blocks.NETHER_PORTAL, ""),
Map.entry(Blocks.END_PORTAL, ""),
Map.entry(Blocks.END_PORTAL_FRAME, ""),
Map.entry(Blocks.END_GATEWAY, ""));
public boolean banSpawnEggs = true;
public boolean checkEnchants = true;
public boolean checkPotionLevels = true;
public boolean checkItemCount = true;
Expand Down Expand Up @@ -341,6 +351,38 @@ public Set<Block> read(JsonReader in) throws IOException {
}
}


/**
* Adapts {@link ItemConvertible} and string {@link NbtCompound} between it and the identifier.
*/
private static final class BlockMapAdapter extends TypeAdapter<Map<ItemConvertible, String>> {

@Override
public void write(JsonWriter out, Map<ItemConvertible, String> value) throws IOException {
out.beginObject();
var reg = Registries.ITEM;
for (var entry : value.entrySet()) {
out.name(reg.getId(entry.getKey().asItem()).toString());
out.value(entry.getValue());
}
out.endObject();

}

@Override
public Map<ItemConvertible, String> read(JsonReader in) throws IOException {
in.beginObject();
var reg = Registries.ITEM;
var map = new HashMap<ItemConvertible, String>();
while (in.hasNext()) {
var key = reg.get(Identifier.tryParse(in.nextName()));
map.put(key, in.nextString());
}
in.endObject();
return map;
}
}

/**
* Adapts {@link EntityType} between it and the identifier.
* <p>
Expand Down

0 comments on commit ae4ed14

Please sign in to comment.