Code duplication
MiranCZ opened this issue ยท 19 comments
code from /glass
and /copystate
could be merged together in one method that would be given an interface to determine what type of item the player gets
an abstract command feature that takes a block the player is looking at in addition to the arguments might be a nice way to merge them
I could implement this
Just need to know how, because I was thinking about creating an interface that just returns itemstack based on block player is looking at and you are saying something about superclasses
I was thinking that instead of extending CommandFeature classed could extend RaycastFeature which in turn extends CommandFeature, RaycasFeature would get the block the player is looking at inside of the execute method, then it would call an abstract method which is the same as execute except it takes 1 additional argument, the block the player is looking at
I thought the whole method could be merged, because the only "variable" that changes is ItemStack
Oh, so the RayCast feature would call a method to get the ItemStack which could be overridden?
Oh, so the RayCast feature would call a method to get the ItemStack which could be overridden?
Why would it need to be overridden? The RaycastFeature would get the block the player is looking at or null if the player isn't looking at any and then call execute(source, arguments, block)
which is an abstract method of the abstract RaycastFeature class
Oh, so the RayCast feature would call a method to get the ItemStack which could be overridden?
Why would it need to be overridden? The RaycastFeature would get the block the player is looking at or null if the player isn't looking at any and then call
execute(source, arguments, block)
which is an abstract method of the abstract RaycastFeature class
Because the giving code is also the same
I dont see why you would need to override something that'll always be the same
I thought you override method that returns ItemStack based on block the player is looking at
I dont think it should return an itemstack, just the block the player is looking at
this is how I imagine it looking:
@Override
protected int execute(ServerCommandSource source, EmptyOptions options) throws CommandSyntaxException {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player == null || client.world == null) {
throw new CommandSyntaxException(null, Text.of("This command is client-side only"));
}
if (client.crosshairTarget == null || client.crosshairTarget.getType() != HitResult.Type.BLOCK) {
source.sendError(Text.of("You must be looking at a block to use this command"));
return -1;
}
BlockHitResult blockHit = (BlockHitResult) client.crosshairTarget;
BlockState blockState = client.world.getBlockState(blockHit.getBlockPos());
Block block = blockState.getBlock();
ItemStack itemStack = getStack(args);//override this
if (itemStack == null) {
source.sendError(Text.of("Invalid itemstack"));
return -1;
}
PlayerInventory playerInventory = client.player.getInventory();
playerInventory.addPickBlock(itemStack);
if (client.interactionManager == null) {
throw new CommandSyntaxException(null, Text.of("Failed to get interaction manager"));
}
client.interactionManager.clickCreativeStack(client.player.getStackInHand(Hand.MAIN_HAND), 36 + playerInventory.selectedSlot);
return Command.SINGLE_SUCCESS;
}
We could create another abstract class called something like PickBlockFeature which extends RaycastFeature and has an abstract method called getItemStack which gets passed the source, arguments and block the player is looking at
Not every command that works with the block the player is looking at will give the player an item
Closed by #69