Skip to content

Commit

Permalink
Items stuff
Browse files Browse the repository at this point in the history
+ allStack
+ stack size command
  • Loading branch information
JohanVonElectrum committed Jun 28, 2021
1 parent b5c82c8 commit a021f85
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ org.gradle.jvmargs=-Xmx1G
carpet_core_version=1.4.16+v201105

# Mod Properties
mod_version = 2021.6.8
mod_version = 2021.6.28
maven_group = com.johanvonelectrum
archives_base_name = johan-carpet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class JohanExtension implements CarpetExtension {

public static final String MOD_NAME = "Johan Carpet Addon";
public static final String MOD_ID = "johan-carpet";
public static final String VERSION = "2021.6.8";
public static final String VERSION = "2021.6.28";

public static void noop() {}

Expand All @@ -35,6 +35,7 @@ public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher)
CommandSignal.register(dispatcher);
CommandComputation.register(dispatcher);
CommandBatch.register(dispatcher);
CommandItem.register(dispatcher);
}

private static Instant lastUpdateCheck = Instant.MIN;
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/johanvonelectrum/johan_carpet/JohanSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public String validate(ServerCommandSource serverCommandSource, ParsedRule<Strin

@Rule(
desc = "Enables /location command to know where is a player.",
category = { johanSettingsCategory, SURVIVAL, COMMAND }
category = { johanSettingsCategory, COMMAND }
)
public static boolean commandLocation = false;

Expand All @@ -101,13 +101,13 @@ public String validate(ServerCommandSource serverCommandSource, ParsedRule<Strin

@Rule(
desc = "Enables /total command to know the total sum of a scoreboard.",
category = { johanSettingsCategory, SURVIVAL, COMMAND }
category = { johanSettingsCategory, COMMAND }
)
public static boolean commandTotal = false;

@Rule(
desc = "Enables /computation command to test redstone contraptions.",
category = { johanSettingsCategory, SURVIVAL, COMMAND }
category = { johanSettingsCategory, COMMAND }
)
public static boolean commandComputation = false;

Expand All @@ -117,6 +117,12 @@ public String validate(ServerCommandSource serverCommandSource, ParsedRule<Strin
)
public static boolean commandBatch = false;

@Rule(
desc = "Enables /item command to get item data.",
category = { johanSettingsCategory, COMMAND }
)
public static boolean commandItem = false;

/* ===== End Commands Rules ===== */

/* ===== Begin Score Rules ===== */
Expand Down Expand Up @@ -228,6 +234,12 @@ public String validate(ServerCommandSource serverCommandSource, ParsedRule<Strin
)
public static boolean disableAnvilXpLimit;

@Rule(
desc = "All items have 64 stack size.",
category = { johanSettingsCategory, SURVIVAL, CHEAT }
)
public static boolean allStackable;

@Rule(
desc = "Using a bucket renamed to \"sponge\" removes fluids in the area.",
category = { johanSettingsCategory, SURVIVAL, CHEAT }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.johanvonelectrum.johan_carpet.commands;

import com.johanvonelectrum.johan_carpet.JohanSettings;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.item.Item;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.util.registry.Registry;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

public class CommandItem {

public static final Map<Integer, List<Item>> ITEMS = new HashMap<>();

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder literalargumentbuilder = literal("item")
.requires((player) -> JohanSettings.commandItem)
.then(argument("stack_size", IntegerArgumentType.integer(1, 64))
.executes(context -> getItemCount(context.getSource(), IntegerArgumentType.getInteger(context, "stack_size"), false))
.then(argument("list", BoolArgumentType.bool())
.executes(context -> getItemCount(context.getSource(), IntegerArgumentType.getInteger(context, "stack_size"), BoolArgumentType.getBool(context, "list")))
)
);

dispatcher.register(literalargumentbuilder);
}

private static int getItemCount(ServerCommandSource source, int stackSize, boolean list) throws CommandSyntaxException {
List<Item> items = ITEMS.get(stackSize);
if (items != null) {
source.sendFeedback(new LiteralText("There are " + items.size() + " items whose stack size is " + stackSize + "."), false);

if (!list)
return 1;

String result = items.stream()
.map(n -> Registry.ITEM.getId(n).toString())
.collect(Collectors.joining("\n", "", ""));
source.sendFeedback(new LiteralText(result), false);
return 0;
} else
source.sendError(new LiteralText("There are no items whose stack size is " + stackSize + "."));

return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BucketItemMixin {

@Shadow @Final private Fluid fluid;

@Inject(method = "use", at = @At("HEAD"), cancellable = true)
@Inject(method = "use", at = @At("INVOKE"), cancellable = true)
private void use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
ItemStack itemStack = user.getStackInHand(hand);
boolean sponge = JohanSettings.bucketSponge &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.johanvonelectrum.johan_carpet.mixins;

import com.johanvonelectrum.johan_carpet.JohanSettings;
import net.minecraft.item.Item;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Item.class)
public class ItemMixin {

@Inject(method = "getMaxCount", at=@At("HEAD"), cancellable = true)
public final void getMaxCount(CallbackInfoReturnable<Integer> cir) {
if (JohanSettings.allStackable)
cir.setReturnValue(64);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.johanvonelectrum.johan_carpet.mixins;

import com.johanvonelectrum.johan_carpet.commands.CommandItem;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.ArrayList;
import java.util.List;

@Mixin(Items.class)
public class ItemsMixin {

@Inject(method = "register(Lnet/minecraft/util/Identifier;Lnet/minecraft/item/Item;)Lnet/minecraft/item/Item;", at = @At("HEAD"))
private static void register(Identifier id, Item item, CallbackInfoReturnable<Item> cir) {
List<Item> eqSizeItems = CommandItem.ITEMS.computeIfAbsent(item.getMaxCount(), k -> new ArrayList<>());

eqSizeItems.add(item);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static String getLatestRelease() {
exception.printStackTrace();
response = JohanExtension.VERSION;
}
System.out.println("Latest version: " + response);
return response;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/johan_carpet.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"HeightmapMixin",
"HopperBlockEntityMixin",
"ItemFrameEntityMixin",
"ItemMixin",
"ItemsMixin",
"MinecraftServerMixin",
"PlayerManagerMixin",
"ServerChunkManagerMixin",
Expand Down

0 comments on commit a021f85

Please sign in to comment.